Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security


 

Java Spring Boot VS .NetCore (一)來一個簡單的 Hello World

Java Spring Boot VS .NetCore (二)實現一個過濾器Filter

Java Spring Boot VS .NetCore (三)Ioc容器處理

Java Spring Boot VS .NetCore (四)數據庫操作 Spring Data JPA vs EFCore

Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

Java Spring Boot VS .NetCore (七) 配置文件

Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute

Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor

Java Spring Boot VS .NetCore (十一)自定義標簽 Java Tag Freemarker VS .NetCore Tag TagHelper

談到安全,如現在市面上有的 OAuth2 \ OIDC -OpenId Connect ,身份認證、授權等,下面先來說下Java Security

這一塊的東西非常多復雜,不能是Spring Security 還是 .NetCore Security,一點一點的比較說明

Spring Security

組成部分:

SecurityContextHolder, 提供幾種訪問 SecurityContext的方式。

SecurityContext, 保存Authentication信息和請求對應的安全信息。

Authentication, 展示Spring Security特定的主體。

GrantedAuthority, 反應,在應用程序范圍你,賦予主體的權限。

UserDetails,通過你的應用DAO,提供必要的信息,構建Authentication對象。

UserDetailsService, 創建一個UserDetails,傳遞一個 String類型的用戶名(或者證書ID或其他).

Spring Security 安全種的 SecurityContextHolder 對象 與 .NetCore中的 HttpContext上下對象 針對 Security這塊  類似,當然.NetCore中的HttpContext 還有其他職責,這里就 HttpContext Authentication 說事

SecurityContextHolder:為我們提供了 獲取 SecurityContext的上下文對象及策略相關,這里根據不同的策略獲取獲取到三種:

ThreadLocalSecurityContextHolderStrategy

InheritableThreadLocalSecurityContextHolderStrategy

GlobalSecurityContextHolderStrategy

當然也可以自定義策略處理,有單獨的自定處理

else {
            try {
                Class<?> clazz = Class.forName(strategyName);
                Constructor<?> customStrategy = clazz.getConstructor();
                strategy = (SecurityContextHolderStrategy)customStrategy.newInstance();
            } catch (Exception var2) {
                ReflectionUtils.handleReflectionException(var2);
            }

SecurityContext: 通過這個對象我們可以獲取到 授權信息

SecurityContextHolder.getContext().getAuthentication()
public interface Authentication extends Principal, Serializable {
    Collection<? extends GrantedAuthority> getAuthorities();

    Object getCredentials();

    Object getDetails();

    Object getPrincipal();

    boolean isAuthenticated();

    void setAuthenticated(boolean var1) throws IllegalArgumentException;
}

這里就跟 .NetCore中的 HttpContext.User.Identity 身份信息一致 

Spring中 Security getAuthentication 得到了授權身份信息,那么這個身份 有沒有授權,是什么樣的身份信息呢?這里都能得到相關的處理

那么獲取想當前訪問人的信息 

Object principal=  SecurityContextHolder.getContext().getAuthentication().getPrincipal();

這里跟.NetCore  Authentication下的 方法類是 ,這個下面也封裝了 Principal (ClaimsPrincipal 類型),當然對外部也提供了 那就是 User強轉 ClaimsPrincipal 

 public abstract Task<AuthenticateInfo> GetAuthenticateInfoAsync

看下.NetCore下面的強轉:

  var user = HttpContext.User as ClaimsPrincipal;

這點其實在Spring 里面也存在這個處理 看到 getPrincipal() 獲取去當事人信息的時候得到的是 Object對象 並不是 UserDeatils這個 對象

所以 Spring Security 里面 也有這么一出 

Object principal=  SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal instanceof UserDetails) {
            String username = ((UserDetails)principal).getUsername();
        } else {
            String username = principal.toString();
        }

這里跟.NetCore中的擴展登錄信息一樣 需要處理 當事人的身份信息,這我用.NeCore中 Windows 身份當事人信息來舉例子

if (result?.Principal is WindowsPrincipal wp)
 {
 
 id.AddClaim(new Claim(JwtClaimTypes.Subject, wp.Identity.Name));
}

這一點跟上面的Spring Security 是同樣的原理 

 

.NetCore 

首先拋開Session這種登錄處理,這里介紹的是 Authentication認證,下面簡單介紹下

AuthenticationBuilder :創建認證
AuthenticationSchemeOptions :認證的參數
AuthenticationHandler :認證處理
AuthenticationMiddleware : 認證中間件

.NetCore下 首先

添加認證服務給出參數

services.AddAuthentication(
              options =>
              {
                  options.DefaultScheme = "Cookies";
                 // options.DefaultChallengeScheme = "oidc";
               
              })

然后添加授權認證的中間件,說有授權都是中間件來處理,這里可以去看中間件的原理,處理完成后會把信息寫入HttpContext上下文對象中的身份認證信息,同時暴露對HttpContext的安全訪問

 app.UseAuthentication();

代碼中通過 SignInAsync、SignOutAsync 處理 (這里是異步) 這些方法暴露給了Httpcontext 同時也暴露給了 AuthenticationManager  對象

SignIn 會把通過本地驗證后的信息寫入認證相關的對象中,同時中間件對HttpContext上下問提供安全訪問

所以在代碼中我們一般這樣處理:這里提供認證管理 只讀的安全訪問對象操作

public abstract AuthenticationManager Authentication { get; }

同時還擴展暴露了 身份信息

public abstract ClaimsPrincipal User { get; set; }

這個玩意是用來干什么的呢?其實就是為了我們獲取認證的身份信息

可以看下這個下面的身份信息,下面有IsAuthenticated 、Name 、AuthenticationType

HttpContext.User.Identity

IsAuthenticated :這個用戶的身份 是否認證

Name: 這個用戶的身份 是誰 是哪個人

AuthenticationType:身份類型

 

這一篇就說道這里,可能說的不夠詳細~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM