在struts2 + hibernate中区分不同的用户

时间:2013-04-07 05:05:32

标签: struts2 actioncontext

struts.xml中

<package name="admin" extends="struts-default" namespace="/admin">
        <interceptors>
            <interceptor class="interceptor.LoginInterceptor" name="loginInterceptor"/>
            <interceptor-stack name="loginStack">
                <interceptor-ref name="loginInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>                 
        </interceptors>     

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

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

        <action name="index">
           <result>/WEB-INF/admin/index.jsp</result>
        </action>

        actions
        .
        .           

</package>

<package name="secure" extends="struts-default" namespace="/secure">
        <interceptors>
            <interceptor class="interceptor.LoginInterceptor" name="loginInterceptor"/>
            <interceptor-stack name="loginStack">
                <interceptor-ref name="loginInterceptor"/>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>                 
        </interceptors>     

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

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

        <action name="index">
           <result>/WEB-INF/admin/index.jsp</result>
        </action>

        <action class="actions.LoginAction" name="authenticateUser">
             <!-- generate dynamically for success-->
             <result name="input">/login.jsp</result>
             <result name="error">/login.jsp</result>
       </action>

</package>

这是我的iterceptor

public String intercept(ActionInvocation invocation) throws Exception {        
        final ActionContext context = invocation.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
        HttpSession session = request.getSession(true);

        Object user = session.getAttribute("user");
        if (user == null) {
            String loginAttempt = request.getParameter("loginAttempt");
            if(!StringUtils.isEmpty(loginAttempt)){
                return invocation.invoke();            
            }                            
            return "login";
        } else {            
            // Check for user role here
            // and redirect to action of specific namespace
            return invocation.invoke();
        } 
    }

对不起,我的错误是我误解了。

我的应用程序管理员和客户中有两种用户。 我想区分它们并基于角色重定向到特定的命名空间动作。

目前只有admin的身份验证功能。

我提出了这个解决方案。

但我不知道如何将请求网址映射到特定网站名称的操作“安全”#39;对于客户或&#39;管理员&#39;适用于基于角色的管理员。 否则显示全局异常。

admin的所有JSP都位于admin文件夹中,客户位于WEF-INF文件夹中的客户文件夹内。

我还指出了一些我仍需填写的评论,但不知道如何做到这一点。 对于cheking角色我知道,但是对于重定向,我不知道该怎么做。

我还没有实现这个。但我想到了这个想法。 甚至我认为可能与否。 如果有更好的解决方案,那么你可以告诉我。

0 个答案:

没有答案