2 回答

TA貢獻1873條經(jīng)驗 獲得超9個贊
由于您的要求是擁有多個身份驗證入口點,因此它并不像 Atul 的答案那么簡單。
你需要的是
您需要在登錄時區(qū)分客戶和員工。(首選方式單選按鈕)
您需要實現(xiàn)自定義身份驗證過濾器,即實現(xiàn)
UsernamePasswordAuthenticationFilter
代替 spring-security 提供的默認值.formLogin()
創(chuàng)建兩個
UsernamePasswordAuthenticationToken
asEmployeeUsernamePasswordAuthenticationToken
和CustomerUsernamePasswordAuthenticationToken
在您的自定義過濾器中,從請求中獲取 userType,并根據(jù) userType 將 authToken 設置為 empAuthToken 或 customerAuthToken 以區(qū)分所需的身份驗證提供程序。
創(chuàng)建
AuthenticationProvider
為EmployeeCustomAuthenticationProvider
并CustomerCustomAuthenticationProvider
在每個AuthenticationProvider
應該重寫的地方支持方法,其中 AuthenticationProvider 支持特定令牌(customerAuthToken 或 employeeAuthToken)。覆蓋身份驗證方法,其中身份驗證方法已通過身份驗證參數(shù)傳遞,您可以從中獲取用戶名和密碼,您可以將其傳遞給任何自定義服務以對用戶進行身份驗證并授予用戶所需的權(quán)限。
在實現(xiàn) CustomAuthenticationFilter 時,還需要提供自定義的authenticationSuccessHandler 和 AuthenticationFailureHandlers。
如果您實現(xiàn)上述所有內(nèi)容而沒有任何錯誤,則可以避免在配置了兩個 customAuthenticationProvider 的情況下 spring-security 默認提供的回退身份驗證。

TA貢獻1804條經(jīng)驗 獲得超8個贊
“是否會有兩個 UserDetailsService 和兩個 AuthenticationProvider 分別指向自己的表(Customer 和 Employee)?” ......答案是肯定的。
Spring security有過濾器,UsernamePasswordAuthenticationFilter(檢查過濾器的名稱),您可以根據(jù)輸入類型來實現(xiàn)具體的實現(xiàn)。
我做了同樣的事情,但是針對不同的身份驗證機制。但根據(jù)您的要求,這可能是您想要的。
添加回答
舉報