我使用具有代码授予类型的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。如何强制使用任意重定向网址?
答案 0 :(得分:0)
我通过完全不指定重定向URL来解决了这个问题。相反,我利用了OAuth2AuthorizationCodeGrantFilter
默认使用的授权URL。
我在RestController中的路径/authorize/oauth2/code/{providerId}
上注册了两个RequestMapping,一个是params = "!error"
,另一个是params = "error"
。由于OAuth过滤器对重定向请求不执行任何操作(因为它已从oauth参数中删除),因此这两个映射可以用作成功和失败授权处理程序。
另一方面,我在spring-security中打开了一个issue以支持可注射的RequestCache
,这将启用适当的适当重定向机制。这个问题目前似乎可以解决,但还包含其他两种可能的解决方法。