注销

时间:2017-01-25 10:15:43

标签: javascript jquery spring spring-mvc

当我从应用程序注销时,我正在执行spring mvc应用程序如果我单击浏览器后退按钮它到应用程序的上一页我必须限制这个并且我在我的* .xml文件中尝试了这个

      <interceptors>
            <beans:bean id="webContentInterceptor"
              class="org.springframework.web.servlet.mvc.WebContentInterceptor">
              <beans:property name="cacheSeconds" value="0" />
              <beans:property name="useExpiresHeader" value="true" />
              <beans:property name="useCacheControlHeader" value="true" />
              <beans:property name="useCacheControlNoStore" value="true" />
            </beans:bean>
          </interceptors>
如果我点击浏览器后退按钮进入上一页,如果我再次注销,那么这只适用于应用程序。

我尝试使用Javascript和Jquery示例,但没有在我的spring mvc应用程序中工作。 任何人都可以在这方面建议我和遵循的方法。

1 个答案:

答案 0 :(得分:1)

好的,我在这里告诉你如何做到这一点。 假设您在SessionFilter包中有一个com.test.filter类来实现Filter。在这里,您覆盖doFilter()方法并检查会话是否存在,如下:

public class SessionFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
                FilterChain chain) throws IOException,
                ServletException {

            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
            String url = request.getRequestURI();

            response.setHeader("pragma", "no-cache");              
            response.setHeader("Cache-control", "no-cache, no-store, must-revalidate");             
            response.setHeader("Expires", "0"); 
            HttpSession session = request.getSession(false);

            //!url.contains("login.html") check if the requested page is login page or not. you can do it a numerous way.
            // but for simpplicity here i do that
            if(session==null && !url.contains("login.html")) {
                response.sendRedirect(request.getContextPath() + "/login.html"); // here goto http://yourdoamin/login.html
                response.setHeader("message", "Session Timeout."); // you can set your preffered message.
                return; //break filter chain, requested JSP/servlet will not be executed
            }

            chain.doFilter(req, res);
        }
}

现在将类引用添加到web.xml

<filter>
    <description>session filter</description>
    <filter-name>SessionFilter</filter-name>
    <filter-class>com.test.filter.SessionFilter</filter-class>
</filter>