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

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.

主站蜘蛛池模板: 吐鲁番市| 突泉县| 龙泉市| 丁青县| 习水县| 拉萨市| 云阳县| 通榆县| 靖宇县| 抚宁县| 洛扎县| 伊春市| 华坪县| 襄樊市| 霍州市| 民乐县| 大悟县| 犍为县| 成武县| 靖边县| 巴林左旗| 三穗县| 南郑县| 乐陵市| 富顺县| 昌都县| 盐亭县| 尉犁县| 出国| 山阴县| 合肥市| 北辰区| 广宗县| 巴彦淖尔市| 太湖县| 肇东市| 沽源县| 原阳县| 靖江市| 正蓝旗| 犍为县|