Java Spring 资源服务器似乎在响应时检查 JWT token

标签 java spring spring-boot spring-security jwt

我有一个 @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/

相关文章:

java - Spring Boot 多模块 Gradle 构建 - 多个 "Fat Jars"

spring - 意外的 token (START_OBJECT),应为 VALUE_STRING : need JSON String that contains type id (for subtype of java. lang.Object)

java - 休息 通话不工作

java - 如何将 GitHub 存储库链接为 Maven 依赖项

Java Spring Boot - 如何像在 PHP Laravel 中一样为数据库播种

java - jsp 上无法访问 ModelAttributes

spring-boot - 将 Spring Boot Actuator 与 New Relic 集成

java - 我正在尝试打印 StringBuffer.toString() 但它不起作用

java - 小拼图 : why null pointer exception?

html - 存储在 "/images"目录中的静态内容未被提供,因为 URL 相对于“/css”目录