Spring Security認(rèn)證提供程序示例詳解
1.簡(jiǎn)介
本教程將介紹如何在Spring Security中設(shè)置身份驗(yàn)證提供程序,與使用簡(jiǎn)單UserDetailsService的標(biāo)準(zhǔn)方案相比,提供了額外的靈活性。
2. The Authentication Provider
Spring Security提供了多種執(zhí)行身份驗(yàn)證的選項(xiàng) - 所有這些都遵循簡(jiǎn)單的規(guī)范 - 身份驗(yàn)證請(qǐng)求由Authentication Provider處理,并且返回具有完整憑據(jù)的完全身份驗(yàn)證的對(duì)象。
標(biāo)準(zhǔn)和最常見的實(shí)現(xiàn)是DaoAuthenticationProvider - 它從一個(gè)簡(jiǎn)單的只讀用戶DAO檢索用戶詳細(xì)信息 - UserDetailsService。此UserDetailsService只能訪問用戶名,用來檢索完整的用戶實(shí)體 - 在很多情況下,這就足夠了。
更多常見的場(chǎng)景仍然需要訪問完整的身份驗(yàn)證請(qǐng)求才能執(zhí)行身份驗(yàn)證過程。例如,在針對(duì)某些外部第三方服務(wù)(例如Crowd)進(jìn)行身份驗(yàn)證時(shí),將需要來自身份驗(yàn)證請(qǐng)求的用戶名和密碼。
對(duì)于這些更高級(jí)的方案,我們需要定義自定義身份驗(yàn)證提供程序:
@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); } }
請(qǐng)注意,在返回的Authentication對(duì)象上設(shè)置的授予權(quán)限是空的 - 這是因?yàn)闄?quán)限當(dāng)然是特定于應(yīng)用程序的。
3.注冊(cè)Authentication Provider
既然定義了身份驗(yàn)證提供程序,我們需要使用可用的命名空間支持在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
接下來,我們來看看相應(yīng)的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. 測(cè)試認(rèn)證
無論是否在后端使用此自定義身份驗(yàn)證提供程序,從客戶端請(qǐng)求身份驗(yàn)證基本相同 - 我們可以使用簡(jiǎn)單的curl命令發(fā)送經(jīng)過身份驗(yàn)證的請(qǐng)求:
curl --header "Accept:application/json" -i --user user1:user1Pass http://localhost:8080/spring-security-custom/api/foo/1
請(qǐng)注意 - 出于本示例的目的 - 我們已使用基本身份驗(yàn)證保護(hù)REST API。
我們從服務(wù)器返回預(yù)期的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的自定義身份驗(yàn)證提供程序的示例。
可以在GitHub項(xiàng)目中找到本教程的完整實(shí)現(xiàn) - 這是一個(gè)基于Maven的項(xiàng)目,因此它應(yīng)該很容易導(dǎo)入和運(yùn)行。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
SpringBoot整合Xxl-job實(shí)現(xiàn)定時(shí)任務(wù)的全過程
XXL-JOB是一個(gè)分布式任務(wù)調(diào)度平臺(tái),其核心設(shè)計(jì)目標(biāo)是開發(fā)迅速、學(xué)習(xí)簡(jiǎn)單、輕量級(jí)、易擴(kuò)展,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Xxl-job實(shí)現(xiàn)定時(shí)任務(wù)的相關(guān)資料,需要的朋友可以參考下2022-01-01快速學(xué)習(xí)JavaWeb中監(jiān)聽器(Listener)的使用方法
這篇文章主要幫助大家快速學(xué)習(xí)JavaWeb中監(jiān)聽器(Listener)的使用方法,感興趣的小伙伴們可以參考一下2016-09-09一文帶你搞懂Java中Synchronized和Lock的原理與使用
這篇文章主要為大家詳細(xì)介紹了Java中Synchronized和Lock的原理與使用,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2023-04-04SpringBoot?Loki安裝簡(jiǎn)介及實(shí)戰(zhàn)思路
這篇文章主要為大家介紹了SpringBoot?Loki安裝簡(jiǎn)介及實(shí)戰(zhàn)思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪的相關(guān)資料2022-11-11詳解領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)之事件驅(qū)動(dòng)與CQRS
這篇文章分析了如何應(yīng)用事件來分離軟件核心復(fù)雜度。探究CQRS為什么廣泛應(yīng)用于DDD項(xiàng)目中,以及如何落地實(shí)現(xiàn)CQRS框架。當(dāng)然我們也要警惕一些失敗的教訓(xùn),利弊分析以后再去抉擇正確的應(yīng)對(duì)之道2021-06-06SpringCloud中使用Sentinel實(shí)現(xiàn)限流的實(shí)戰(zhàn)
限流在很多地方都可以使用的到,本篇博客將介紹如何使用SpringCloud中使用Sentinel實(shí)現(xiàn)限流,從而達(dá)到服務(wù)降級(jí)的目的,感興趣的可以了解一下2022-01-01