Redirect from login interceptor in Struts 2(从 Struts 2 中的登录拦截器重定向)
问题描述
我们的应用程序要求用户登录才能查看任何内容.LoginInterceptor 拦截对所有页面的访问,如果用户没有有效的会话,则会显示登录表单.
Our application requires users to be logged in to view any content. Access to all pages is intercepted by LoginInterceptor which brings up the login form if there's no valid session for the user.
我希望拦截器在显示登录表单之前记住原始请求 URI,并在登录表单验证成功时重定向到它.
I'd like the interceptor to remember the original request URI before displaying the login form and redirect to it if the login form validation is successful.
我尝试按照 Struts 2 Redirect to correct action after身份验证拦截器.
@Service
@Results({
@Result(name = "redirect", type = "redirect", location = "${savedUrl}")
})
public class LoginInterceptor extends AbstractInterceptor {
//...
private String savedUrl;
//...
@Override
public final String intercept(final ActionInvocation invocation) throws Exception {
// ...
savedUrl = (String) session.getAttribute("savedUrl");
// ...
if (processLogin(request, session)) { // validate login form
if (!StringUtils.isEmpty(savedUrl)) {
return "redirect";
}
return LOGIN_SUCCESS_RESULT;
}
// if there's no loginData in sesssion, remeber the URI and display a login form
String queryString = request.getQueryString();
session.setAttribute("savedUrl", request.getRequestURI() + (queryString==null ? "" : ("?" + queryString)));
return "login";
}
// ...
public String getSavedUrl(){
return savedUrl;
}
}
但是,由于 return redirect",我得到一个空白页.getSavedUrl() 永远不会被调用.
However I get a blank page as a result of return "redirect". getSavedUrl() is never called.
解决方案:
完全删除 @Results 注释,而不是 return "redirect"; 调用
Scratch the @Results annotation completely and instead of return "redirect"; call
response.sendRedirect(savedUrl); return null;
推荐答案
如果没有登录则重定向到 LOGIN 结果.然后你应该重写你的拦截器像
If not logged in then redirect to LOGIN result. Then you should rewrite your interceptor something like
public final String intercept(final ActionInvocation invocation) throws Exception {
// before save original url
Map session = invocation.getInvocationContext().getSession();
Object action = invocation.getAction();
if (!(action instanceof LoginAction)){
String queryString = request.getQueryString();
session.put("savedUrl", request.getRequestURI()+(queryString==null?"":("?"+queryString)));
} else {
return invocation.invoke();
}
if (!processLogin(request, session)) { //return false if not authenticated
session.put("isLogin", true);
return Action.LOGIN;
} else {
savedUrl = (String) session.get("savedUrl");
boolean isLogin = (boolean)session.get("isLogin");
if (!StringUtils.isEmpty(savedUrl) && isLogin) {
session.put("isLogin", false);
return "redirect";
}
return invocation.invoke();
}
}
这篇关于从 Struts 2 中的登录拦截器重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Struts 2 中的登录拦截器重定向
基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
