一、GlobalAuthenticationManager:
private AuthenticationConfiguration authenticationConfiguration;
public void setAuthenticationConfiguration(AuthenticationConfiguration authenticationConfiguration) {
this.authenticationConfiguration = authenticationConfiguration;
}
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
AuthenticationManager authenticationManager = authenticationConfiguration.getAuthenticationManager();
return http.build();
}
二、LocalAuthenticationManager:
public class MyCustom extends AbstractHttpConfigurer<MyCustom, HttpSecurity> {
@Override
public void configure(HttpSecurity http) throws Exception {
AuthenticationManager authenticationManager = http.getSharedObject(AuthenticationManager.class);
}
}
三、自定义配置类
形式一:
public class SmsLoginConfigurer extends AbstractHttpConfigurer<SmsLoginConfigurer, HttpSecurity> {
private AuthenticationSuccessHandler authenticationSuccessHandler;
private AuthenticationFailureHandler authenticationFailureHandler;
@Override
public void configure(HttpSecurity http) throws Exception {
SmsLoginAuthenticationFilter smsLoginAuthenticationFilter = new SmsLoginAuthenticationFilter("/smsCodeLogin");
smsLoginAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
smsLoginAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
smsLoginAuthenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler);
smsLoginAuthenticationFilter.setAuthenticationDetailsSource(new WebAuthenticationDetailsSource());
http.addFilterAfter(smsLoginAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
public final SmsLoginConfigurer successHandler(AuthenticationSuccessHandler authenticationSuccessHandler) {
this.authenticationSuccessHandler = authenticationSuccessHandler;
return this;
}
public final SmsLoginConfigurer failureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
this.authenticationFailureHandler = authenticationFailureHandler;
return this;
}
}
public DefaultSecurityFilterChain filterChain(HttpSecurity http,
MyAuthenticationSuccessHandler myAuthenticationSuccessHandler,
MyAuthenticationFailureHandler myAuthenticationFailureHandler,
MyAuthenticationEntryPoint myAuthenticationEntryPoint) throws Exception {
SmsLoginConfigurer<Httpsecurity> smsLoginConfigurer = new SmsLoginConfigurer();
smsLoginConfigurer.successHandler(myAuthenticationSuccessHandler);
smsLoginConfigurer.failureHandler(myAuthenticationFailureHandler);
http.authenticationProvider(new SmsLoginAuthenticationProvider());
http.apply(smsLoginConfigurer);
return http.build();
}
形式二:
public class SmsLoginConfigurer<B extends HttpSecurityBuilder<B>> extends AbstractHttpConfigurer<SmsLoginConfigurer<B>, B> {
private AuthenticationSuccessHandler authenticationSuccessHandler;
private AuthenticationFailureHandler authenticationFailureHandler;
@Override
public void configure(B builder) throws Exception {
SmsLoginAuthenticationFilter smsLoginAuthenticationFilter = new SmsLoginAuthenticationFilter("/smsCodeLogin");
smsLoginAuthenticationFilter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));
smsLoginAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
smsLoginAuthenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler);
smsLoginAuthenticationFilter.setAuthenticationDetailsSource(new WebAuthenticationDetailsSource());
builder.addFilterAfter(smsLoginAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
public final SmsLoginConfigurer<B> successHandler(AuthenticationSuccessHandler authenticationSuccessHandler) {
this.authenticationSuccessHandler = authenticationSuccessHandler;
return this;
}
public final SmsLoginConfigurer<B> failureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
this.authenticationFailureHandler = authenticationFailureHandler;
return this;
}
}
public DefaultSecurityFilterChain filterChain(HttpSecurity http,
MyAuthenticationSuccessHandler myAuthenticationSuccessHandler,
MyAuthenticationFailureHandler myAuthenticationFailureHandler,
MyAuthenticationEntryPoint myAuthenticationEntryPoint) throws Exception {
SmsLoginConfigurer smsLoginConfigurer = new SmsLoginConfigurer();
smsLoginConfigurer.successHandler(myAuthenticationSuccessHandler);
smsLoginConfigurer.failureHandler(myAuthenticationFailureHandler);
http.authenticationProvider(new SmsLoginAuthenticationProvider());
http.apply(smsLoginConfigurer);
return http.build();
}
此处完整的代替方案