【问题标题】:Spring Security Filter is not being triggered by configurationSpring Security Filter 没有被配置触发
【发布时间】:2017-05-11 09:48:33
【问题描述】:

我正在配置我的 REST 的安全性,但我不知道如何保护我的方法,但是允许触发过滤器来设置我的权限

    http
    .authorizeRequests()
    .antMatchers(PERSISTENCE_SERVICE_URL)
    .hasAuthority(AUTHORITY_PERSISTENCE_SERVICE)
    .and()
    .csrf()
    .disable();

在我的扩展 OncePerRequestFilter 的过滤器中做了这样的事情

  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
this.authenticationImpl.init();
  String jwt = request.getHeader("jwt");
  String refresh = request.getHeader("refresh");
  if(jwt != null) {
   this.jwtPropertyExtractor.commitJwt(jwt, refresh);
   String jwtId = this.jwtPropertyExtractor.getIdentityId();
   String securityRole = this.jwtPropertyExtractor.getSecurityRole();
   this.authenticationImpl.setIdentityId(jwtId);
   this.authenticationImpl.updateSecurityRole(securityRole);
  SecurityContextHolder.getContext().setAuthentication(this.authenticationImpl);
}

filterChain.doFilter(request, response);  }

所以当我将.hasAuthority(AUTHORITY_PERSISTENCE_SERVICE) 放入我的配置时,我的过滤器甚至没有被触发,但我需要他设置我的身份验证。

【问题讨论】:

  • 您的配置显示您尚未注册自定义过滤器

标签: spring spring-security


【解决方案1】:

好的,我添加了

        .addFilterBefore(jwtAuthorisationFilter(),
            BasicAuthenticationFilter.class);
  }

  @Bean
  public JwtAuthorisationFilter jwtAuthorisationFilter() {
    return new JwtAuthorisationFilter(jwtExtractor(),
        authenticationImpl());
  }

现在它的工作正常,感谢那个提示:>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 2014-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2011-01-08
    相关资源
    最近更新 更多