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

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.

主站蜘蛛池模板: 隆回县| 西畴县| 聂荣县| 淮阳县| 江阴市| 琼中| 玛沁县| 慈利县| 莆田市| 教育| 郑州市| 大埔区| 庆云县| 敦煌市| 绥化市| 麻江县| 双桥区| 顺平县| 濮阳市| 惠来县| 当雄县| 刚察县| 日土县| 阿巴嘎旗| 永登县| 酒泉市| 工布江达县| 垣曲县| 达拉特旗| 通辽市| 巴彦县| 加查县| 东方市| 新乡市| 临安市| 静海县| 台中县| 铜川市| 广昌县| 新宾| 通河县|