UserDetailsService在身份認(rèn)證中的作用
Spring Security中進(jìn)行身份驗(yàn)證的是AuthenticationManager接口高帖,ProviderManager是它的一個(gè)默認(rèn)實(shí)現(xiàn)谁尸,但它并不用來處理身份認(rèn)證粘咖,而是委托給配置好的AuthenticationProvider,每個(gè)AuthenticationProvider會(huì)輪流檢查身份認(rèn)證趴久。檢查后或者返回Authentication對(duì)象或者拋出異常匾荆。
驗(yàn)證身份就是加載響應(yīng)的UserDetails,看看是否和用戶輸入的賬號(hào)粒督、密碼陪竿、權(quán)限等信息匹配。此步驟由實(shí)現(xiàn)AuthenticationProvider的DaoAuthenticationProvider(它利用UserDetailsService驗(yàn)證用戶名屠橄、密碼和授權(quán))處理族跛。包含 GrantedAuthority 的 UserDetails對(duì)象在構(gòu)建 Authentication對(duì)象時(shí)填入數(shù)據(jù)。