码迷,mamicode.com
首页 > 其他好文 > 详细

利用Filter实现session拦截

时间:2014-12-28 19:30:25      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

1、在web.xml中配置

    <!-- Session监听器 -->
    <filter>
        <filter-name>sessionValidateFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sessionValidateFilter</filter-name>
        <url-pattern>/admin/*</url-pattern>
    </filter-mapping>

2、在applicationContent.xml中配置

    <!-- 配置登录验证拦截器 -->
    <bean id="sessionValidateFilter" class="cn.jxufe.core.filter.SessionValidateFilter"></bean>
    

3、创建自定义过滤器类

public class SessionValidateFilter implements Filter {
    public void destroy() {}

    public void doFilter(ServletRequest servletRequest,
            ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        if ((session == null) || (session.getAttribute("username") == null)) {
            String PATH=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
            response.sendRedirect(PATH+"login.jsp");
        }
        chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) throws ServletException {}

}

 

利用Filter实现session拦截

标签:

原文地址:http://www.cnblogs.com/hehaiyang/p/4190250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!