我正在使用 spring 的新授权服务器实现自定义身份验证服务器。我们需要实现 CustomAccessTokenResponseHandler 以在 http json 响应中提供附加信息。我通过实现 AuthenticationSuccessHandler 并重写 onAuthenticationSuccess 方法来添加一些额外的用户信息来实现这一点。配置中还使用自定义身份验证授予类型(身份验证转换器、提供程序和令牌)和 TokenCustomizer,以向令牌添加额外的声明。问题是刷新令牌,即;当提供授予类型作为刷新令牌并请求新的访问令牌时,由于我没有提供用户名和密码,如果我不使用 customResponseHandler 但不是没有它,令牌仍然会生成。现在的问题是身份验证对象包含访问令牌,但现在在其主体中,我找不到用户详细信息。我无法理解 securityContext 在从刷新令牌创建访问令牌时如何获取用户信息以及在哪里可以找到这些详细信息。我只需要用户名来查询数据库并获取一些额外的信息以在响应中提供。
@Transactional
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
Auth2AccessTokenAuthenticationToken accessTokenAuthentication =
(OAuth2AccessTokenAuthenticationToken) authentication;
OAuth2ClientAuthenticationToken oAuth2ClientAuthenticationToken = (OAuth2ClientAuthenticationToken) accessTokenAuthentication.getPrincipal();
CustomPasswordUser user = (CustomPasswordUser) oAuth2ClientAuthenticationToken.getDetails();
..................
..........................}```
This piece of code is giving error because oAuth2ClientAuthenticationToken.getDetails() now has the following structure
OAuth2ClientAuthenticationToken [Principal=eQCLrL7JVHw1GRzP, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=null], Granted Authorities=[]].
SecurityContextHolder.getContext().getAuthentication().getDetails() returns WebAuthenticationDetails [RemoteIpAddress=0:0:0:0:0:0:0:1, SessionId=null].
P.S : I can get the actual accesstoken from authentication but I am parallely working on creating custom JWTEncoder using JWE, and do not want to decode the token inside authorization server nor does I think it is the right approach.
OAuth2AuthorizationService
。您可以查询 OAuth2Authorization
并从传递给您的 refresh_token
中的 authentication
获取用户的所有详细信息。