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



我正在使用 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();
        }


    }

网络.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>

注销用户操作.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>

管理员用户操作.java

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

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

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

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 页面在显示登录屏幕时检查请求是否具有"会话过期"属性,如果是,则向用户显示该属性。

最新更新