javascript
Spring Security原理与应用
Spring Security是什么
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應(yīng)用上下文中配置的Bean(注:包括認(rèn)證與權(quán)限獲取、配置、處理相關(guān)實(shí)例),充分利用了Spring IoC,DI(控制反轉(zhuǎn)Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)(注:代理增強(qiáng)類)功能,為應(yīng)用系統(tǒng)提供聲明式的安全訪問控制功能,減少了為企業(yè)系統(tǒng)安全控制編寫大量重復(fù)代碼的工作。
核心類庫與認(rèn)證流程
核心驗(yàn)證器
AuthenticationManager
該對象提供了認(rèn)證方法的入口,接收一個(gè)Authentiaton對象作為參數(shù);
public interface AuthenticationManager {Authentication authenticate(Authentication authentication) throws AuthenticationException; }驗(yàn)證邏輯
AuthenticationManager?接收?Authentication?對象作為參數(shù),并通過?authenticate(Authentication)?方法對其進(jìn)行驗(yàn)證;AuthenticationProvider實(shí)現(xiàn)類用來支撐對?Authentication?對象的驗(yàn)證動(dòng)作;UsernamePasswordAuthenticationToken實(shí)現(xiàn)了?Authentication主要是將用戶輸入的用戶名和密碼進(jìn)行封裝,并供給?AuthenticationManager?進(jìn)行驗(yàn)證;驗(yàn)證完成以后將返回一個(gè)認(rèn)證成功的?Authentication?對象;
ProviderManager
它是?AuthenticationManager?的一個(gè)實(shí)現(xiàn)類,提供了基本的認(rèn)證邏輯和方法;它包含了一個(gè)?List<AuthenticationProvider>?對象,通過 AuthenticationProvider 接口來擴(kuò)展出不同的認(rèn)證提供者(當(dāng)Spring Security默認(rèn)提供的實(shí)現(xiàn)類不能滿足需求的時(shí)候可以擴(kuò)展AuthenticationProvider?覆蓋supports(Class<?> authentication)?方法);
實(shí)現(xiàn)邏輯
public Authentication authenticate(Authentication authentication) throws AuthenticationException { //#1.獲取當(dāng)前的Authentication的認(rèn)證類型 Class<? extends Authentication> toTest = authentication.getClass(); AuthenticationException lastException = null; Authentication result = null; boolean debug = logger.isDebugEnabled(); //#2.遍歷所有的providers使用supports方法判斷該provider是否支持當(dāng)前的認(rèn)證類型,不支持的話繼續(xù)遍歷 for (AuthenticationProvider provider : getProviders()) { if (!provider.supports(toTest)) { continue; } if (debug) { logger.debug("Authentication attempt using " + provider.getClass().getName()); } try { #3.支持的話調(diào)用provider的authenticat方法認(rèn)證 result = provider.authenticate(authentication); if (result != null) { #4.認(rèn)證通過的話重新生成Authentication對應(yīng)的Token copyDetails(authentication, result); break; } } catch (AccountStatusException e) { prepareException(e, authentication); // SEC-546: Avoid polling additional providers if auth failure is due to // invalid account status throw e; } catch (InternalAuthenticationServiceException e) { prepareException(e, authentication); throw e; } catch (AuthenticationException e) { lastException = e; } } if (result == null && parent != null) { // Allow the parent to try. try { #5.如果#1 沒有驗(yàn)證通過,則使用父類型AuthenticationManager進(jìn)行驗(yàn)證 result = parent.authenticate(authentication); } catch (ProviderNotFoundException e) { // ignore as we will throw below if no other exception occurred prior to // calling parent and the parent // may throw ProviderNotFound even though a provider in the child already // handled the request } catch (AuthenticationException e) { lastException = e; } } #6. 是否擦除敏感信息 if (result != null) { if (eraseCredentialsAfterAuthentication && (result instanceof CredentialsContainer)) { // Authentication is complete. Remove credentials and other secret data轉(zhuǎn)載于:https://www.cnblogs.com/free-wings/p/9308592.html
總結(jié)
以上是生活随笔為你收集整理的Spring Security原理与应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 酷狗铃声怎么设置闹钟铃声
- 下一篇: 史上最简单的SpringCloud教程