记一次继承了AbstractAuthenticationProcessingFilter 的过滤器被执行了两次问题

在项目中使用了为了使用spring security的token方式进行鉴权,继承了AbstractAuthenticationProcessingFilter来对请求拦截处理,如下:

public class JwtAuthenticationTokenFilter extends AbstractAuthenticationProcessingFilter {

    private SelectUserInfoWebService selectUserInfoWebService;

    private CacheClient cacheClient;

    public void setCacheClient(CacheClient cacheClient) {
        this.cacheClient = cacheClient;
    }

    public void setSelectUserInfoWebService(SelectUserInfoWebService selectUserInfoWebService) {
        this.selectUserInfoWebService = selectUserInfoWebService;
    }

    public JwtAuthenticationTokenFilter() {
        super("/auth/**");
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
//部分代码

另外需要一个集成WebSecurityConfigurerAdapter的配置类,如下,一开始是这样写的:

@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 根据用户id获取用户信息
     */
    @HSFConsumer(serviceGroup = Constants.AUTHORITY_SERVICE_GROUP,serviceVersion = Constants.SERVICE_VERSION)
    private SelectUserInfoWebService selectUserInfoWebService;

    @Autowired
    private JwtAuthenticationEntryPoint entryPoint;

    @Autowired
    private CacheClient cacheClient;

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilter() throws Exception {
        JwtAuthenticationTokenFilter filter = new JwtAuthenticationTokenFilter();
        filter.setAuthenticationManager(authenticationManager());
        filter.setAuthenticationSuccessHandler(new AuthenticationSuccessHandlerImpl());
        filter.setAuthenticationFailureHandler(new AuthenticationFailureHandlerImpl());
        filter.setCacheClient(cacheClient);
        filter.setSelectUserInfoWebService(selectUserInfoWebService);
        return filter;
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable()
                .authorizeRequests().antMatchers("/auth/**").authenticated()
                .and()
                .exceptionHandling().authenticationEntryPoint(entryPoint)
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.addFilterBefore(authenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
        http.headers().cacheControl();

    }
}

问题出现了,AuthenticationSuccessHandlerImpl是
AbstractAuthenticationProcessingFilter成功后的执行方法,如下:

public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        System.out.println("Successfully Authentication");
    }
}

发现,每次都执行了两次
在这里插入图片描述
后来发现是@Bean注解导致的,继承了AbstractAuthenticationProcessingFilter的JwtAuthenticationTokenFilter本来就会被加载,加了bean注解,会被加载两次,删除后就好了。。。。
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值