记一次继承了AbstractAuthenticationProcessingFilter 的过滤器被执行了两次问题
生活随笔
收集整理的這篇文章主要介紹了
记一次继承了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/**");}@Overridepublic 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;@Autowiredprivate JwtAuthenticationEntryPoint entryPoint;@Autowiredprivate CacheClient cacheClient;@Beanpublic 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;}@Overrideprotected 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成功后的執行方法,如下:
發現,每次都執行了兩次
后來發現是@Bean注解導致的,繼承了AbstractAuthenticationProcessingFilter的JwtAuthenticationTokenFilter本來就會被加載,加了bean注解,會被加載兩次,刪除后就好了。。。。
總結
以上是生活随笔為你收集整理的记一次继承了AbstractAuthenticationProcessingFilter 的过滤器被执行了两次问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac iterm2 终端优化
- 下一篇: elasticsearch常见错误解决办