需要手动重新授权使用 OAuth2AuthorizationRequest 不起作用

问题描述 投票:0回答:0

我正在构建一个客户端应用程序,该应用程序需要能够在身份验证级别不够的情况下升级用户。 为此,资源服务器通过自省评估 access_token,并将带有 WWW-Authenticate 标头的 401 返回给客户端。

客户端实现 @ExceptionHandler 来捕获 401,然后应该执行必要的操作以使用新的身份验证级别进行重新授权。

目前我正在尝试创建一个 OAuth2AuthorizationRequest,我在其中添加了我的 additionalParameters。 但是,返回的 Oauth2AuthorizationRequest 为空,因此我无法获得正确的 Oauth2AuthorizationRequest 以通过 OAuth2AuthorizedClientManager 进行授权。

本质上过程如下:

  • 用户访问客户端并被发送出去进行身份验证
  • 用户使用客户端对资源服务器执行基本操作
  • 用户执行更多关键操作,资源服务器响应 401
  • 客户端被重定向到授权服务器重新进行身份验证(基于授权请求中指定的新身份验证级别)
  • 用户重新验证并使用新会话重定向到客户端应用程序
  • 重新尝试用户的初始操作(现在使用新会话)到资源服务器

我已经开发了我的大部分客户端应用程序...... 我只是坚持创建正确的 OAuth2AuthorizationRequest 以将用户重定向到我的 OIDC 提供商,并使用新的会话详细信息(access_token、userinfo、id token)更新会话。

@PostMapping("/reauthenticate")
public String postAuthenticate(OAuth2AuthenticationToken authentication, @RequestParam("acr") String acr, HttpServletRequest request, HttpServletResponse response) {
              
CustomAuthorizationRequestResolver authorizationRequestResolver = new CustomAuthorizationRequestResolver(repo);
// Create a new OAuth2AuthorizationRequest using your custom AuthorizationRequestResolver
OAuth2AuthorizationRequest authorizationRequest = authorizationRequestResolver.resolve(request);
// Add the additionalQueryParam to the authorization request
authorizationRequest = authorizationRequestResolver.modifyRequest(authorizationRequest, "acr_values", acr);
   
OAuth2AuthorizedClient authorizedClient = authorizedClientManager.authorize( (OAuth2AuthorizeRequest) authorizationRequest );
                
return "index";  
spring-security authorization spring-security-oauth2
© www.soinside.com 2019 - 2024. All rights reserved.