也许您应该检查 referer 标头文件,看看它是否满足您的需求:在成功登录过程后使用它重定向回来。检查this answer 是否使用SimpleUrlAuthenticationSuccessHandler
@Bean
public AuthenticationSuccessHandler successHandler() {
SimpleUrlAuthenticationSuccessHandler handler = new SimpleUrlAuthenticationSuccessHandler();
handler.setUseReferer(true);
return handler;
}
或者,如果您在 spring 中手动配置更多部分 - 您可以使用 this answer
用于在过滤阶段获取引荐来源网址并将其保存在会话中。
该答案的一种修改可能是:扩展 OAuth2ClientAuthenticationProcessingFilter 并在 doFilter 中获取引用值
public class MyOAuth2ClientAuthenticationProcessingFilter extends OAuth2ClientAuthenticationProcessingFilter {
...
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String referrer = request.getHeader("Referer");
if (null != referrer)
request.getSession().setAttribute("url_prior_login", referrer);
super.doFilter(req, res, chain);
}
}
所以你可以在处理你的'...handle-successful-authentication'后重定向 - 但我看到这个重定向是开销,试着把这个逻辑放在其他地方(例如,如果 UserInfoTokenServices 中的successHandler() 或 pricipalExtractor()您需要来自社交 oauth 提供商的更多用户详细信息)
successHandler() 可能如下所示:
@Bean
public AuthenticationSuccessHandler successHandler() {
AuthenticationSuccessHandler rst = new AuthenticationSuccessHandler() {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
...
HttpSession session = request.getSession();
String redirectUrl = null;
if (session != null) {
redirectUrl = (String) session
.getAttribute("url_prior_login");
if (null == redirectUrl || redirectUrl.trim().length() <= 0)
redirectUrl = "http://your_default_redirect_url";
response.sendRedirect(redirectUrl);
};
return rst;
}
无论如何,请检查弹簧docs