java - Spring 中的资源服务器是否会在每个请求时调用授权服务器(Spring)?

标签 java spring-boot authorization-server

我是 Spring Boot 新手,正在开发一个具有很少端点的简单 API。现在我正在使用我的端点实现授权服务器(Oauth2AuthorizationServer)和资源服务器。我注意到,当 AuthServer 使用提供的凭据发出一次 JWT token 时(我正在使用凭据流),即使 AuthServer 停止,资源服务器也能够验证 JWT。这个魔法是如何发生的?我一开始就想AuthServer必须对向资源服务器发出的每个请求进行授权。即使身份验证服务器已关闭,我也无法弄清楚资源服务器如何能够解码 JWT。请有人向我解释一下事情到底是如何运作的。

当我尝试向资源服务器发出第一个请求并且 AuthServer 停止时,出现错误 - 没有带解码器()的 @Bean。

最佳答案

Spring Security 支持使用两种形式的 OAuth 2.0 承载 token 来保护端点:

  • 智威汤逊
  • 不透明 token

您遇到的行为意味着您通过使用颁发者 URI 指定授权服务器来配置 JWT 身份验证,例如:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: https://idp.example.com/issuer

根据Spring documentation :

Resource Server will use this property to further self-configure, discover the authorization server’s public keys, and subsequently validate incoming JWTs.

它通过以下步骤进行自身配置:

  1. Query the Provider Configuration or Authorization Server Metadata endpoint for the jwks_url property
  2. Query the jwks_url endpoint for supported algorithms
  3. Configure the validation strategy to query jwks_url for valid public keys of the algorithms found
  4. Configure the validation strategy to validate each JWTs iss claim against idp.example.com.

文档中回答您的一个问题的重要说明是:

If the authorization server is down when Resource Server queries it (given appropriate timeouts), then startup will fail.

在运行时,任何包含 Authorization: Bearer header 的请求都会按如下方式处理:

  1. Validate its signature against a public key obtained from the jwks_url endpoint during startup and matched against the JWT

  2. Validate the JWT’s exp and nbf timestamps and the JWT’s iss claim, and

  3. Map each scope to an authority with the prefix SCOPE_.

在此过程中资源服务器和授权服务器之间没有通信,因此启动后不必启动。


您一开始所期望的行为实际上与不透明 token 身份验证有关。其中授权服务器指定了不同的属性,根据Spring documentation :

spring:
  security:
    oauth2:
      resourceserver:
        opaque-token:
          introspection-uri: https://idp.example.com/introspect
          client-id: client
          client-secret: secret

This startup process is quite a bit simpler than for JWTs since no endpoints need to be discovered and no additional validation rules get added.

在运行时,它还会处理任何包含 Authorization: Bearer header 的请求:

  1. Query the provided introspection endpoint using the provided credentials and the token

  2. Inspect the response for an { 'active': true } attribute

  3. Map each scope to an authority with the prefix SCOPE_

这意味着 token 仅在授权服务器响应时才有效。

关于java - Spring 中的资源服务器是否会在每个请求时调用授权服务器(Spring)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75309899/

相关文章:

java - 将对象添加到 ArrayList 时出现问题

java - 如何不从 Java 应用程序输出到 stderr?

java - Server和Client的InputStream

java - 带参数的 REST API 查询

java - 如何使用 Spring JDBCTemplate 将 Blob 数据读入 String 对象

java - 使用spring RestTemplate发送post请求时出现异常

spring-security - 使用 spring-authorization-server 时将 token 存储在 redis 中

java - 创建 jar 文件的不同方法?