注册客户端
A 是向授权服务器注册的客户端的表示形式。
客户端必须先向授权服务器注册,然后才能启动授权授权流,例如 或 。RegisteredClientauthorization_codeclient_credentials
在客户端注册期间,将为客户端分配唯一的客户端标识符、(可选)客户端密码(取决于客户端类型)以及与其唯一客户端标识符关联的元数据。 客户端的元数据范围可以从面向人类的显示字符串(例如客户端名称)到特定于协议流的项(例如有效重定向 URI 的列表)。
| Spring Security 的 OAuth2 客户端支持中对应的客户端注册模型是 ClientRegistration。 |
客户端的主要用途是请求访问受保护的资源。 客户端首先通过向授权服务器进行身份验证并出示授权授予来请求访问令牌。 授权服务器对客户端和授权进行身份验证,如果它们有效,则颁发访问令牌。 客户端现在可以通过提供访问令牌从资源服务器请求受保护的资源。
以下示例演示如何配置允许执行authorization_code授权流以请求访问令牌:RegisteredClient
RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
.clientId("client-a")
.clientSecret("{noop}secret") (1)
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.redirectUri("http://127.0.0.1:8080/authorized")
.scope("scope-a")
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
.build();
| 1 | {noop}表示 Spring Security 的 NoOpPasswordEncoder 的 ID。PasswordEncoder |
Spring Security 的 OAuth2 客户端支持中的相应配置为:
spring:
security:
oauth2:
client:
registration:
client-a:
provider: spring
client-id: client-a
client-secret: secret
authorization-grant-type: authorization_code
redirect-uri: "http://127.0.0.1:8080/authorized"
scope: scope-a
provider:
spring:
issuer-uri: http://localhost:9000
A 具有与其唯一客户端标识符关联的元数据(属性),定义如下:RegisteredClient
public class RegisteredClient implements Serializable {
private String id; (1)
private String clientId; (2)
private Instant clientIdIssuedAt; (3)
private String clientSecret; (4)
private Instant clientSecretExpiresAt; (5)
private String clientName; (6)
private Set<ClientAuthenticationMethod> clientAuthenticationMethods; (7)
private Set<AuthorizationGrantType> authorizationGrantTypes; (8)
private Set<String> redirectUris; (9)
private Set<String> postLogoutRedirectUris; (10)
private Set<String> scopes; (11)
private ClientSettings clientSettings; (12)
private TokenSettings tokenSettings; (13)
...
}
| 1 | id:唯一标识 .RegisteredClient |
| 2 | clientId:客户端标识符。 |
| 3 | clientIdIssuedAt:颁发客户端标识符的时间。 |
| 4 | clientSecret:客户的秘密。该值应使用 Spring Security 的 PasswordEncoder 进行编码。 |
| 5 | clientSecretExpiresAt:客户端密钥过期的时间。 |
| 6 | clientName:用于客户端的描述性名称。该名称可能在某些情况下使用,例如在同意页面中显示客户端名称时。 |
| 7 | clientAuthenticationMethods:客户端可能使用的身份验证方法。支持的值为 、 、 private_key_jwt 和 (公共客户端)。client_secret_basicclient_secret_postclient_secret_jwtnone |
| 8 | authorizationGrantTypes:客户端可以使用的授权类型。支持的值为 、 、 、 和 。authorization_codeclient_credentialsrefresh_tokenurn:ietf:params:oauth:grant-type:device_codeurn:ietf:params:oauth:grant-type:token-exchange |
| 9 | redirectUris:客户端可以在基于重定向的流中使用的已注册重定向 URI,例如 grant。authorization_code |
| 10 | postLogoutRedirectUris:客户端可用于注销的注销后重定向 URI。 |
| 11 | scopes:允许客户端请求的范围。 |
| 12 | clientSettings:客户端的自定义设置 - 例如,需要 PKCE、需要授权同意等。 |
| 13 | tokenSettings:颁发给客户端的 OAuth2 令牌的自定义设置,例如,访问/刷新令牌生存时间、重用刷新令牌等。 |
| Spring Security 的 OAuth2 客户端支持中对应的客户端注册模型是 ClientRegistration。 |
| 1 | {noop}表示 Spring Security 的 NoOpPasswordEncoder 的 ID。PasswordEncoder |
| 1 | id:唯一标识 .RegisteredClient |
| 2 | clientId:客户端标识符。 |
| 3 | clientIdIssuedAt:颁发客户端标识符的时间。 |
| 4 | clientSecret:客户的秘密。该值应使用 Spring Security 的 PasswordEncoder 进行编码。 |
| 5 | clientSecretExpiresAt:客户端密钥过期的时间。 |
| 6 | clientName:用于客户端的描述性名称。该名称可能在某些情况下使用,例如在同意页面中显示客户端名称时。 |
| 7 | clientAuthenticationMethods:客户端可能使用的身份验证方法。支持的值为 、 、 private_key_jwt 和 (公共客户端)。client_secret_basicclient_secret_postclient_secret_jwtnone |
| 8 | authorizationGrantTypes:客户端可以使用的授权类型。支持的值为 、 、 、 和 。authorization_codeclient_credentialsrefresh_tokenurn:ietf:params:oauth:grant-type:device_codeurn:ietf:params:oauth:grant-type:token-exchange |
| 9 | redirectUris:客户端可以在基于重定向的流中使用的已注册重定向 URI,例如 grant。authorization_code |
| 10 | postLogoutRedirectUris:客户端可用于注销的注销后重定向 URI。 |
| 11 | scopes:允许客户端请求的范围。 |
| 12 | clientSettings:客户端的自定义设置 - 例如,需要 PKCE、需要授权同意等。 |
| 13 | tokenSettings:颁发给客户端的 OAuth2 令牌的自定义设置,例如,访问/刷新令牌生存时间、重用刷新令牌等。 |
RegisteredClientRepository
是可以注册新客户端和查询现有客户端的中心组件。
在遵循特定协议流(如客户端身份验证、授权授权处理、令牌自检、动态客户端注册等)时,其他组件会使用它。RegisteredClientRepository
提供的 are 和 的实现。
该实现将实例存储在内存中,建议仅在开发和测试期间使用。 是一个 JDBC 实现,它通过使用 来持久化实例。RegisteredClientRepositoryInMemoryRegisteredClientRepositoryJdbcRegisteredClientRepositoryInMemoryRegisteredClientRepositoryRegisteredClientJdbcRegisteredClientRepositoryRegisteredClientJdbcOperations
是必需的组件。RegisteredClientRepository |
以下示例演示如何注册:RegisteredClientRepository@Bean
@Bean
public RegisteredClientRepository registeredClientRepository() {
List<RegisteredClient> registrations = ...
return new InMemoryRegisteredClientRepository(registrations);
}
或者,可以通过 OAuth2AuthorizationServerConfigurer 进行配置:RegisteredClientRepository
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.registeredClientRepository(registeredClientRepository);
...
return http.build();
}
同时应用多个配置选项时很有用。OAuth2AuthorizationServerConfigurer |
是必需的组件。RegisteredClientRepository |
同时应用多个配置选项时很有用。OAuth2AuthorizationServerConfigurer |
OAuth2授权
An 是 OAuth2 授权的表示形式,它保存与资源所有者授予客户端的授权相关的状态,或者在授权授予类型的情况下保持其自身。OAuth2Authorizationclient_credentials
| Spring Security 的 OAuth2 客户端支持中的相应授权模型是 OAuth2AuthorizedClient。 |
成功完成授权授权流后,将创建一个 OAuth2AccessToken、一个(可选)OAuth2RefreshToken 以及特定于已执行授权类型的其他状态。OAuth2Authorization
与授权授权类型关联的 OAuth2Token 实例会有所不同。OAuth2Authorization
对于 OAuth2 authorization_code授权,将关联 、 和 (可选)。OAuth2AuthorizationCodeOAuth2AccessTokenOAuth2RefreshToken
对于 OpenID Connect 1.0 authorization_code 授予,将关联一个 、一个 OidcIdToken、一个 和 一个(可选)。OAuth2AuthorizationCodeOAuth2AccessTokenOAuth2RefreshToken
对于 OAuth2 client_credentials 授权,仅关联 an。OAuth2AccessToken
OAuth2Authorization其属性定义如下:
public class OAuth2Authorization implements Serializable {
private String id; (1)
private String registeredClientId; (2)
private String principalName; (3)
private AuthorizationGrantType authorizationGrantType; (4)
private Set<String> authorizedScopes; (5)
private Map<Class<? extends OAuth2Token>, Token<?>> tokens; (6)
private Map<String, Object> attributes; (7)
...
}
| 1 | id:唯一标识 .OAuth2Authorization |
| 2 | registeredClientId:唯一标识 RegisteredClient 的 ID。 |
| 3 | principalName:资源所有者(或客户端)的主体名称。 |
| 4 | authorizationGrantType:使用过的。AuthorizationGrantType |
| 5 | authorizedScopes:为客户端授权的范围。Set |
| 6 | tokens:特定于已执行授权授权类型的实例(和关联的元数据)。OAuth2Token |
| 7 | attributes:特定于已执行授权类型的其他属性,例如,经过身份验证的 、 等。PrincipalOAuth2AuthorizationRequest |
OAuth2Authorization其关联的实例具有固定的生命周期。
新颁发的债券处于活动状态,当它过期或失效(撤销)时将变为非活动状态。
当所有关联的实例都处于非活动状态时,(隐式)处于非活动状态。
每个都保存在 中,它提供 、 和 的访问器。OAuth2TokenOAuth2TokenOAuth2AuthorizationOAuth2TokenOAuth2TokenOAuth2Authorization.TokenisExpired()isInvalidated()isActive()
OAuth2Authorization.Token还提供 ,它返回与 关联的声明(如果有)。getClaims()OAuth2Token
| Spring Security 的 OAuth2 客户端支持中的相应授权模型是 OAuth2AuthorizedClient。 |
| 1 | id:唯一标识 .OAuth2Authorization |
| 2 | registeredClientId:唯一标识 RegisteredClient 的 ID。 |
| 3 | principalName:资源所有者(或客户端)的主体名称。 |
| 4 | authorizationGrantType:使用过的。AuthorizationGrantType |
| 5 | authorizedScopes:为客户端授权的范围。Set |
| 6 | tokens:特定于已执行授权授权类型的实例(和关联的元数据)。OAuth2Token |
| 7 | attributes:特定于已执行授权类型的其他属性,例如,经过身份验证的 、 等。PrincipalOAuth2AuthorizationRequest |
OAuth2AuthorizationService
是存储新授权和查询现有授权的中心组件。
在遵循特定协议流时,其他组件会使用它,例如,客户端身份验证、授权授权处理、令牌自省、令牌吊销、动态客户端注册等。OAuth2AuthorizationService
提供的 are 和 的实现。
该实现将实例存储在内存中,建议仅在开发和测试期间使用。 是一个 JDBC 实现,它通过使用 来持久化实例。OAuth2AuthorizationServiceInMemoryOAuth2AuthorizationServiceJdbcOAuth2AuthorizationServiceInMemoryOAuth2AuthorizationServiceOAuth2AuthorizationJdbcOAuth2AuthorizationServiceOAuth2AuthorizationJdbcOperations
这是一个可选组件,默认为 。OAuth2AuthorizationServiceInMemoryOAuth2AuthorizationService |
以下示例演示如何注册:OAuth2AuthorizationService@Bean
@Bean
public OAuth2AuthorizationService authorizationService() {
return new InMemoryOAuth2AuthorizationService();
}
或者,可以通过 OAuth2AuthorizationServerConfigurer 进行配置:OAuth2AuthorizationService
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.authorizationService(authorizationService);
...
return http.build();
}
同时应用多个配置选项时很有用。OAuth2AuthorizationServerConfigurer |
这是一个可选组件,默认为 。OAuth2AuthorizationServiceInMemoryOAuth2AuthorizationService |
同时应用多个配置选项时很有用。OAuth2AuthorizationServerConfigurer |
OAuth2AuthorizationConsent
An 表示来自 OAuth2 授权请求流的授权“同意”(决定),例如,授予,它持有资源所有者授予客户端的权限。OAuth2AuthorizationConsentauthorization_code
在授权访问客户端时,资源所有者只能授予客户端请求的权限的子集。
典型的用例是授权流,其中客户端请求范围,资源所有者授予(或拒绝)对所请求范围的访问权限。authorization_code
完成 OAuth2 授权请求流后,将创建(或更新)一个,并将授予的权限与客户端和资源所有者相关联。OAuth2AuthorizationConsent
OAuth2AuthorizationConsent其属性定义如下:
public final class OAuth2AuthorizationConsent implements Serializable {
private final String registeredClientId; (1)
private final String principalName; (2)
private final Set<GrantedAuthority> authorities; (3)
...
}
| 1 | registeredClientId:唯一标识 RegisteredClient 的 ID。 |
| 2 | principalName:资源所有者的主体名称。 |
| 3 | authorities:资源所有者授予客户端的权限。颁发机构可以表示范围、声明、权限、角色等。 |
| 1 | registeredClientId:唯一标识 RegisteredClient 的 ID。 |
| 2 | principalName:资源所有者的主体名称。 |
| 3 | authorities:资源所有者授予客户端的权限。颁发机构可以表示范围、声明、权限、角色等。 |
OAuth2AuthorizationConsentService
是存储新授权同意和查询现有授权同意的核心组件。
它主要由实现 OAuth2 授权请求流的组件使用,例如授权。OAuth2AuthorizationConsentServiceauthorization_code
提供的 are 和 的实现。
该实现将实例存储在内存中,建议仅用于开发和测试。 是一个 JDBC 实现,它通过使用 来持久化实例。OAuth2AuthorizationConsentServiceInMemoryOAuth2AuthorizationConsentServiceJdbcOAuth2AuthorizationConsentServiceInMemoryOAuth2AuthorizationConsentServiceOAuth2AuthorizationConsentJdbcOAuth2AuthorizationConsentServiceOAuth2AuthorizationConsentJdbcOperations
这是一个可选组件,默认为 。OAuth2AuthorizationConsentServiceInMemoryOAuth2AuthorizationConsentService |
以下示例演示如何注册:OAuth2AuthorizationConsentService@Bean
@Bean
public OAuth2AuthorizationConsentService authorizationConsentService() {
return new InMemoryOAuth2AuthorizationConsentService();
}
或者,可以通过 OAuth2AuthorizationServerConfigurer 进行配置:OAuth2AuthorizationConsentService
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.authorizationConsentService(authorizationConsentService);
...
return http.build();
}
同时应用多个配置选项时很有用。OAuth2AuthorizationServerConfigurer |
这是一个可选组件,默认为 。OAuth2AuthorizationConsentServiceInMemoryOAuth2AuthorizationConsentService |
同时应用多个配置选项时很有用。OAuth2AuthorizationServerConfigurer |
OAuth2TokenContext
An 是一个上下文对象,它保存与 OAuth2TokenGenerator 和 OAuth2TokenCustomizer 关联并由 OAuth2TokenCustomizer 使用的信息。OAuth2TokenContextOAuth2Token
OAuth2TokenContext提供以下访问器:
public interface OAuth2TokenContext extends Context {
default RegisteredClient getRegisteredClient() ... (1)
default <T extends Authentication> T getPrincipal() ... (2)
default AuthorizationServerContext getAuthorizationServerContext() ... (3)
@Nullable
default OAuth2Authorization getAuthorization() ... (4)
default Set<String> getAuthorizedScopes() ... (5)
default OAuth2TokenType getTokenType() ... (6)
default AuthorizationGrantType getAuthorizationGrantType() ... (7)
default <T extends Authentication> T getAuthorizationGrant() ... (8)
...
}
| 1 | getRegisteredClient():与授权授权关联的 RegisteredClient。 |
| 2 | getPrincipal():资源所有者(或客户端)的实例。Authentication |
| 3 | getAuthorizationServerContext():保存授权服务器运行时环境信息的 AuthorizationServerContext 对象。 |
| 4 | getAuthorization():与授权授权关联的 OAuth2Authorization。 |
| 5 | getAuthorizedScopes():为客户端授权的范围。 |
| 6 | getTokenType():要生成的。支持的值为 、 、 和 。OAuth2TokenTypecodeaccess_tokenrefresh_tokenid_token |
| 7 | getAuthorizationGrantType():与授权授权关联的。AuthorizationGrantType |
| 8 | getAuthorizationGrant():处理授权授予的实例。AuthenticationAuthenticationProvider |
| 1 | getRegisteredClient():与授权授权关联的 RegisteredClient。 |
| 2 | getPrincipal():资源所有者(或客户端)的实例。Authentication |
| 3 | getAuthorizationServerContext():保存授权服务器运行时环境信息的 AuthorizationServerContext 对象。 |
| 4 | getAuthorization():与授权授权关联的 OAuth2Authorization。 |
| 5 | getAuthorizedScopes():为客户端授权的范围。 |
| 6 | getTokenType():要生成的。支持的值为 、 、 和 。OAuth2TokenTypecodeaccess_tokenrefresh_tokenid_token |
| 7 | getAuthorizationGrantType():与授权授权关联的。AuthorizationGrantType |
| 8 | getAuthorizationGrant():处理授权授予的实例。AuthenticationAuthenticationProvider |
OAuth2TokenGenerator(OAuth2令牌生成器)
An 负责从提供的 OAuth2TokenContext 中包含的信息生成 an。OAuth2TokenGeneratorOAuth2Token
生成的 主要取决于 中指定的类型。OAuth2TokenOAuth2TokenTypeOAuth2TokenContext
例如,当 for 为:valueOAuth2TokenType
-
code,然后生成。OAuth2AuthorizationCode -
access_token,然后生成。OAuth2AccessToken -
refresh_token,然后生成。OAuth2RefreshToken -
id_token,然后生成。OidcIdToken
此外,生成的格式会有所不同,具体取决于为 RegisteredClient 配置的格式。
如果格式为(默认值),则生成 a。
如果格式为 ,则生成一个“不透明”标记。OAuth2AccessTokenTokenSettings.getAccessTokenFormat()OAuth2TokenFormat.SELF_CONTAINEDJwtOAuth2TokenFormat.REFERENCE
最后,如果生成的声明和实现有一组,则可以从 OAuth2Authorization.Token.getClaims() 访问这些声明。OAuth2TokenClaimAccessor
主要由实现授权授权处理的组件使用,例如 、 和 。OAuth2TokenGeneratorauthorization_codeclient_credentialsrefresh_token
提供的实现是 、 和 。
生成一个“不透明”() 访问令牌,并生成一个 ()。OAuth2AccessTokenGeneratorOAuth2RefreshTokenGeneratorJwtGeneratorOAuth2AccessTokenGeneratorOAuth2TokenFormat.REFERENCEJwtGeneratorJwtOAuth2TokenFormat.SELF_CONTAINED
是一个 OPTIONAL 组件,默认由 和 组成。OAuth2TokenGeneratorDelegatingOAuth2TokenGeneratorOAuth2AccessTokenGeneratorOAuth2RefreshTokenGenerator |
如果 a 是注册的,则 a 另外组成在 .JwtEncoder@BeanJWKSource<SecurityContext>@BeanJwtGeneratorDelegatingOAuth2TokenGenerator |
它提供了极大的灵活性,因为它可以支持 和 的任何自定义令牌格式。OAuth2TokenGeneratoraccess_tokenrefresh_token
以下示例演示如何注册:OAuth2TokenGenerator@Bean
@Bean
public OAuth2TokenGenerator<?> tokenGenerator() {
JwtEncoder jwtEncoder = ...
JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
return new DelegatingOAuth2TokenGenerator(
jwtGenerator, accessTokenGenerator, refreshTokenGenerator);
}
或者,可以通过 OAuth2AuthorizationServerConfigurer 进行配置:OAuth2TokenGenerator
@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
new OAuth2AuthorizationServerConfigurer();
http.apply(authorizationServerConfigurer);
authorizationServerConfigurer
.tokenGenerator(tokenGenerator);
...
return http.build();
}
同时应用多个配置选项时很有用。OAuth2AuthorizationServerConfigurer |
是一个 OPTIONAL 组件,默认由 和 组成。OAuth2TokenGeneratorDelegatingOAuth2TokenGeneratorOAuth2AccessTokenGeneratorOAuth2RefreshTokenGenerator |
如果 a 是注册的,则 a 另外组成在 .JwtEncoder@BeanJWKSource<SecurityContext>@BeanJwtGeneratorDelegatingOAuth2TokenGenerator |
同时应用多个配置选项时很有用。OAuth2AuthorizationServerConfigurer |
OAuth2TokenCustomizer
An 提供了自定义 的属性的功能,这些属性可在提供的 OAuth2TokenContext 中访问。
OAuth2TokenGenerator 使用它来让它在生成之前自定义 的属性。OAuth2TokenCustomizerOAuth2TokenOAuth2Token
使用泛型类型 () 声明的提供了自定义“不透明”声明的能力。 提供对 的访问,允许添加、替换和删除声明。OAuth2TokenCustomizer<OAuth2TokenClaimsContext>OAuth2TokenClaimsContextimplements OAuth2TokenContextOAuth2AccessTokenOAuth2TokenClaimsContext.getClaims()OAuth2TokenClaimsSet.Builder
以下示例演示如何实现 an 并使用 :OAuth2TokenCustomizer<OAuth2TokenClaimsContext>OAuth2AccessTokenGenerator
@Bean
public OAuth2TokenGenerator<?> tokenGenerator() {
JwtEncoder jwtEncoder = ...
JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
accessTokenGenerator.setAccessTokenCustomizer(accessTokenCustomizer());
OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
return new DelegatingOAuth2TokenGenerator(
jwtGenerator, accessTokenGenerator, refreshTokenGenerator);
}
@Bean
public OAuth2TokenCustomizer<OAuth2TokenClaimsContext> accessTokenCustomizer() {
return context -> {
OAuth2TokenClaimsSet.Builder claims = context.getClaims();
// Customize claims
};
}
如果 未作为 提供或未通过 配置,则 将自动配置为 。OAuth2TokenGenerator@BeanOAuth2AuthorizationServerConfigurerOAuth2TokenCustomizer<OAuth2TokenClaimsContext>@BeanOAuth2AccessTokenGenerator |
使用泛型类型 () 声明的提供了自定义 . 提供对 的访问,允许添加、替换和删除标头。 提供对 的访问,允许添加、替换和删除声明。OAuth2TokenCustomizer<JwtEncodingContext>JwtEncodingContextimplements OAuth2TokenContextJwtJwtEncodingContext.getJwsHeader()JwsHeader.BuilderJwtEncodingContext.getClaims()JwtClaimsSet.Builder
以下示例演示如何实现 an 并使用 :OAuth2TokenCustomizer<JwtEncodingContext>JwtGenerator
@Bean
public OAuth2TokenGenerator<?> tokenGenerator() {
JwtEncoder jwtEncoder = ...
JwtGenerator jwtGenerator = new JwtGenerator(jwtEncoder);
jwtGenerator.setJwtCustomizer(jwtCustomizer());
OAuth2AccessTokenGenerator accessTokenGenerator = new OAuth2AccessTokenGenerator();
OAuth2RefreshTokenGenerator refreshTokenGenerator = new OAuth2RefreshTokenGenerator();
return new DelegatingOAuth2TokenGenerator(
jwtGenerator, accessTokenGenerator, refreshTokenGenerator);
}
@Bean
public OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer() {
return context -> {
JwsHeader.Builder headers = context.getJwsHeader();
JwtClaimsSet.Builder claims = context.getClaims();
if (context.getTokenType().equals(OAuth2TokenType.ACCESS_TOKEN)) {
// Customize headers/claims for access_token
} else if (context.getTokenType().getValue().equals(OidcParameterNames.ID_TOKEN)) {
// Customize headers/claims for id_token
}
};
}
如果 未作为 提供或未通过 配置,则 将自动配置为 。OAuth2TokenGenerator@BeanOAuth2AuthorizationServerConfigurerOAuth2TokenCustomizer<JwtEncodingContext>@BeanJwtGenerator |
| 有关演示如何自定义 ID 令牌的示例,请参阅指南操作方法:自定义 OpenID Connect 1.0 UserInfo 响应。 |
如果 未作为 提供或未通过 配置,则 将自动配置为 。OAuth2TokenGenerator@BeanOAuth2AuthorizationServerConfigurerOAuth2TokenCustomizer<OAuth2TokenClaimsContext>@BeanOAuth2AccessTokenGenerator |
如果 未作为 提供或未通过 配置,则 将自动配置为 。OAuth2TokenGenerator@BeanOAuth2AuthorizationServerConfigurerOAuth2TokenCustomizer<JwtEncodingContext>@BeanJwtGenerator |
| 有关演示如何自定义 ID 令牌的示例,请参阅指南操作方法:自定义 OpenID Connect 1.0 UserInfo 响应。 |
会话注册表
如果启用了 OpenID Connect 1.0,则使用实例来跟踪经过身份验证的会话。
与 OAuth2 授权终结点关联的默认实现用于注册新的经过身份验证的会话。SessionRegistrySessionRegistrySessionAuthenticationStrategy
如果未注册 a,则将使用默认实现。SessionRegistry@BeanSessionRegistryImpl |
如果 a 已注册并且是 的实例,则还应注册 a 作为其负责通知会话生命周期事件(例如 ),以提供删除实例的功能。SessionRegistry@BeanSessionRegistryImplHttpSessionEventPublisher@BeanSessionRegistryImplSessionDestroyedEventSessionInformation |
当最终用户请求注销时,OpenID Connect 1.0 注销端点使用 查找与经过身份验证的最终用户关联的注销以执行注销。SessionRegistrySessionInformation
如果正在使用 Spring Security 的并发会话控制功能,建议注册一个以确保它在 Spring Security 的并发会话控制和 Spring 授权服务器的注销功能之间共享。SessionRegistry@Bean
以下示例演示如何注册 a 和 (必需):SessionRegistry@BeanHttpSessionEventPublisher@BeanSessionRegistryImpl
@Bean
public SessionRegistry sessionRegistry() {
return new SessionRegistryImpl();
}
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
如果未注册 a,则将使用默认实现。SessionRegistry@BeanSessionRegistryImpl |
如果 a 已注册并且是 的实例,则还应注册 a 作为其负责通知会话生命周期事件(例如 ),以提供删除实例的功能。SessionRegistry@BeanSessionRegistryImplHttpSessionEventPublisher@BeanSessionRegistryImplSessionDestroyedEventSessionInformation |