重定向到struts 2拦截器中的另一个动作

发布于 2024-09-04 08:25:55 字数 1311 浏览 2 评论 0原文

我目前正在学习 Struts 2,并且正在构建一个简单的应用程序,其中未经验证的用户将被重定向到登录表单。

我有一个登录表单和操作功能,它获取用户凭据,验证它们并在会话中存储用户对象,但是我现在试图在登录发生之前阻止访问页面,并且我正在尝试使用拦截器来做到这一点。

我的问题是我编写了一个拦截器来检查 User 对象是否已保存在会话中,但如果没有,我想重定向到登录页面,并且在不绕过 struts 并使用HttpServletResponse.sendRedirect方法

配置:

<package name="mypackage" extends="struts-default" namespace="/admin">

    <interceptors>
        <interceptor name="login" class="my.LoginInterceptor" />
    </interceptors>

    <default-interceptor-ref name="login"/>

    <action name="login" class="my.LoginAction">
        <result name="input">/admin/login.jsp</result>
        <result name="success" type="redirect">/admin</result>
    </action>

    <action name="private" class="my.PrivateAction">
        <result>/admin/private.jsp</result>
    </action>

</package>

拦截器代码:

@Override
public String intercept(ActionInvocation inv) throws Exception {

    Map<String, Object> session = inv.getInvocationContext().getSession();

    Object user = session.get("user");
    if(user == null) {

                      // redirect to the 'login' action here            

    }
    else {
        return inv.invoke();
    }

}

I am currently in the process of learning Struts 2 and I am currently building a simple application where unverified users are redirected to a login form.

I have a login form and action functional which takes the users credentials, verifies them and stores a User object in the session however I am now trying to prevent access to pages before the login has taken place and I am trying to do this with an interceptor.

My problem is that I have written an interceptor that checks whether the User object has been saved in the session but if it has not I want to redirect to the login page and can't find any way of doing this without bypassing struts and using the HttpServletResponse.sendRedirect method

Configuration:

<package name="mypackage" extends="struts-default" namespace="/admin">

    <interceptors>
        <interceptor name="login" class="my.LoginInterceptor" />
    </interceptors>

    <default-interceptor-ref name="login"/>

    <action name="login" class="my.LoginAction">
        <result name="input">/admin/login.jsp</result>
        <result name="success" type="redirect">/admin</result>
    </action>

    <action name="private" class="my.PrivateAction">
        <result>/admin/private.jsp</result>
    </action>

</package>

The interceptor code:

@Override
public String intercept(ActionInvocation inv) throws Exception {

    Map<String, Object> session = inv.getInvocationContext().getSession();

    Object user = session.get("user");
    if(user == null) {

                      // redirect to the 'login' action here            

    }
    else {
        return inv.invoke();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望喜 2024-09-11 08:25:55

标准方法是返回一个特殊的全局结果(例如“login”)并定义从该结果到 admin/login.jsp 的全局映射。因此,您只需添加这一行:

if(user == null) {
      return "login";
}

在您的 struts.xml 中:

<global-results>
   <result name="login">/admin/login.jsp</result>
</global-results>

顺便说一句,恐怕您正在用单个拦截器替换默认的 Struts2 拦截器堆栈,通常您想要添加 你的拦截器到堆栈。例如:

<interceptors>
 <interceptor name="login" class="my.LoginInterceptor" />

 <interceptor-stack name="stack-with-login">
  <interceptor-ref name="login"/>
  <interceptor-ref name="defaultStack"/>
 </interceptor-stack>
</interceptors>
<default-interceptor-ref name="stack-with-login"/>

BTW2:当然,您不能将拦截器应用于您的登录操作。

The standard way is to return a special global result (eg "login") and define a global mapping from that result to your admin/login.jsp. So you just must add this line:

if(user == null) {
      return "login";
}

And in your struts.xml:

<global-results>
   <result name="login">/admin/login.jsp</result>
</global-results>

BTW, I'm afraid that you are replacing the default Struts2 interceptor stack with your single interceptor, normally you want to add your interceptor to the stack. Eg:

<interceptors>
 <interceptor name="login" class="my.LoginInterceptor" />

 <interceptor-stack name="stack-with-login">
  <interceptor-ref name="login"/>
  <interceptor-ref name="defaultStack"/>
 </interceptor-stack>
</interceptors>
<default-interceptor-ref name="stack-with-login"/>

BTW2: You must NOT apply the interceptor to your login action, of course.

输什么也不输骨气 2024-09-11 08:25:55

您可以在这里找到带有自定义登录拦截器的 struts2 的完整示例

http://sandeepbhardwaj.github.io/2010/12/01/struts2-with-login-interceptor.html

很棒的教程。

You can find the complete example of struts2 with a custom Login Interceptor here

http://sandeepbhardwaj.github.io/2010/12/01/struts2-with-login-interceptor.html

great tutorial.

饮惑 2024-09-11 08:25:55

如果您需要使用发送重定向,请返回 null 以避免此问题(例如从 www.domain.com 重定向到 domain.com):

public String intercept(final ActionInvocation invocation) throws Exception {

    String url=RequestUtil.getURLWithParams();  //you should implement this
    int index=url.indexOf("www");
    if (index!=-1 && index<10) {
        //Note: <10 to check that the www is in the domain main url
        //https://localhost:8443/mycontext/myaction.action?oneparam=http://www.youtube.com/user/someuser
        String redirection=url.replaceFirst("www\\.", ""); 
        LOG.debug("Redirection from "+url+" to "+redirection);
        RequestUtil.getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        RequestUtil.getResponse().sendRedirect(redirection);
        return null;
    }
    return invocation.invoke();
}

If you need to use send redirect, return null to avoid this problem (example redirecting from www.domain.com to domain.com):

public String intercept(final ActionInvocation invocation) throws Exception {

    String url=RequestUtil.getURLWithParams();  //you should implement this
    int index=url.indexOf("www");
    if (index!=-1 && index<10) {
        //Note: <10 to check that the www is in the domain main url
        //https://localhost:8443/mycontext/myaction.action?oneparam=http://www.youtube.com/user/someuser
        String redirection=url.replaceFirst("www\\.", ""); 
        LOG.debug("Redirection from "+url+" to "+redirection);
        RequestUtil.getResponse().setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
        RequestUtil.getResponse().sendRedirect(redirection);
        return null;
    }
    return invocation.invoke();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文