How-to: Implement an Extension Authorization Grant Type
本指南介绍了如何使用 extension authorization grant type扩展 Spring Authorization Server。本指南的目的是展示如何实现扩展授权授予类型并在 OAuth2 Token endpoint处对其进行配置。
使用新的授权授予类型扩展 Spring 授权服务器,需要实现 AuthenticationConverter`和 `AuthenticationProvider
,并在 OAuth2 Token endpoint处配置这两个组件。除了组件实现之外,一个唯一的绝对 URI 也需要分配给 `grant_type`参数使用。
Implement AuthenticationConverter
假设 grant_type
参数的绝对 URI 为 urn:ietf:params:oauth:grant-type:custom_code
,并且 code
参数表示授权授予,则以下示例展示了 AuthenticationConverter
的样例实现:
link:{examples-dir}/main/java/sample/extgrant/CustomCodeGrantAuthenticationConverter.java[role=include]
单击以上代码示例中“展开折叠文本”图标以显示完整示例。 |
1 | 如果 grant_type 参数是 not urn:ietf:params:oauth:grant-type:custom_code ,那么返回 null ,从而允许另一个 AuthenticationConverter 处理令牌请求。 |
2 | code 参数包含授权授予。 |
3 | 返回由 xref:guides/how-to-ext-grant-type.adoc#implement-authentication-provider[CustomCodeGrantAuthenticationProvider 处理的 CustomCodeGrantAuthenticationToken 的实例。 |
Implement AuthenticationProvider
AuthenticationProvider
实现负责验证授权授予,如果有效且经过授权,则颁发访问令牌。
以下示例展示了 AuthenticationProvider
的样例实现:
link:{examples-dir}/main/java/sample/extgrant/CustomCodeGrantAuthenticationProvider.java[role=include]
|
Configure OAuth2 Token Endpoint
以下示例展示了如何使用 `AuthenticationConverter`和 `AuthenticationProvider`配置 OAuth2 Token endpoint:
link:{examples-dir}/main/java/sample/extgrant/SecurityConfig.java[role=include]
1 | 将 AuthenticationConverter 添加到 OAuth2 令牌端点配置。 |
2 | 将 AuthenticationProvider 添加到 OAuth2 令牌端点配置。 |