Struts2从登录拦截器重定向

编程入门 行业动态 更新时间:2024-10-24 19:14:10
本文介绍了Struts2从登录拦截器重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们的应用程序要求用户登录才能查看任何内容。 LoginInterceptor 拦截对所有页面的访问,如果用户没有有效会话,则会显示登录表单。我希望拦截器在显示登录表单之前记住原始请求URI,并在登录表单验证成功时重定向到它。我尝试按照 Struts 2 Redirect更正身份验证拦截器后的操作。

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. 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. I tried to follow Struts 2 Redirect to correct action after authentication interceptor.

@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; } }

但是我得到一个空白页面返回重定向。永远不会调用 getSavedUrl()。

However I get a blank page as a result of return "redirect". getSavedUrl() is never called.

解决方案:完全刮开@Results注释而不是返回重定向; 调用 response.sendRedirect(savedUrl); return null;

Solution: 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(); } }

更多推荐

Struts2从登录拦截器重定向

本文发布于:2023-10-10 22:36:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   拦截器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!