我是 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
Resource Server will use this property to further self-configure, discover the authorization server’s public keys, and subsequently validate incoming JWTs.
它通过以下步骤进行自身配置:
- Query the Provider Configuration or Authorization Server Metadata endpoint for the
jwks_url
property- Query the
jwks_url
endpoint for supported algorithms- Configure the validation strategy to query
jwks_url
for valid public keys of the algorithms found- Configure the validation strategy to validate each JWTs
iss
claim againstidp.example.com
.
文档中回答您的一个问题的重要说明是:
If the authorization server is down when Resource Server queries it (given appropriate timeouts), then startup will fail.
在运行时,任何包含 Authorization: Bearer
header 的请求都会按如下方式处理:
Validate its signature against a public key obtained from the
jwks_url
endpoint during startup and matched against the JWTValidate the JWT’s
exp
andnbf
timestamps and the JWT’siss
claim, andMap 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 的请求:
Query the provided introspection endpoint using the provided credentials and the token
Inspect the response for an
{ 'active': true }
attributeMap each scope to an authority with the prefix
SCOPE_
这意味着 token 仅在授权服务器响应时才有效。
关于java - Spring 中的资源服务器是否会在每个请求时调用授权服务器(Spring)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75309899/