我想实现一个LogoutSuccessHandler,它将当前用户重定向到最后一页( referer ),但前提是引用页面不受保护!
当前,无论注销用户是否可以访问该页面,我的LogoutSuccessHandler都会将用户重定向到上一页。有时这会导致用户直接重定向到登录页面的问题。如果引荐来源指向受保护的页面,则应将用户重定向到我的Web应用程序的索引/欢迎页面。
我当前的实现如下所示;
@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
String referer = request.getHeader("referer");
if (referer == null) {
referer = "/";
}
response.sendRedirect(refererUrl);
}
}
我尝试过但不起作用的内容:
@Component
public class MyLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {
@Autowired
private WebInvocationPrivilegeEvaluator webInvocationPrivilegeEvaluator;
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
super.onLogoutSuccess(request, response, authentication);
String refererUrl = request.getHeader("referer");
if (webInvocationPrivilegeEvaluator.isAllowed(refererUrl, authentication)) {
response.sendRedirect(refererUrl);
} else {
response.sendRedirect(request.getContextPath());
}
}
此方法存在问题:
所以有人知道如何实现我的目标吗?