Spring Authorization Server 学习笔记(一)
Spring Authorization Server 学习笔记(二)
Spring Authorization Server 学习笔记(三)

核心组件(2)- RegisteredClient

RegisteredClient 对应 The OAuth 2.1 Authorization Framework 中的 client

client 是发起受保护资源请求的应用程序,是代表资源所有者发起的,是在其授权下发起的1。‌

根据 Spring Authorization Server 的文档RegisteredClient 是在 authorization server 注册过的 client,注册 client 必须要有 client-identifier 也就是 clientId,可根据 client-types 的不同决定是否需要 clientSecret,此外还需要与 clientId 关联的元数据(详见代码部分),RegisteredClient 的主要目的是请求访问受保护资源,其访问受保护资源的步骤为:

  1. client 发起获取 access_token 的请求
  2. authorization server 签发 access_token
  3. clientaccess_token 访问受保护资源

RegisteredClient 的属性

String id

注意,这个并非上文中提到的 clientId ,代码注释中的说法比较清楚,这是 identifier for the registration,相当于是 registrationId

String clientId

这才是 clientId,客户端的认证用的也是这个。

Instant clientIdIssuedAt

issue clientId 的时间,issue 在OAuth2的认证流程中是个高频词汇,不管翻译成什么,都有些不伦不类,这里不做翻译,大意为颁发。

String clientSecret

密码,必须用 PasswordEncoder 编码,前几篇我们都用了明文,明文对应的 PasswordEncoder 实现为 NoOpPasswordEncoder,所以我们在密码前面写上了 {noop}。PasswordEncoder 是 Spring Security 的功能,不做赘述。

Instant clientSecretExpiresAt

密码过期时间,null 表示永久。

String clientName

这个属性,实际上是客户端描述,也需要与 clientId 区分开来,认证用到的是 clientIdclientName 仅在某些特定情况下做展示用。

Set clientAuthenticationMethods

客户端的认证方法,可选项为:

  • CLIENT_SECRET_BASIC
  • CLIENT_SECRET_POST
  • CLIENT_SECRET_JWT
  • PRIVATE_KEY_JWT
  • NONE
  • TLS_CLIENT_AUTH
  • SELF_SIGNED_TLS_CLIENT_AUTH

Set authorizationGrantTypes

授权方法,可选项为:

  • AUTHORIZATION_CODE
  • REFRESH_TOKEN
  • CLIENT_CREDENTIALS
  • JWT_BEARER
  • DEVICE_CODE
  • TOKEN_EXCHANGE

Set redirectUris

可用的 redirectUri。OAuth2 基于重定向的流程(比如 AUTHORIZATION_CODE)中,需要带一个 redirect_uri 的参数,如果此集合中只有一个值,则 redirect_uri 可省略,否则必传,如果实际传入的参数不在此集合中,则认证失败。

Set postLogoutRedirectUris

注销用的

Set scopes

客户端可用的范围

ClientSettings clientSettings

自定义配置,本质上就是一个 Map,可以配置任何东西,当然 Spring 也提供了一些常用的配置项。用 ClientSettings.builder() 可以很方便地构建 ClientSettings ,它的方法有:

  • setting(String name, Object value) 和 settings(Consumer<Map<String, Object>> settingsConsumer):向 Map 中任意配置。
  • requireProofKey(boolean requireProofKey):PKCE 相关配置,AUTHORIZATION_CODE 流程可选
  • requireAuthorizationConsent(boolean requireAuthorizationConsent):是否需要手动点同意,类似于某个 app 选了微信登录,跳转到微信需要点一下同意。交互类流程使用,如 AUTHORIZATION_CODEDEVICE_CODE
  • jwkSetUrl(String jwkSetUrl):见名知义
  • tokenEndpointAuthenticationSigningAlgorithm(JwsAlgorithm authenticationSigningAlgorithm):CLIENT_SECRET_JWTPRIVATE_KEY_JWT 用的 JWS 算法。
  • x509CertificateSubjectDN(String x509CertificateSubjectDN):用于 TLS_CLIENT_AUTH,DN 是 distinguished name 的缩写,详见 RFC 8705

TokenSettings tokenSettings

给客户端的 token 的配置。可配置项有:

  • authorizationCodeTimeToLive(Duration authorizationCodeTimeToLive):授权码有效期,默认 5 分钟
  • accessTokenTimeToLive(Duration accessTokenTimeToLive):access token有效期,默认 5 分钟
  • accessTokenFormat(OAuth2TokenFormat accessTokenFormat):默认 SELF_CONTAINED,可选 REFERENCE
  • deviceCodeTimeToLive(Duration deviceCodeTimeToLive):device code 有效期,默认 5 分钟
  • reuseRefreshTokens(boolean reuseRefreshTokens):refresh token 是否复用,默认 true,复用
  • refreshTokenTimeToLive(Duration refreshTokenTimeToLive):refresh token 有效期,默认 60 分钟
  • idTokenSignatureAlgorithm(SignatureAlgorithm idTokenSignatureAlgorithm):为 id token 制定 JWS 算法,默认 RS256
  • x509CertificateBoundAccessTokens(boolean x509CertificateBoundAccessTokens):使用 TLS_CLIENT_AUTHSELF_SIGNED_TLS_CLIENT_AUTH 时,是否绑定 access token 和 x509 证书,默认 false

  1. “client”: An application making protected resource requests on behalf of the resource owner and with its authorization. ↩︎

更多推荐