【问题标题】:WebSecurity ignoring does not working with AbstractAuthenticationProcessingFilterWebSecurity 忽略不适用于 AbstractAuthenticationProcessingFilter
【发布时间】:2019-09-03 11:22:33
【问题描述】:

我创建了一个过滤器,在过滤器超级构造函数中它需要一个defaultFilterProcessesUrl。我通过 url /v1/** 选择了所有请求。

我需要去未登录/v1/usersPOST 方法)和/v1/users/signinPOST 方法),但过滤器不允许。如何解决这个问题?

JWT 过滤器:

public class JwtAuthenticationTokenFilter extends AbstractAuthenticationProcessingFilter {

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

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {

         String header = request.getHeader("Authorization");
         if(header == null || !header.startsWith("Bearer")){
             throw new RuntimeException("JWT token is missing");
         }

         String authenticationToken = header.substring(7);

        JwtAuthenticationToken token = new JwtAuthenticationToken(authenticationToken);
        return getAuthenticationManager().authenticate(token);
    }

    @Override
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
        super.successfulAuthentication(request, response, chain, authResult);
        chain.doFilter(request, response);
    }
}

Spring 安全配置:

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationProvider authenticationProvider;
    @Autowired
    private JwtAuthenticationEntryPoint entryPoint;

    @Bean
    public AuthenticationManager authenticationManager(){
        return new ProviderManager(Collections.singletonList(authenticationProvider));
    }

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilter(){
        JwtAuthenticationTokenFilter filter = new JwtAuthenticationTokenFilter();
        filter.setAuthenticationManager(authenticationManager());
        filter.setAuthenticationSuccessHandler(new JwtSuccessHandler());
        return filter;
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
            .ignoring()
                .antMatchers(HttpMethod.POST, "/v1/users")
                .antMatchers(HttpMethod.POST, "/v1/users/signin")
                .antMatchers(HttpMethod.POST, "/token");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http
             .csrf().disable()
             .authorizeRequests()
                .anyRequest().hasAnyRole("SG_ADMIN", "O_ADMIN", "OS_ADMIN")
                .and()
             .exceptionHandling().authenticationEntryPoint(entryPoint)
                .and()
             .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

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

【问题讨论】:

    标签: java spring spring-boot spring-security jwt


    【解决方案1】:

    您可以使用带有RequestMatcher 的其他构造函数,请参阅AbstractAuthenticationProcessingFilter

    AbstractAuthenticationProcessingFilter

    protected AbstractAuthenticationProcessingFilter(RequestMatcher requiresAuthenticationRequestMatcher)

    创建一个新实例

    参数:

    requiresAuthenticationRequestMatcher - RequestMatcher 用于确定是否需要身份验证。不能为空。

    您修改后的代码:

    public JwtAuthenticationTokenFilter() {
        super(customRequestMatcher);
    }
    

     RequestMatcher customRequestMatcher = new AndRequestMatcher(
          new AntPathRequestMatcher("/v1/**"), 
          new NegatedRequestMatcher(
              new AntPathRequestMatcher("/v1/users", "POST")
          ),
          new NegatedRequestMatcher(
              new AntPathRequestMatcher("/v1/users/signin", "POST")
          )
     );
    

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2020-10-21
      • 2012-11-24
      • 2011-03-19
      • 2021-04-02
      相关资源
      最近更新 更多