Class OpenIDConnectService
java.lang.Object
tech.riemann.nif.oidc.service.OpenIDConnectService
- All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationEventPublisherAware
public class OpenIDConnectService
extends Object
implements org.springframework.context.ApplicationEventPublisherAware
OpenID Connect服务类
提供基于OpenID Connect协议的完整认证流程
主要功能: 1. 获取OIDC发现文档(Discovery Document) 2. 使用授权码换取Token(支持PKCE) 3. 使用Token获取用户信息 4. 生成登录和登出URL 5. 发布登录成功事件
OIDC认证流程: 1. 调用loginUrl()生成授权URL 2. 用户重定向到OIDC提供商进行授权 3. 授权完成后回调,获取code和state 4. 调用token()使用code换取Token 5. 调用user()使用Token获取用户信息
使用示例(构造参数顺序由 @RequiredArgsConstructor 按 final 字段声明生成:
clientId, clientSecret, discoveryUrl, redirectUrl, openIDConnectHttpRestClient,
idTokenCheckEnabled, idTokenVerifierProvider):
OpenIDConnectService service = new OpenIDConnectService(
"client-id",
"client-secret",
"https://auth.example.com/.well-known/openid-configuration",
"https://app.example.com/callback",
restClient,
true,
null);
// 生成登录URL
String loginUrl = service.loginUrl(state, nonce, codeVerifier);
// 回调后换取Token和用户信息
AuthUser user = service.login(code, codeVerifier, nonce);
- Author:
- Kerbores(kerbores@riemann.tech)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription获取OIDC客户端配置信息jwtGenerator(long clockSkewSeconds) 构建 JwksJWTGenerator(懒获取 jwks uri,时钟容差由外部配置经构造传入)tech.riemann.nif.auth.service.AuthUser使用授权码进行登录,并按全局配置校验 ID Token(验签 + 有效期 + nonce)登录地址登出地址voidsetApplicationEventPublisher(@NonNull org.springframework.context.ApplicationEventPublisher applicationEventPublisher) 使用授权码换取 Tokentech.riemann.nif.auth.service.AuthUser使用 Token 换取并构建用户信息
-
Constructor Details
-
OpenIDConnectService
public OpenIDConnectService()
-
-
Method Details
-
getClient
-
token
-
jwtGenerator
-
jwtGenerator
构建 JwksJWTGenerator(懒获取 jwks uri,时钟容差由外部配置经构造传入)- Parameters:
clockSkewSeconds- 令牌验证时钟容差(秒),与全局配置 nif.auth.jwt.clock-skew-seconds 保持一致- Returns:
- JwksJWTGenerator 实例
-
user
使用 Token 换取并构建用户信息- Parameters:
token- 用户的访问令牌 (Access Token)- Returns:
- 构建好的 AuthUser 对象,若获取失败则返回 null
-
login
public tech.riemann.nif.auth.service.AuthUser login(String code, String codeVerifier, String expectedNonce) 使用授权码进行登录,并按全局配置校验 ID Token(验签 + 有效期 + nonce)校验流程: 1. 使用授权码换取 Token(含 id_token) 2. 若全局配置 idTokenCheckEnabled 且验证器可用,则校验 id_token:验签、exp、nonce 与发起登录时保存的 nonce 比对 3. 使用 access token 获取用户信息
- Parameters:
code- 授权码codeVerifier- PKCE 验证码,未启用可传 nullexpectedNonce- 发起登录时写入 Cookie 的 nonce(可 null,不启用 nonce 比对时忽略)- Returns:
- 认证用户信息
-
loginUrl
-
logoutUrl
-
setApplicationEventPublisher
public void setApplicationEventPublisher(@NonNull org.springframework.context.ApplicationEventPublisher applicationEventPublisher) - Specified by:
setApplicationEventPublisherin interfaceorg.springframework.context.ApplicationEventPublisherAware- Parameters:
applicationEventPublisher-- See Also:
-