如何从OAuth2 SSO Principal
获取有关JWT的详细信息? ( OAuth2Authentication
getDetails
的实例 OAuth2AuthenticationDetails
getDecodedDetails
返回 null
)
我有...
-
Angular 6客户端w /隐式登录为
acme
client(使用angular-oauth2-oidc
) -
Spring Boot OAuth2授权服务器,带JWT
TokenService
配置w /第三方SSO到GitHub -
Auth服务器配置为
acme
asimplicit
和GitHub客户端用于SSO -
Auth服务器公开
/login/github
-
Auth服务器公开
/me
(受ResourceServer配置保护)
当我登录时......
-
Angular app重定向到Auth服务登录
-
Auth服务重定向到GitHub
-
[用户验证]
-
GitHub重定向到Auth服务
-
Auth服务启动会话并发出令牌
-
Auth服务重定向到Angular
-
浏览器令牌是一个合适的JWT
现在,当我与Auth服务 /me
沟通时:
-
直接,我得到
Principal
,其中包含GitHub(yay)的所有详细信息 -
间接来自Angular应用程序通过
Authorization: Bearer ...
标头传递令牌,我得到一个包含acme
client(ugh)的最小OAuth客户端信息的Principal
我已经尝试了自定义 TokenEnhancer
,但 OAuth2Authentication
实例已经是最低限度,没有任何细节 . 并且,当从Angular启动调用时,它不希望共享会话 - 我想将详细信息放在JWT中) .
[Update #1]
我尝试了自定义 JwtAccessTokenConverter
并在 @EnableAuthorizationServer
和 @EnableResourceServer
(确保 /me
endpoints )配置类中使用它 . 但它没有用 . 我仍然从 OAuth2Authentication
获得null详细信息 .
final JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setAccessTokenConverter(new CustomTokenConverter());
1 回答
Spring Lemon这样做的方式是替换OAuth2和OpenID连接用户服务(请参阅spring security docs) . 有关详细信息,请参阅
LemonOAuth2UserService
和LemonOidcUserService
. 对于无状态,它会将客户端作为param的短路JWT令牌传递给targetUrl,正如您在OAuth2AuthenticationSuccessHandler
类中看到的那样 . 它使用一些cookie机制来完成所有这些无状态的操作,可以通过查看它的HttpCookieOAuth2AuthorizationRequestRepository
以及它是如何配置来进一步理解的 .这篇文章更详细地解释了这个:https://www.naturalprogrammer.com/blog/1681261/spring-security-5-oauth2-login-signup-stateless-restful-web-services .