Spring Security認(rèn)證提供程序示例詳解
1.簡介
本教程將介紹如何在Spring Security中設(shè)置身份驗證提供程序,與使用簡單UserDetailsService的標(biāo)準(zhǔn)方案相比,提供了額外的靈活性。
2. The Authentication Provider
Spring Security提供了多種執(zhí)行身份驗證的選項 - 所有這些都遵循簡單的規(guī)范 - 身份驗證請求由Authentication Provider處理,并且返回具有完整憑據(jù)的完全身份驗證的對象。
標(biāo)準(zhǔn)和最常見的實現(xiàn)是DaoAuthenticationProvider - 它從一個簡單的只讀用戶DAO檢索用戶詳細(xì)信息 - UserDetailsService。此UserDetailsService只能訪問用戶名,用來檢索完整的用戶實體 - 在很多情況下,這就足夠了。
更多常見的場景仍然需要訪問完整的身份驗證請求才能執(zhí)行身份驗證過程。例如,在針對某些外部第三方服務(wù)(例如Crowd)進(jì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);
}
}
請注意,在返回的Authentication對象上設(shè)置的授予權(quán)限是空的 - 這是因為權(quán)限當(dāng)然是特定于應(yīng)用程序的。
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
接下來,我們來看看相應(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. 測試認(rèn)證
無論是否在后端使用此自定義身份驗證提供程序,從客戶端請求身份驗證基本相同 - 我們可以使用簡單的curl命令發(fā)送經(jīng)過身份驗證的請求:
curl --header "Accept:application/json" -i --user user1:user1Pass http://localhost:8080/spring-security-custom/api/foo/1
請注意 - 出于本示例的目的 - 我們已使用基本身份驗證保護(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的自定義身份驗證提供程序的示例。
可以在GitHub項目中找到本教程的完整實現(xiàn) - 這是一個基于Maven的項目,因此它應(yīng)該很容易導(dǎo)入和運行。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Java設(shè)計模式之備忘錄模式實現(xiàn)對象狀態(tài)的保存和恢復(fù)
本文介紹Java設(shè)計模式之備忘錄模式,該模式可以實現(xiàn)對象狀態(tài)的保存和恢復(fù)。通過詳細(xì)講解備忘錄模式的原理、實現(xiàn)方法和應(yīng)用場景,幫助讀者深入理解該設(shè)計模式,并提供示例代碼和技巧,便于讀者實際應(yīng)用2023-04-04
Java中LinkedList數(shù)據(jù)結(jié)構(gòu)的詳細(xì)介紹
這篇文章主要介紹了Java中LinkedList,Linked List 是 java.util 包中 Collection 框架的一部分,文中提供了詳細(xì)的代碼說明,需要的朋友可以參考下2023-05-05
springboot+vue2+elementui實現(xiàn)時間段查詢方法
這篇文章主要介紹了springboot+vue2+elementui實現(xiàn)時間段查詢方法,本文通過實例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05

