首页 > 解决方案 > JwtAuthorizationFilter 实现不起作用

问题描述

总之,JwtAuthorizationFilter 不起作用。

如果我离开 //filterChain.doFilter(request, response) 注释掉我正确地得到一个 200 但主体是空的,这意味着两件事:1)来自控制器的控制器/响应被执行但不是它的逻辑。2) 函数 getAuthentication() 正确读取 Claims/token

如果我取消注释行 //filterChain.doFilter(request, response) 因为我得到 405,就会出现问题。应该取消注释该行,过滤器链才能完全执行并获得包含内容的响应正文。

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,    
 FilterChain filterChain)
        throws IOException, ServletException {
    UsernamePasswordAuthenticationToken authentication = getAuthentication(request);
    if (authentication == null) {
        filterChain.doFilter(request, response);
        return;
    }

    SecurityContextHolder.getContext().setAuthentication(authentication);
    **//filterChain.doFilter(request, response);**
}

Function in the controller:
@GetMapping("/foo")
@ResponseStatus(code = HttpStatus.OK)
public MyObject retrieve(@RequestBody MyModel obj) {
    //code here is never called
}

For reference, this is my security config:
@Override
protected void configure(final HttpSecurity http) throws Exception {

    http.cors().and().csrf().disable().authorizeRequests()
        .antMatchers(HttpMethod.POST, "/bar").permitAll()
        .anyRequest().authenticated().and().addFilter(new AnotherFilter(authenticationManager()))
        .addFilter(new JwtAuthorizationFilter(authenticationManager()))
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

}

What Am I doing wrong?

标签: springspring-data

解决方案


只是说我清理了构建,重新启动 IDE 并工作。也许有一些缓存不允许 JWT 过滤器正常工作。谢谢,


推荐阅读