我想根据login.jsp页面中的一些隐藏值重定向我的页面,这是我的代码
public class AuthenticationHandler extends SavedRequestAwareAuthenticationSuccessHandler {
private final String HASH_URL = "hashURL";
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
super.onAuthenticationSuccess(request, response, authentication);
String hashValue = request.getParameter(HASH_URL);
if(hashValue != null){
if(!hashValue.isEmpty()){
setDefaultTargetUrl("/home/" + hashValue);
}
}
}
}
我可以看到hashValue从login.jsp页面获取最新值,但它总是移动网站/ home / page,这是我的配置
<security:form-login
login-page="/auth/login"
default-target-url="/home/"
authentication-success-handler-ref="customAuthenticationSuccessHandler"
always-use-default-target="true"
authentication-failure-url="/auth/login?error=true"/>
答案 0 :(得分:1)
在应用程序运行时,您不应该调用setDefaultTargetUrl
。它是班级中的一个字段,而不是本地变量,因此它将适用于您网站的所有用户。
查看您在SavedRequestAwareAuthenticationSuccessHandler
中扩展的代码。如果要调用超类方法,了解它的工作原理至关重要。您在方法中首先调用super.onAuthenticationSuccess
。这将采用defaultTargetUrl
(或缓存请求)的当前值并重定向到该URL。您正在重定向后设置属性。
正如我所说,你不应该在运行时设置属性。我猜你的方法应该更像
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
String hashValue = request.getParameter(HASH_URL);
if(hashValue != null) {
response.sendRedirect(getDefaultTargetUrl() + "/" + hashValue;
} else {
response.sendRedirect(getDefaultTargetUrl());
}
}
你可能根本不需要扩展一个类。直接自己实施AuthenticationSuccessHandler
。如果没有继承,将会更容易理解发生了什么。
如果您使用的是自定义处理程序类,那么影响默认处理程序的名称空间属性(如always-use-default-target
)将不会产生任何影响。
答案 1 :(得分:1)
我已在之前的问题Redirecting URL after user logged out to previous page opened
中引用了此内容Following a successful authentication SavedRequestAwareAuthenticationSuccessHandler: If the alwaysUseDefaultTargetUrl property is set to true, the defaultTargetUrl will be used for the destination. Any DefaultSavedRequest stored in the session will be removed.
所以你应该设置always-use-default-target =“false”,你也可以删除default-target-url属性。
希望这有帮助。