Spring Authorization Server 1.5.2 使用YML配置的方式,最常用法总结

发布于:2025-09-04 ⋅ 阅读:(17) ⋅ 点赞:(0)

OAuth2常用授权方式配置及使用

授权码模式(authorization_code)

授权码模式是最常用的OAuth2授权方式,适用于有后端的Web应用。用户通过浏览器重定向到授权服务器,授权后返回授权码,后端用授权码换取访问令牌。

配置步骤:

  • 在授权服务器注册客户端,配置回调地址(redirect_uri)。
  • 客户端需要提供client_id和client_secret。

使用流程:

  • 客户端引导用户访问授权端点,携带client_id、redirect_uri、response_type=code等参数。
  • 用户授权后,授权服务器重定向到回调地址,附带授权码。
  • 客户端后端用授权码、client_id、client_secret向令牌端点请求访问令牌。

示例请求授权码:

oidc-client:
            registration:
              client-id: "oidc-client"
              client-name: "oidc-client"
              client-secret: "{noop}artvqIe21qvqDlXE8LPGDxGctcgFCh06idXGBOc5Ed0"
              client-authentication-methods:
                - "client_secret_basic"
                - "client_secret_post"
                - "client_secret_jwt"
                - "private_key_jwt"
                - "tls_client_auth"
                - "self_signed_tls_client_auth"
                - "none"
              authorization-grant-types:
                - "authorization_code"
                - "refresh_token"
              redirect-uris:
                - "http://localhost:8080/login/oauth2/code/authcodeNone"
                - "http://localhost:8080/login/oauth2/code/authcodeBasic"
                - "http://localhost:8080/login/oauth2/code/authcodePost"
                - "http://localhost:8080/login/oauth2/code/secretJwt"
                - "http://localhost:8080/login/oauth2/code/privateJwt"
              post-logout-redirect-uris:
                - "http://127.0.0.1:8080/"
              scopes:
                - "openid"
                - "profile"
            require-authorization-consent: true
            #token-endpoint-authentication-signing-algorithm: HS256
            token-endpoint-authentication-signing-algorithm: RS256
            jwk-set-uri: http://localhost:8080/jwks

这里是在服务端注册了一个client,该client支持的授权方式为授权码方式,支持的认证方式有

多种:不同的方式的安全级别也不一样。

客户端模式(client_credentials)

客户端模式适用于机器对机器的场景,客户端直接用自己的凭证获取令牌,无需用户参与。

配置步骤:

  • 在授权服务器注册客户端,确保客户端有client_credentials权限。
  • 客户端需要妥善保管client_secret。

使用流程:

  • 客户端直接向令牌端点发送请求,携带client_id、client_secret和grant_type=client_credentials。

示例请求:

just-client:
            registration: 
              client-id: "just-client"
              client-name: "just-client"
              client-secret: "artvqIe21qvqDlXE8LPGDxGctcgFCh06idXGBOc5Ed0"
              client-authentication-methods:
                - "client_secret_basic"
                - "client_secret_post"
                - "client_secret_jwt"
                - "private_key_jwt"
              authorization-grant-types:
                - "client_credentials"
            token-endpoint-authentication-signing-algorithm: RS256
            jwk-set-uri: http://localhost:8080/jwks

可以看到客户模式也是支持多种认证方式,尤其是client_secret_jwt和private_key_jwt,这让认证

更加安全。

注意事项
  • 授权码模式需要妥善处理回调地址,避免开放重定向漏洞。
  • 客户端模式不应在前端使用,避免泄露client_secret。
  • JWT Bearer模式需要确保JWT的签名安全,防止篡改。
  • 所有模式都应使用HTTPS保证通信安全。
  • 令牌应有合理有效期,必要时使用刷新令牌(refresh_token)机制。

OAuth2常用授权方式配置及使用

授权码模式(authorization_code)

授权码模式是最常用的OAuth2授权方式,适用于有后端的Web应用。用户通过浏览器重定向到授权服务器,授权后返回授权码,后端用授权码换取访问令牌。

配置步骤:

  • 在授权服务器注册客户端,配置回调地址(redirect_uri)。
  • 客户端需要提供client_id和client_secret。

使用流程:

  • 客户端引导用户访问授权端点,携带client_id、redirect_uri、response_type=code等参数。
  • 用户授权后,授权服务器重定向到回调地址,附带授权码。
  • 客户端后端用授权码、client_id、client_secret向令牌端点请求访问令牌。

示例请求授权码:

GET /authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=read&state=STATE HTTP/1.1
Host: auth-server.com

示例换取令牌:

POST /token HTTP/1.1
Host: auth-server.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=REDIRECT_URI&client_id=CLIENT_ID&client_secret=CLIENT_SECRET

客户端模式(client_credentials)

客户端模式适用于机器对机器的场景,客户端直接用自己的凭证获取令牌,无需用户参与。

配置步骤:

  • 在授权服务器注册客户端,确保客户端有client_credentials权限。
  • 客户端需要妥善保管client_secret。

使用流程:

  • 客户端直接向令牌端点发送请求,携带client_id、client_secret和grant_type=client_credentials。

示例请求:

POST /token HTTP/1.1
Host: auth-server.com
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET

注意事项
  • 授权码模式需要妥善处理回调地址,避免开放重定向漏洞。
  • 客户端模式不应在前端使用,避免泄露client_secret。
  • JWT Bearer模式需要确保JWT的签名安全,防止篡改。
  • 所有模式都应使用HTTPS保证通信安全。
  • 令牌应有合理有效期,必要时使用刷新令牌(refresh_token)机制。

OAuth2常用授权方式配置及使用

授权码模式(authorization_code)

授权码模式是最常用的OAuth2授权方式,适用于有后端的Web应用。用户通过浏览器重定向到授权服务器,授权后返回授权码,后端用授权码换取访问令牌。

配置步骤:

  • 在授权服务器注册客户端,配置回调地址(redirect_uri)。
  • 客户端需要提供client_id和client_secret。

使用流程:

  • 客户端引导用户访问授权端点,携带client_id、redirect_uri、response_type=code等参数。
  • 用户授权后,授权服务器重定向到回调地址,附带授权码。
  • 客户端后端用授权码、client_id、client_secret向令牌端点请求访问令牌。

示例请求授权码:

GET /authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=read&state=STATE HTTP/1.1
Host: auth-server.com

示例换取令牌:

POST /token HTTP/1.1
Host: auth-server.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=AUTHORIZATION_CODE&redirect_uri=REDIRECT_URI&client_id=CLIENT_ID&client_secret=CLIENT_SECRET

客户端模式(client_credentials)

客户端模式适用于机器对机器的场景,客户端直接用自己的凭证获取令牌,无需用户参与。

配置步骤:

  • 在授权服务器注册客户端,确保客户端有client_credentials权限。
  • 客户端需要妥善保管client_secret。

使用流程:

  • 客户端直接向令牌端点发送请求,携带client_id、client_secret和grant_type=client_credentials。

示例请求:

POST /token HTTP/1.1
Host: auth-server.com
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET

JWT Bearer模式

JWT Bearer模式允许客户端使用JWT作为授权断言来获取访问令牌,适用于已有身份验证机制的系统集成。

配置步骤:

  • 在授权服务器注册客户端,配置JWT签名验证的公钥或密钥。
  • 客户端需要生成符合要求的JWT,包含iss、sub、aud、exp等声明。

使用流程:

  • 客户端生成JWT,使用私钥签名。
  • 向令牌端点发送请求,携带grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer和assertion参数(JWT)。

示例JWT内容:

{
  "iss": "client_id",
  "sub": "client_id",
  "aud": "https://auth-server.com/token",
  "exp": 1625097600,
  "iat": 1625094000
}

示例请求:

POST /token HTTP/1.1
Host: auth-server.com
Content-Type: application/x-www-form-urlencoded

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=JWT_TOKEN

注意事项
  • 授权码模式需要妥善处理回调地址,避免开放重定向漏洞。
  • 客户端模式不应在前端使用,避免泄露client_secret。
  • JWT Bearer模式需要确保JWT的签名安全,防止篡改。
  • 所有模式都应使用HTTPS保证通信安全。
  • 令牌应有合理有效期,必要时使用刷新令牌(refresh_token)机制。

具体代码参考:Spring Authorization Server: Spring Authorization Server Demo 最佳实践 - Gitee.com