Spring Security是什么
Spring Security是一個能夠為基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean(注:包括認證與權限獲取、配置、處理相關實例),充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)(注:代理增強類)功能,為應用系統提供聲明式的安全訪問控制功能,減少了為企業系統安全控制編寫大量重復代碼的工作。
核心類庫與認證流程
核心驗證器
AuthenticationManager
該對象提供了認證方法的入口,接收一個Authentiaton
對象作為參數;
public interface AuthenticationManager { Authentication authenticate(Authentication authentication) throws AuthenticationException; }
驗證邏輯
AuthenticationManager
接收 Authentication
對象作為參數,並通過 authenticate(Authentication)
方法對其進行驗證;AuthenticationProvider
實現類用來支撐對 Authentication
對象的驗證動作;UsernamePasswordAuthenticationToken
實現了 Authentication
主要是將用戶輸入的用戶名和密碼進行封裝,並供給 AuthenticationManager
進行驗證;驗證完成以后將返回一個認證成功的 Authentication
對象;
ProviderManager
它是 AuthenticationManager
的一個實現類,提供了基本的認證邏輯和方法;它包含了一個 List<AuthenticationProvider>
對象,通過 AuthenticationProvider 接口來擴展出不同的認證提供者(當Spring Security
默認提供的實現類不能滿足需求的時候可以擴展AuthenticationProvider
覆蓋supports(Class<?> authentication)
方法);
實現邏輯
public Authentication authenticate(Authentication authentication) throws AuthenticationException { //#1.獲取當前的Authentication的認證類型 Class<? extends Authentication> toTest = authentication.getClass(); AuthenticationException lastException = null; Authentication result = null; boolean debug = logger.isDebugEnabled(); //#2.遍歷所有的providers使用supports方法判斷該provider是否支持當前的認證類型,不支持的話繼續遍歷 for (AuthenticationProvider provider : getProviders()) { if (!provider.supports(toTest)) { continue; } if (debug) { logger.debug("Authentication attempt using " + provider.getClass().getName()); } try { #3.支持的話調用provider的authenticat方法認證 result = provider.authenticate(authentication); if (result != null) { #4.認證通過的話重新生成Authentication對應的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 沒有驗證通過,則使用父類型AuthenticationManager進行驗證 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 // from authentication ((CredentialsContainer