1

我正在使用 struts 2 登录拦截器。代码工作正常。在我的应用程序中使用了许多弹出窗口。当我打开我的弹出窗口并从我的主窗口中注销时,弹出窗口显示我编码时的登录页面但仅适用于这种情况,我希望它会显示任何消息(会话已过期或你有已注销)而不是登录页面。

如果需要任何修改,请通过我的代码

登录拦截器.java

public class LoginInterceptor extends AbstractInterceptor implements
        StrutsStatics{

        private AdminUserSessionInfo objAdminUserSessionInfo = new AdminUserSessionInfo();

    private static final long serialVersionUID = 1L;

    private static final Log log = LogFactory.getLog(LoginInterceptor.class);

    private static final String LOGIN_ATTEMPT = "loginAttempt";

    private static final String LOGIN_OUT = "loginOut";

    private static final String USER_HANDLE = "loggedInUser";

    Map sessionMap = null;


    public void init() {
        log.info("Intializing LoginInterceptor");
    }

    public void destroy() {
    }

    public String intercept(ActionInvocation invocation) throws Exception {

        final ActionContext context = invocation.getInvocationContext();

        HttpServletRequest request = (HttpServletRequest) context
                .get(HTTP_REQUEST);


        HttpServletResponse response = (HttpServletResponse) context
        .get(HTTP_RESPONSE);

        response.setHeader("Cache-Control", "no-store");

        response.setHeader("Pragma", "no-cache");

        response.setDateHeader("Expires", 0);




        HttpSession session = request.getSession(true);


        Object user = session.getAttribute(USER_HANDLE);

        String loginOut = request.getParameter(LOGIN_OUT);  




        if (user == null) {         

            String loginAttempt = request.getParameter(LOGIN_ATTEMPT);              

            System.out.println("loginAttemp---->"+loginAttempt);

            /* The user is attempting to log in. */
            if (!StringUtils.isBlank(loginAttempt)) {   


                return invocation.invoke();
            }
            return "login";
        } else {

            return invocation.invoke();
        }





    }

web.xml

<interceptors>  
        <interceptor class="org.iaf.aos.common.LoginInterceptor"
            name="loginInterceptor"></interceptor>          
        <interceptor-stack name="loginStack">
            <interceptor-ref name="loginInterceptor" />
            <interceptor-ref name="defaultStack" />
        </interceptor-stack>        
    </interceptors>

    <default-interceptor-ref name="loginStack"></default-interceptor-ref>
    <global-results><result name="login">aos.jsp</result></global-results>  
        <action name="checkUserLogin" class="org.iaf.aos.web.login.action.AdminUserAction" method="checkUserLogin">
                        <!-- <result name="success">index.jsp</result> -->
                        <interceptor-ref name="loginStack"></interceptor-ref>
                        <result name="success" type="chain">HomePage</result>
                        <result name="error">WEB-INF/jsp/admin/Error.jsp</result>
                        <result name="selectRole">aos1.jsp</result>
                        <!--<result name="selectRole">WEB-INF/jsp/admin/SelectRole.jsp</result>-->
                    </action>

                <action name="home">
                    <!--  <result>index.jsp</result>-->
                    <interceptor-ref name="loginStack"></interceptor-ref>
                    <result name="success" type="chain">HomePage</result>
                </action>

                <action name="logOutUser" class="org.iaf.aos.web.login.action.LogOutUserAction">                    
                    <interceptor-ref name="loginStack"></interceptor-ref>                   
                    <result name="logout">WEB-INF/jsp/admin/LoggedOut.jsp
                    </result>
                </action>

LogOutUserAction.java

public class LogOutUserAction extends ActionSupport {

    private static final long serialVersionUID = 1L;

public String execute() throws Exception { 
      System.out.println("inside :: LogOutUserAction------");

      Map session = ActionContext.getContext().getSession();


  session.remove("loggedInUser"); 
  return "logout";
  }
}

注销.jsp

<td width="*" align="right" valign="top">                                   
                                    <s:url var="urlLogOut" action="logOutUser.action">                              
                                        <s:param name="loginOut" value="%{'2'}"/>                                       
                                    </s:url>
                                    <sx:a href="%{#urlLogOut}" targets="divAddEditUser">
                                        <font color="white">Log Out</font>
                                    </sx:a>
                                        <!--<a href="logOutUser.action"><font color="white">Log Out</font></a>
                                    --></td>
                                </tr>

AdminUserAction.java

 ServletActionContext.getRequest().getSession().setAttribute("loggedInUser", loginId);

    return "selectRole";
4

1 回答 1

0

您必须明确地编写该逻辑,默认情况下不会这样做。

在从会话中删除用户后的注销操作中,也将其无效。

session.remove("loggedInUser"); 
session.invalidate();
session = null;

然后在您的拦截器中检查会话是否有效,如果不是,则添加一个属性,说明您的会话已过期或您已注销。

所以你的拦截器代码变成了这样:

HttpSession session = request.getSession(true);
if(session == null){
   request.setAttribute("SessionExpired","Your session has expired or you have logged out");
}
Object user = session.getAttribute(USER_HANDLE);

然后,如果您的 jsp 页面在您显示登录屏幕时检查请求是否具有“SessionExpired”属性,如果是则向用户显示。

于 2012-05-12T18:47:01.060 回答