Spring Security 5.7.5 CURRENT GA获取Global/LocalAuthenticationManager以及自定义配置类

一、GlobalAuthenticationManager:

    private AuthenticationConfiguration authenticationConfiguration;

    public void setAuthenticationConfiguration(AuthenticationConfiguration authenticationConfiguration) {
        this.authenticationConfiguration = authenticationConfiguration;
    }
    
    @Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // 获取GlobalAuthenticationManager
        AuthenticationManager authenticationManager = authenticationConfiguration.getAuthenticationManager();
        
        return http.build();
    }

二、LocalAuthenticationManager:

public class MyCustom extends AbstractHttpConfigurer<MyCustom, HttpSecurity> {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        // 获取LocalAuthenticationManager
        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());  // 设置自己的Provider或者直接使用LocalAuthenticationManager中的Provider。
        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());  // 设置自己的Provider或者直接使用LocalAuthenticationManager中的Provider。
        http.apply(smsLoginConfigurer);  // 应用自己的配置类
        return http.build();
    }

此处完整的代替方案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cs4m

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值