在注销主窗口后,弹出窗口链接仍然有效

时间:2012-05-12 14:23:50

标签: login struts2 logout interceptor

我正在使用struts 2登录拦截器。代码工作正常。在我的应用程序中,许多弹出窗口已经使用。当我打开弹出窗口并从我的主窗口弹出窗口注销时显示我编码的登录页面但仅针对此场景,我希望它将显示任何消息(会话已过期或您已经已登出)而不是登录页面。

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

LoginInterceptor.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";
  }
}

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";

1 个答案:

答案 0 :(得分: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”属性,如果是,则向用户显示该属性。