【问题标题】:Redirect to a specific URL after successful anonymous auth via Spring Security OAuth2 client通过 Spring Security OAuth2 客户端成功匿名身份验证后重定向到特定 URL
【发布时间】:2023-04-01 19:10:01
【问题描述】:

我使用带有代码授权类型的 Spring Security 的 OAuth2 客户端身份验证来进行匿名身份验证。

内置的OAuth2AuthorizationCodeGrantFilter 在匿名主体成功认证后进行重定向。 它重定向到通过存储在 RequestCache 中的请求获得的 URL,或使用从查询参数中剥离的默认 url (oauth2/code/{registrationId})。

OAuth2AuthorizationCodeGrantFilter 的代码有问题:

String redirectUrl = authorizationResponse.getRedirectUri();
SavedRequest savedRequest = this.requestCache.getRequest(request, response);
if (savedRequest != null) {
    redirectUrl = savedRequest.getRedirectUrl();
    this.requestCache.removeRequest(request, response);
}
this.redirectStrategy.sendRedirect(request, response, redirectUrl);

我可以利用请求缓存来存储重定向请求。但是RequestCache 的界面不允许我为重定向指定任意 URL,只能使用现有的(不可变的)HttpServletRequest。

我需要根据一些业务逻辑重定向到特定的 URL。如何强制使用任意重定向 URL?

【问题讨论】:

    标签: spring-security spring-security-oauth2 http-redirect


    【解决方案1】:

    我通过根本不指定重定向 URL 解决了这个问题。相反,我利用了OAuth2AuthorizationCodeGrantFilter 默认使用的授权 URL。

    我在路径/authorize/oauth2/code/{providerId} 的RestController 中注册了两个RequestMapping,一个使用params = "!error",第二个使用params = "error"。由于 OAuth 过滤器对重定向请求不做任何事情(因为它已从 oauth 参数中剥离),所以这两个映射可以用作成功和失败授权处理程序。

    另一方面,我在 spring-security 中打开了一个 issue 以支持可注入的 RequestCache,这将启用正确的重定向机制。该问题目前似乎已得到解决,但包含其他两种可能的解决方法。

    【讨论】:

      猜你喜欢
      • 2013-07-20
      • 2019-01-24
      • 1970-01-01
      • 2018-06-15
      • 2020-10-04
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2015-07-27
      相关资源
      最近更新 更多