Spring Security認證提供程序示例詳解
1.簡介
本教程將介紹如何在Spring Security中設置身份驗證提供程序,與使用簡單UserDetailsService的標準方案相比,提供了額外的靈活性。
2. The Authentication Provider
Spring Security提供了多種執(zhí)行身份驗證的選項 - 所有這些都遵循簡單的規(guī)范 - 身份驗證請求由Authentication Provider處理,并且返回具有完整憑據(jù)的完全身份驗證的對象。
標準和最常見的實現(xiàn)是DaoAuthenticationProvider - 它從一個簡單的只讀用戶DAO檢索用戶詳細信息 - UserDetailsService。此UserDetailsService只能訪問用戶名,用來檢索完整的用戶實體 - 在很多情況下,這就足夠了。
更多常見的場景仍然需要訪問完整的身份驗證請求才能執(zhí)行身份驗證過程。例如,在針對某些外部第三方服務(例如Crowd)進行身份驗證時,將需要來自身份驗證請求的用戶名和密碼。
對于這些更高級的方案,我們需要定義自定義身份驗證提供程序:
@Component public class CustomAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String name = authentication.getName(); String password = authentication.getCredentials().toString(); if (shouldAuthenticateAgainstThirdPartySystem()) { // use the credentials // and authenticate against the third-party system return new UsernamePasswordAuthenticationToken( name, password, new ArrayList<>()); } else { return null; } } @Override public boolean supports(Class<?> authentication) { return authentication.equals( UsernamePasswordAuthenticationToken.class); } }
請注意,在返回的Authentication對象上設置的授予權(quán)限是空的 - 這是因為權(quán)限當然是特定于應用程序的。
3.注冊Authentication Provider
既然定義了身份驗證提供程序,我們需要使用可用的命名空間支持在XML安全配置中指定它:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <http use-expressions="true"> <intercept-url pattern="/**" access="isAuthenticated()"/> <http-basic/> </http> <authentication-manager> <authentication-provider ref="customAuthenticationProvider" /> </authentication-manager> </beans:beans>
4. Java Configuration
接下來,我們來看看相應的Java配置:
@Configuration @EnableWebSecurity @ComponentScan("org.baeldung.security") public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomAuthenticationProvider authProvider; @Override protected void configure( AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(authProvider); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated() .and() .httpBasic(); } }
5. 測試認證
無論是否在后端使用此自定義身份驗證提供程序,從客戶端請求身份驗證基本相同 - 我們可以使用簡單的curl命令發(fā)送經(jīng)過身份驗證的請求:
curl --header "Accept:application/json" -i --user user1:user1Pass http://localhost:8080/spring-security-custom/api/foo/1
請注意 - 出于本示例的目的 - 我們已使用基本身份驗證保護REST API。
我們從服務器返回預期的200 OK
HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Set-Cookie: JSESSIONID=B8F0EFA81B78DE968088EBB9AFD85A60; Path=/spring-security-custom/; HttpOnly Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Sun, 02 Jun 2013 17:50:40 GMT
六,總結(jié)
在本文中,我們討論了Spring Security的自定義身份驗證提供程序的示例。
可以在GitHub項目中找到本教程的完整實現(xiàn) - 這是一個基于Maven的項目,因此它應該很容易導入和運行。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關(guān)文章
SpringBoot整合Xxl-job實現(xiàn)定時任務的全過程
XXL-JOB是一個分布式任務調(diào)度平臺,其核心設計目標是開發(fā)迅速、學習簡單、輕量級、易擴展,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Xxl-job實現(xiàn)定時任務的相關(guān)資料,需要的朋友可以參考下2022-01-01快速學習JavaWeb中監(jiān)聽器(Listener)的使用方法
這篇文章主要幫助大家快速學習JavaWeb中監(jiān)聽器(Listener)的使用方法,感興趣的小伙伴們可以參考一下2016-09-09一文帶你搞懂Java中Synchronized和Lock的原理與使用
這篇文章主要為大家詳細介紹了Java中Synchronized和Lock的原理與使用,文中的示例代碼講解詳細,對我們學習Java有一定的幫助,需要的可以參考一下2023-04-04SpringBoot?Loki安裝簡介及實戰(zhàn)思路
這篇文章主要為大家介紹了SpringBoot?Loki安裝簡介及實戰(zhàn)思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪的相關(guān)資料2022-11-11SpringCloud中使用Sentinel實現(xiàn)限流的實戰(zhàn)
限流在很多地方都可以使用的到,本篇博客將介紹如何使用SpringCloud中使用Sentinel實現(xiàn)限流,從而達到服務降級的目的,感興趣的可以了解一下2022-01-01