日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/30 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记一次继承了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成功后的執行方法,如下:

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

發現,每次都執行了兩次

后來發現是@Bean注解導致的,繼承了AbstractAuthenticationProcessingFilter的JwtAuthenticationTokenFilter本來就會被加載,加了bean注解,會被加載兩次,刪除后就好了。。。。

總結

以上是生活随笔為你收集整理的记一次继承了AbstractAuthenticationProcessingFilter 的过滤器被执行了两次问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。