官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 肇东市| 安新县| 辽宁省| 汪清县| 敖汉旗| 和平区| 澜沧| 涪陵区| 杭锦后旗| 祥云县| 桐城市| 佳木斯市| 余姚市| 五家渠市| 闸北区| 兴和县| 白山市| 蒙阴县| 司法| 盘锦市| 巴中市| 合江县| 南郑县| 侯马市| 阜城县| 大港区| 石楼县| 旌德县| 临清市| 如东县| 瓦房店市| 广丰县| 平度市| 介休市| 东乌珠穆沁旗| 衡南县| 封开县| 眉山市| 通山县| 崇信县| 许昌县|