- Hands-On Spring Security 5 for Reactive Applications
- Tomcy John
- 158字
- 2021-07-23 18:59:20
Multiple AuthenticationProvider
Spring Security allows you to declare multiple AuthenticationProvider in your application. They are executed according to the order in which they are declared in the configuration.
The jetty-in-memory-basic-custom-authentication project is modified further, and we have used the newly created CustomAuthenticationProvider as an AuthenticationProvider (Order 1) and the existing inMemoryAuthentication as our second AuthenticationProvider (Order 2):
@EnableWebSecurity
@ComponentScan(basePackageClasses = CustomAuthenticationProvider.class)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
CustomAuthenticationProvider customAuthenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/**")
.authenticated(); // Use Basic authentication
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// Custom authentication provider - Order 1
auth.authenticationProvider(customAuthenticationProvider);
// Built-in authentication provider - Order 2
auth.inMemoryAuthentication()
.withUser("admin")
.password("{noop}admin@password")
//{noop} makes sure that the password encoder doesn't do anything
.roles("ADMIN") // Role of the user
.and()
.withUser("user")
.password("{noop}user@password")
.credentialsExpired(true)
.accountExpired(true)
.accountLocked(true)
.roles("USER");
}
}
Whenever the authenticate method executes without error, the controls return and thereafter configured AuthenticationProvider's doesn't get executed.
推薦閱讀
- Securing Blockchain Networks like Ethereum and Hyperledger Fabric
- Web安全與攻防入門很輕松(實(shí)戰(zhàn)超值版)
- Enterprise Cloud Security and Governance
- 網(wǎng)絡(luò)安全技術(shù)及應(yīng)用(第3版)
- 防火墻技術(shù)與應(yīng)用(第2版)
- 移動(dòng)APT:威脅情報(bào)分析與數(shù)據(jù)防護(hù)
- 從0到1:CTFer成長(zhǎng)之路
- 網(wǎng)絡(luò)安全與攻防入門很輕松(實(shí)戰(zhàn)超值版)
- 軟件安全保障體系架構(gòu)
- 互聯(lián)網(wǎng)企業(yè)安全高級(jí)指南
- 數(shù)據(jù)要素安全:新技術(shù)、新安全激活新質(zhì)生產(chǎn)力
- 人工智能安全(精裝版)
- 網(wǎng)絡(luò)空間安全導(dǎo)論
- 復(fù)雜信息系統(tǒng)網(wǎng)絡(luò)安全體系建設(shè)指南
- 企業(yè)安全建設(shè)入門:基于開源軟件打造企業(yè)網(wǎng)絡(luò)安全