2017-02-13 402 views
1

我正在開發一個彈簧啓動應用程序,我使用彈簧安全來保護我的應用程序。我創建了一個自定義過濾器,並且我想在UsernamePasswordAuthenticationFilter之後添加它。我使用HttpSecurity.addFilterAfter方法來執行此操作。Spring Security addFilterAfter沒有註冊過濾器

但是,我的過濾器永遠不會被調用。請求你幫助我。代碼:

MultiSessionCustomLMSFilter.java同時呼籲過濾器在引導過程中

public class MultiSessionCustomLMSFilter extends GenericFilterBean { 

private final static Logger log = LoggerFactory.getLogger(MultiSessionCustomLMSFilter.class); 

@Autowired private UserLoginLogRepository userLoginLogRepository; 
private ObjectMapper mapper; 

@Override 
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
    log.debug("Inside doFilter of MultipleSessionFilter"); 
    //CUSTOM APP SPECIFIC LOGIC GOES IN HERE 
    } 
} 

WebSecurityConfig.java

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    public static final String JWT_TOKEN_HEADER_PARAM = "X-Authorization"; 
    public static final String FORM_BASED_LOGIN_ENTRY_POINT = "/api/auth/login"; 
    public static final String CSRF_ENTRY_POINT = "/api/auth/login/csrf"; 
    public static final String TOKEN_BASED_AUTH_ENTRY_POINT = "/api/**"; 
    public static final String TOKEN_REFRESH_ENTRY_POINT = "/api/auth/token"; 

    @Autowired private RestAuthenticationEntryPoint authenticationEntryPoint; 
    @Autowired private AuthenticationSuccessHandler successHandler; 
    @Autowired private AuthenticationFailureHandler failureHandler; 
    @Autowired private LoginAuthenticationProvider loginAuthenticationProvider; 
    @Autowired private JwtAuthenticationProvider jwtAuthenticationProvider; 

    @Autowired private TokenExtractor tokenExtractor; 

    @Autowired private AuthenticationManager authenticationManager; 

    @Autowired private ObjectMapper objectMapper; 

    @Autowired private JwtTokenFactory jwtTokenFactory; 

    protected LoginProcessingFilter buildAjaxLoginProcessingFilter() throws Exception { 
     LoginProcessingFilter filter = new LoginProcessingFilter(FORM_BASED_LOGIN_ENTRY_POINT, successHandler, failureHandler, objectMapper); 
     filter.setAuthenticationManager(this.authenticationManager); 
     return filter; 
    } 

    protected JwtTokenAuthenticationProcessingFilter buildJwtTokenAuthenticationProcessingFilter() throws Exception { 
     List<String> pathsToSkip = Arrays.asList(TOKEN_REFRESH_ENTRY_POINT,FORM_BASED_LOGIN_ENTRY_POINT, CSRF_ENTRY_POINT); 
     SkipPathRequestMatcher matcher = new SkipPathRequestMatcher(pathsToSkip, TOKEN_BASED_AUTH_ENTRY_POINT); 
     JwtTokenAuthenticationProcessingFilter filter = new JwtTokenAuthenticationProcessingFilter(failureHandler, tokenExtractor, matcher,objectMapper,jwtTokenFactory); 
     filter.setAuthenticationManager(this.authenticationManager); 
     return filter; 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) { 
     auth.authenticationProvider(loginAuthenticationProvider); 
     auth.authenticationProvider(jwtAuthenticationProvider); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable() 
     .exceptionHandling() 
     .authenticationEntryPoint(this.authenticationEntryPoint) 

     .and() 
      .sessionManagement() 
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 

     .and() 
      .authorizeRequests() 
       .antMatchers(TOKEN_REFRESH_ENTRY_POINT).permitAll() // Token refresh end-point 
       .antMatchers(CSRF_ENTRY_POINT).permitAll() 
//    .antMatchers(MIQA_FORUM_ENTRY_POINT).permitAll() 
     .and() 
      .authorizeRequests() 
       .antMatchers(TOKEN_BASED_AUTH_ENTRY_POINT).authenticated() // Protected API End-points 
     .and().cors().and() 
      .addFilterBefore(buildAjaxLoginProcessingFilter(), UsernamePasswordAuthenticationFilter.class) 
      .addFilterBefore(buildJwtTokenAuthenticationProcessingFilter(), UsernamePasswordAuthenticationFilter.class) 
      .addFilterAfter(new MultiSessionCustomLMSFilter(),UsernamePasswordAuthenticationFilter.class); 

    } 

應用程序日誌:

Creating filter chain: [email protected]1, 
[org.springframework.secu[email protected]a457c2b, 
org.spring[email protected]464aeb09, 
[email protected]7fd, 
[email protected], 
org.[email protected]c0c8f96, 
[email protected], 
com.egm[email protected]59f45950, 
[email protected], 
org.sp[email protected]59d6642a, 
org.springframework.[email protected]288728e, 
org.springfram[email protected]58164e9a, 
o[email protected]4aa22cc2, 
org[email protected]e01a26b, 
org.springfr[email protected]5c70d7f0] 

回答

0

LoginProcessingFilterJwtTokenAuthenticationProcessingFilter繼續過濾器鏈?

chain.doFilter(request, response); 

需要通過執行來繼續過濾器鏈前的過濾器

相關問題