Session timeout upon refresh at login page

馋奶兔 提交于 2019-12-12 17:15:31

问题


I made a really simple login and session structure for reuse in my future JSP based applications. It's like this:

web.xml (the 1 minute timeout is to test my problem):

<session-config>
 <session-timeout>1</session-timeout>
</session-config>

<filter>
 <filter-name>Access</filter-name>
 <filter-class>com.app.Access</filter-class>
</filter>

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

<servlet>
 <servlet-name>Login</servlet-name>
 <servlet-class>com.app.Login</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>Login</servlet-name>
 <url-pattern>/login</url-pattern>
</servlet-mapping>

Access.java filter:

// Check if the page's the login or if the user logged, else asks login
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    boolean logged = httpRequest.getSession(false) != null && httpRequest.getSession().getAttribute("user") != null;
    if (httpRequest.getServletPath().equals("/login") || logged)
        chain.doFilter(request, response);
    else
        ((HttpServletResponse) response).sendRedirect(httpRequest.getContextPath() + "/login");
}

Login.java servlet (authentication shortened for test):

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (request.getRequestedSessionId() != null && !request.isRequestedSessionIdValid())
        request.setAttribute("failure", "session timeout");
    request.getSession().setAttribute("user", null);
    request.getRequestDispatcher("login.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getSession().setAttribute("user", new User());
    response.sendRedirect("");
}

And the login.jsp page, at the root of WebContent, has a <form action="login" method="post"> form with appropriated innerHTML for authentication and a ${failure} field to receive a session timeout or a login failed message.

This structure works perfectly for me. It intercepts, asks for login, checks both session and authentication, etc., but there's a small flaw: if you're at the login page and refresh it (either F5 or pressing Enter at the URL) after the timeout, the page receives and shows the "session timeout" message in ${failure}.

I found no real working way yet to make it know that the previous page was the login page. Tried about five different ways without success, including request.getHeader("Referer") and the lastWish tag library.


回答1:


One way is to let your publicly accessible JSPs (such as the login page) to not create the session at all. Requesting a JSP page namely implicitly creates the session by default. This can be achieved by adding the following line to top of JSP:

<%@page session="false" %>

This way request.getRequestedSessionId() will return null and thus the timeout check will be bypassed. The session will this way then only be created when you actually login the user. I'd only remove the following line from the servlet since that makes no sense and would still create the session:

request.getSession().setAttribute("user", null);



回答2:


I Just do it like this:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpReq = (HttpServletRequest)request;
    String servletPath = httpReq.getServletPath();
    HttpSession session = httpReq.getSession();
    String redirectUrl = "/login.jsp";
    if (
            (servletPath.endsWith("login.jsp")) ||
            (servletPath.endsWith("rss.html")) ||
            (servletPath.endsWith("httperror403.html")) ||
            (servletPath.endsWith("httperror500.html")) ||
            (servletPath.endsWith("imageMark.do"))||
            (servletPath.indexOf("/api.do") != -1) ||
            (servletPath.indexOf("/help/") != -1)){
        chain.doFilter(request, response);
    }  else if (session == null) {
        httpReq.getRequestDispatcher(redirectUrl).forward(request, response);
    } else {
        SystemUser user = (SystemUser)session.getAttribute("user");
        if (user == null){
            if (session != null){
                session.invalidate();
            }
            httpReq.getRequestDispatcher(redirectUrl).forward(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }
}


来源:https://stackoverflow.com/questions/11278913/session-timeout-upon-refresh-at-login-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!