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 {} }
时间: 2024-10-29 04:34:04