我知道这个问题可以通过不同的解决方案找到。但我无法让它在我的项目中工作。
我们正在向用户发送邮件,其中包含在应用程序中执行某些操作的链接。当用户点击 url 时,如果他没有登录,他应该被重定向到登录页面,登录后应该导航到目标 URL。
我正在尝试使用 CustomLoginSuccessHandler 进行修复,这里是代码:
public class CustomLoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
// public CustomLoginSuccessHandler(String defaultTargetUrl) {
// setDefaultTargetUrl(defaultTargetUrl);
// }
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
HttpSession session = request.getSession(false);
if (session != null) {
String redirectUrl = (String) session.getAttribute("url_prior_login");
if (redirectUrl != null) {
// we do not forget to clean this attribute from session
session.removeAttribute("url_prior_login");
// then we redirect
getRedirectStrategy().sendRedirect(request, response, redirectUrl);
} else {
super.onAuthenticationSuccess(request, response, authentication); …Run Code Online (Sandbox Code Playgroud)