gpt4 book ai didi

java - Struts2 - SessionMonitor - 页面未正确重定向

转载 作者:行者123 更新时间:2023-12-02 07:22:30 25 4
gpt4 key购买 nike

我开发了struts2 Web应用程序。我需要做 session 监视器。如果用户没有登录系统/或者 session 超时,则需要自动重定向到登录页面。

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter>
<filter-name>SessionMonitor</filter-name>
<filter-class>com.xont.tracker.security.filters.ApplicationSessionMonitor</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>SessionMonitor</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<description>This listener class will listen to user session and assigns session for 30 minutes </description>
<listener-class>com.xont.tracker.listeners.SessionListener</listener-class>
</listener>

<listener>
<description>This listener class will listen for context listeners </description>
<listener-class>com.xont.tracker.listeners.ContextListener</listener-class>
</listener>

<context-param>
<param-name>401</param-name>
<param-value>Not Authorized</param-value>
</context-param>

<context-param>
<param-name>401:timeout</param-name>
<param-value>Your session timed out. Please login again</param-value>
</context-param>

<context-param>
<param-name>300</param-name>
<param-value>You don't have permission to access the page</param-value>
</context-param>

<context-param>
<param-name>403</param-name>
<param-value>You must login to continue</param-value>
</context-param>

<error-page>
<error-code>404</error-code>
<location>/error_pages/error_404.jsp</location>
</error-page>

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error_pages/unexpected_error.jsp</location>
</error-page>

<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>

这是我的 ApplicationSessionMonitor

 public class ApplicationSessionMonitor extends ApplicationMonitor {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
try {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpSession session = request.getSession();
Object user = session.getAttribute("loged-user");

if (user == null) {
buildTempPath(session, requestedPath(request));
((HttpServletResponse) servletResponse).sendRedirect(contextPath);
} else {
goAhead(servletRequest, servletResponse, chain);
}
} catch (Exception e) {
e.printStackTrace();;
}
}
}

这是我的 struts.xml

<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.xont.tracker.login.action.LoginAction">
<result name="success" type="redirect">index.jsp</result>
<result name="temp_url" type="redirect">${temp_url}</result>
<result name="error">login.jsp</result>
<result name="input">login.jsp</result>
</action> .....

总是显示页面未正确重定向。它无法正确重定向。请有人指导我出了什么问题?

最佳答案

我无法帮助您调试应用程序监视器,但如果您使用 Struts2,为什么不使用 Struts2 组件,例如 Interceptors (也是基于拦截过滤技术)?

我已经编写了一个拦截器来完全执行您所描述的操作,请随意 read the post .

希望有帮助

关于java - Struts2 - SessionMonitor - 页面未正确重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com