【问题标题】:Java Spring Resource Server seems to check JWT token on responseJava Spring Resource Server 似乎在响应时检查 JWT 令牌
【发布时间】:2018-09-06 00:40:27
【问题描述】:

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

我的班级:

@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));

非常感谢您的帮助。

【问题讨论】:

  • 一个请求不会调用两次,因为日志来自不同的线程:[io-8080-exec-10][nio-8080-exec-1] 通常一个请求只绑定一个线程。
  • 可能是因为我们在发送响应之前使用“CompletableFuture”处理请求吗?要异步。所以处理请求的线程与处理响应的线程不同。
  • 谢谢,看来我们进展顺利:stackoverflow.com/questions/44680072/…

标签: java spring spring-boot spring-security jwt


【解决方案1】:

我已经明白你的问题是什么过程需要超过 30 秒,但是 JWT Token store的好处是Resource Server在RestAPI Consume调用Resource Endpoint的时候不需要去Authorization Server中签入,因为资源可以通过公钥修改自己。

通过 @EnableResourceServer 和 application.properties 中为 security.oauth2.resource.jwt.key-uri 提供的自动配置。

【讨论】:

  • 您能提供详细信息吗?你是什​​么意思反应超过30s?你从资源服务器调用另一个restapi?
  • 从接受请求到发送响应之间的时间。我的应用程序依赖于其他应用程序,我无法控制它们。我的应用程序是一个资源服务器,可以很好地检查请求上的 JWT 签名。但它似乎也在响应发送时检查它。
  • 如果请求的令牌在令牌过期之前从您的资源服务器获取并传递它应该没问题,因为在您尝试访问资源端点之前的 spring security oauth 过滤器链不是关于在其他 api 之后检查回复。因为你的令牌已经通过了。
  • 是的,确实,对于请求,它工作正常。但我说的是回应。忘记其他 API。事实是:如果响应是在令牌过期后发送的,Spring Security 会发送一个 401 HTTP 响应,即使我的应用程序会发送一个 200。这就是我认为 Spring Security 存在错误的原因。
【解决方案2】:

原因是 'filterChain' 为 REQUEST 类型(用于 HTTP 请求接收)调用一次,为 Async 类型(在 CompletableFuture 中用于响应过程的那个)调用一次。

要设置一个属性来避免异步链:

security.filter-dispatcher-types=REQUEST, ERROR

(见于https://github.com/spring-projects/spring-security-oauth/issues/736

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 2019-06-05
    • 2018-06-19
    • 2016-03-22
    • 1970-01-01
    • 2019-08-14
    • 2023-04-04
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多