- 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.
推薦閱讀
- Extending Symfony2 Web Application Framework
- DevSecOps敏捷安全
- 數(shù)據(jù)恢復(fù)方法及案例分析
- 開發(fā)者的Web安全戒律:真實威脅與防御實踐
- 深入淺出隱私計算:技術(shù)解析與應(yīng)用實踐
- 網(wǎng)絡(luò)安全技術(shù)及應(yīng)用(第3版)
- Kali Linux Wireless Penetration Testing Cookbook
- 云原生安全技術(shù)實踐指南
- 數(shù)據(jù)安全領(lǐng)域指南
- 解密數(shù)據(jù)恢復(fù)
- 網(wǎng)絡(luò)安全大數(shù)據(jù)分析與實戰(zhàn)
- Kali Linux高級滲透測試(原書第4版)
- 網(wǎng)絡(luò)安全實戰(zhàn)詳解(企業(yè)專供版)
- INSTANT Kali Linux
- 數(shù)字銀行安全體系構(gòu)建