尝试让 Spring Security OAuth2 5.4.2 使用自定义范围并在 授权代码流程中使用 Auth0。我偶然发现了一个小细节,我没有简单的解决方案。
要请求自定义范围,Auth0 要求用户提供
audience
参数。重定向 URI 在 org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver#resolve(javax.servlet.http.HttpServletRequest, java.lang.String, java.lang.String)
中创建。
这个过程可以通过使用
AuthorizationRequestCustomizer
来修改,但是我没有看到任何注入定制器的方法,并且 DefaultOAuth2AuthorizationRequestResolver
没有注册为 bean,或者至少我不明白它是如何访问的。
您可以实现自定义 OAuth2AuthorizationRequestResolver ,然后将其添加到您的 Spring 安全配置中
.oauth2Login(req->
req.authorizationEndpoint()
.authorizationRequestResolver(new YourCustomAuthorizationRequestResolver)
)
以下是我的场景: Spring Boot + Spring Boot Admin + Spring Security。 配置属性
spring.boot.admin.context-path
后,OAuth2 授权代码流将使用错误的重定向 uri,其中不包含 admin.context-path
。
org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver#setAuthorizationRequestCustomizer
不方便更新redirect-uri
通过构建器,我还计划重写 DefaultOAuth2AuthorizationRequestResolver
,因为我需要更新重定向 uri 生成逻辑。见下文:
String redirectUriStr = expandRedirectUri(request, clientRegistration, redirectUriAction);