注销后 back/reload 问题 Struts 2
After logout back/reload issue in Struts 2
我有一个登录页面(Index.jsp
),用户在这里输入用户名和密码。在提交 LoginAuthentification.java
(action class) 时调用并验证用户,但根据操作 class 中的结果,它 returns JSP.
<action name="login" class="com.struts2.LoginAuthentication"
method="execute">
<interceptor-ref name="clear-cache" />
<result name="manager">/ManagerView.jsp</result>
<result name="SSE" type="redirectAction">
<param name="actionName">viewPlan</param>
<param name="userID">${userID}</param>
</result>
<result name="input">/Index.jsp</result>
<result name="error">/error.jsp</result>
</action>
在这种情况下,它返回 ManagerView.jsp
。现在在这个 JSP 中,我为 logout
添加了一个超链接,它在下面做
<action name="logout" class="com.struts2.LoginAuthentication"
method="logout">
<interceptor-ref name="clear-cache" />
<result name="success">/Index.jsp</result>
<result name="error">/error.jsp</result>
</action>
代码来自 Action
class:
public String logout() {
//if (session instanceof org.apache.struts2.dispatcher.SessionMap) {
session.clear();
//session.re
System.out.println("test");
session.remove("loggedInUser");
((org.apache.struts2.dispatcher.SessionMap) session).invalidate();
//}
return "success";
}
注销后它被重定向到 Index.jsp
,现在我点击了后退按钮
它在 chrome 中显示 "confirm form resubmission"
消息,并且网页在 IE 中过期。但是当我重新加载页面时,它会自动登录旧用户。
我已添加
<%
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Expires", "0");
response.setHeader("Vary", "*");
%>
在 JSP 以及拦截器中。
我正在尝试阻止重新加载时自动登录。
问题是在注销后您实际上并没有重定向到新的操作。这种行为的原因是当您按下后退按钮时,您会在浏览器中看到一个确认对话框。后退按钮不用于调用操作,除非它不是通过使用 Ajax 触发它来调用的。在请求注销时,您应该遵循 post-redirect-get 模式。
<action name="logout" class="com.struts2.LoginAuthentication" method="logout">
<interceptor-ref name="clear-cache" />
<result name="success" type="redirect">/</result>
<result name="error">/error.jsp</result>
</action>
我通过将问题重定向到另一个操作解决了这个问题。并编写了另一个拦截器来验证它是否在会话中有用户 ID。
<default-interceptor-ref name="loginStack"></default-interceptor-ref>
<action name="login" class="com.struts2.LoginAuthentication"
method="execute">
<interceptor-ref name="defaultStack"/>
<result name="manager" type="redirectAction">
<param name="actionName">home</param>
</result>
</action>
<action name="home" class="com.struts2.LoginAuthentication"
method="home">
<result name="success">/ManagerView.jsp</result>
<result name="error">/error.jsp</result>
</action>
我有一个登录页面(Index.jsp
),用户在这里输入用户名和密码。在提交 LoginAuthentification.java
(action class) 时调用并验证用户,但根据操作 class 中的结果,它 returns JSP.
<action name="login" class="com.struts2.LoginAuthentication"
method="execute">
<interceptor-ref name="clear-cache" />
<result name="manager">/ManagerView.jsp</result>
<result name="SSE" type="redirectAction">
<param name="actionName">viewPlan</param>
<param name="userID">${userID}</param>
</result>
<result name="input">/Index.jsp</result>
<result name="error">/error.jsp</result>
</action>
在这种情况下,它返回 ManagerView.jsp
。现在在这个 JSP 中,我为 logout
添加了一个超链接,它在下面做
<action name="logout" class="com.struts2.LoginAuthentication"
method="logout">
<interceptor-ref name="clear-cache" />
<result name="success">/Index.jsp</result>
<result name="error">/error.jsp</result>
</action>
代码来自 Action
class:
public String logout() {
//if (session instanceof org.apache.struts2.dispatcher.SessionMap) {
session.clear();
//session.re
System.out.println("test");
session.remove("loggedInUser");
((org.apache.struts2.dispatcher.SessionMap) session).invalidate();
//}
return "success";
}
注销后它被重定向到 Index.jsp
,现在我点击了后退按钮
它在 chrome 中显示 "confirm form resubmission"
消息,并且网页在 IE 中过期。但是当我重新加载页面时,它会自动登录旧用户。
我已添加
<%
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Expires", "0");
response.setHeader("Vary", "*");
%>
在 JSP 以及拦截器中。
我正在尝试阻止重新加载时自动登录。
问题是在注销后您实际上并没有重定向到新的操作。这种行为的原因是当您按下后退按钮时,您会在浏览器中看到一个确认对话框。后退按钮不用于调用操作,除非它不是通过使用 Ajax 触发它来调用的。在请求注销时,您应该遵循 post-redirect-get 模式。
<action name="logout" class="com.struts2.LoginAuthentication" method="logout">
<interceptor-ref name="clear-cache" />
<result name="success" type="redirect">/</result>
<result name="error">/error.jsp</result>
</action>
我通过将问题重定向到另一个操作解决了这个问题。并编写了另一个拦截器来验证它是否在会话中有用户 ID。
<default-interceptor-ref name="loginStack"></default-interceptor-ref>
<action name="login" class="com.struts2.LoginAuthentication"
method="execute">
<interceptor-ref name="defaultStack"/>
<result name="manager" type="redirectAction">
<param name="actionName">home</param>
</result>
</action>
<action name="home" class="com.struts2.LoginAuthentication"
method="home">
<result name="success">/ManagerView.jsp</result>
<result name="error">/error.jsp</result>
</action>