安全歸根節點須要解決兩個問題:java
後者有些時候也被人們叫作「訪問控制」(access control)。spring security 是一個將鑑權和受權分開的安全框架。包含策略,同時提供對二者的擴展能力。spring
下圖描繪了spring security在鑑權處理流程中涉及到的類和過濾器。數據庫
public interface AuthenticationManager{ Authentication authenticate(Authentication authentication)throws AuthenticationException; }
實際實現類是 ProviderManager 。該實現類包含了一系列配置的 AuthenticationProvider (s), 這些provider受ProvideManager委託,用於用戶請求的鑑權。安全
具體到實現細節, ProviderManager 會遍歷每一個AuthenticationProvider,根據傳入的Authentication對象(例如: UsernamePasswordAuthenticationToken )嘗試鑑權用戶。AuthenticationProvider 接口以下所示:springboot
public interface AuthenticationProvider { Authentication authenticate(Authentication authentication) throws AuthenticationException; boolean supports(Class<?> authentication); }
這裏是一些Spring security框架提供的AuthenticationProvider:框架
public interface UserDetailsService{ UserDetails loadUserByUsername(String username) throws UsernameNotFoundException; }
UserDetailsService 返回 UserDetails 。而 User 是 UserDetails 的具體實現類,固然用戶也能夠自行實現 UserDetails 接口。ide
若是用戶鑑權成功,則返回Authentication對象,相比於傳入的未鑑權的對象,這個鑑權後的的對象更「豐滿」,包含了用戶的角色信息spa
從上圖能夠看出,鑑權成功的Authentication對象(Fully populated Authentication Object)包含:3d
authenticated- truecode
grant authorities list :關聯的角色(角色和權限掛鉤)
user credentials:用戶憑證(僅僅包含用戶名)
若是鑑權未經過,則拋出異常 AuthenticationException 。該異常屬於運行時異常,不指望用戶經過try/catch去處理,spring security提供了一種通用的方式。即經過AuthenticationEntryPoint 處理:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler);
}
9. Authentication is done! 鑑權成功後,AuthenticationManager返回包含完整信息的鑑權對象給相關的Authentication Filter。
10. 將返回的鑑權對象保存到SecurityContext,用於後續過濾器的使用。好比Authorization Filters
SecurityContextHolder.getContext().setAuthentication(authentication);
https://springbootdev.com/2017/08/23/spring-security-authentication-architecture/