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 Details

    • OpenIDConnectService

      public OpenIDConnectService()
  • Method Details

    • getClient

      public OpenIDConnectClient getClient()
      获取OIDC客户端配置信息

      如果缓存中没有,则从发现文档URL获取并缓存

      Returns:
      OIDC客户端配置信息
    • token

      public Token token(String code, String codeVerifier)
      使用授权码换取 Token
      Parameters:
      code - 授权码 (Authorization Code)
      codeVerifier - PKCE 机制的验证码,若未启用可传 null
      Returns:
      换取成功返回 Token 对象,失败返回 null
    • jwtGenerator

      public JwksJWTGenerator jwtGenerator()
    • jwtGenerator

      public JwksJWTGenerator jwtGenerator(long clockSkewSeconds)
      构建 JwksJWTGenerator(懒获取 jwks uri,时钟容差由外部配置经构造传入)
      Parameters:
      clockSkewSeconds - 令牌验证时钟容差(秒),与全局配置 nif.auth.jwt.clock-skew-seconds 保持一致
      Returns:
      JwksJWTGenerator 实例
    • user

      public tech.riemann.nif.auth.service.AuthUser user(String token)
      使用 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 验证码,未启用可传 null
      expectedNonce - 发起登录时写入 Cookie 的 nonce(可 null,不启用 nonce 比对时忽略)
      Returns:
      认证用户信息
    • loginUrl

      public String loginUrl(String state, String nonce, String codeVerifier)
      登录地址
      Parameters:
      state - state
      nonce - nonce
      codeVerifier - codeVerifier
      Returns:
    • logoutUrl

      public String logoutUrl()
      登出地址
      Returns:
      discovery 未成功(无登出端点)时返回空串
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(@NonNull org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
      Specified by:
      setApplicationEventPublisher in interface org.springframework.context.ApplicationEventPublisherAware
      Parameters:
      applicationEventPublisher -
      See Also:
      • ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)