我有一个 @EnableResourceServer,它根据请求检查 JWT。 这个 JWT 有一个 30 秒的 TTL,它被我的 REST API 接受。 但有时在发送响应之前该过程需要 30 多秒。
即使我的应用程序返回 200 响应代码,Spring 也会将其替换为 401,这意味着我的 JWT 已过期。
我认为这是一种不良行为,不应在响应时检查 JWT。 有人遇到过同样的问题并且知道如何避免吗?
我使用 Spring boot 1.5.10.RELEASE、spring-security-oauth2 2.2.1.RELEASE、spring-security-jwt 1.0.9.RELEASE。
我的类(class):
@EnableResourceServer
@Configuration
@Profile("security")
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.requestMatchers().and().authorizeRequests().antMatchers("/pay/**").authenticated();
}
}
我正在使用该属性:
security.oauth2.resource.jwt.key-uri=
这里有一个日志证明调用了两次:
2018-03-27T15:07:48.801-04:00 [APP/PROC/WEB/0] [OUT] 2018-03-27 19:07:48.800 DEBUG [accueil-transfert,c0baad95368697cd,c0baad95368697cd,true] 15 --- [io-8080-exec-10] p.a.OAuth2AuthenticationProcessingFilter : Authentication success: org.springframework.security.oauth2.provider.OAuth2Authentication@822a1a7a: Principal: null; Credentials: [PROTECTED]; Authenticated: true; Details: remoteAddress=xxx.xxx.xxx.xxx, tokenType=BearertokenValue=<TOKEN>; Not granted any authorities
...
2018-03-27T15:08:39.130-04:00 [APP/PROC/WEB/0] [OUT] 2018-03-27 19:08:39.129 DEBUG [accueil-transfert,c0baad95368697cd,c0baad95368697cd,true] 15 --- [nio-8080-exec-1] p.a.OAuth2AuthenticationProcessingFilter : Authentication request failed: error="invalid_token", error_description="Access token expired: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiAiUElTR...
请求存储在我的 RestController 中的 completableFuture 中:
@PostMapping("/myUrl")
public CompletableFuture<ResponseEntity<CorpsReponseDto>> effectue(@RequestHeader HttpHeaders enteteRequete,
@Valid @RequestBody CorpsRequeteDto corpsRequete, BindingResult bindingResult) {
CompletableFuture<ResponseEntity<CorpsReponseDto>> future = new CompletableFuture<>();
...
return future;
}
并在 @Service 类中完成:
future.complete(new ResponseEntity<>(reponseDto, httpStatus));
非常感谢您的帮助。
最佳答案
原因是 'filterChain' 被调用一次用于 REQUEST 类型(用于 HTTP 请求接收)和一次用于异步类型(用于 CompletableFuture 的响应过程)。
要设置一个属性来避免异步链:
security.filter-dispatcher-types=REQUEST, ERROR
(在 https://github.com/spring-projects/spring-security-oauth/issues/736 上看到)
关于Java Spring 资源服务器似乎在响应时检查 JWT token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49515690/