標籤:win 添加 boot builder 程式 reflect 唯讀 身份認證 比較
談到安全,如現在市面上有的 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的內容物件及策略相關,這雷根據不同的策略擷取擷取到三種:
ThreadLocalSecurityContextHolderStrategyInheritableThreadLocalSecurityContextHolderStrategyGlobalSecurityContextHolderStrategy
當然也可以自訂策略處理,有單獨的自定處理
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:身份類型
這一篇就說道這裡,可能說的不夠詳細~
Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security