spring - 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?
解决方案
只是说我清理了构建,重新启动 IDE 并工作。也许有一些缓存不允许 JWT 过滤器正常工作。谢谢,
推荐阅读
- python - ValueError:检查目标时出错:预期dense_4的形状为(1,)但得到的数组形状为(6,)
- neo4j - 在 neo4j 查询中跳过反向路径
- java - Android分页库索引超出范围异常
- python - 如何选择和打印字典和列表中常见的某些值?
- svelte - 我可以让 Sapper 的预加载函数只等待一个承诺而不需要返回该承诺的结果吗?
- java - 发生异常时手动回滚
- python - pycharm python项目中的无效符号链接错误
- java - Sorted array to array consists of balanced BST
- angular - Uploading image to spring rest service using Angular
- amazon-web-services - How to increase the vm.max_map_count in AWS ECS fargate?