Java web登录验证过滤器

1.首先写一个权限过滤filter类,实现Filter接口

复制代码
 1 import java.io.IOException;
 2 
 3 import javax.servlet.Filter;
 4 import javax.servlet.FilterChain;
 5 import javax.servlet.FilterConfig;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.ServletRequest;
 8 import javax.servlet.ServletResponse;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 
13 public class LoginFilter implements Filter {
14 
15     @Override
16     public void init(FilterConfig filterConfig) throws ServletException {
17         // TODO Auto-generated method stub
18 
19     }
20 
21     @Override
22     public void doFilter(ServletRequest request, ServletResponse response,
23             FilterChain chain) throws IOException, ServletException {
24         // 获得在下面代码中要用的request,response,session对象
25         HttpServletRequest servletRequest = (HttpServletRequest) request;
26         HttpServletResponse servletResponse = (HttpServletResponse) response;
27         HttpSession session = servletRequest.getSession();
28 
29         // 获得用户请求的URI
30         String path = servletRequest.getRequestURI();
31         //System.out.println(path);
32         
33         // 从session里取员工工号信息
34         String empId = (String) session.getAttribute("empId");
35 
36         /*创建类Constants.java,里面写的是无需过滤的页面
37         for (int i = 0; i < Constants.NoFilter_Pages.length; i++) {
38 
39             if (path.indexOf(Constants.NoFilter_Pages[i]) > -1) {
40                 chain.doFilter(servletRequest, servletResponse);
41                 return;
42             }
43         }*/
44         
45         // 登陆页面无需过滤
46         if(path.indexOf("/login.jsp") > -1) {
47             chain.doFilter(servletRequest, servletResponse);
48             return;
49         }
50 
51         // 判断如果没有取到员工信息,就跳转到登陆页面
52         if (empId == null || "".equals(empId)) {
53             // 跳转到登陆页面
54             servletResponse.sendRedirect("/JingXing_OA/login.jsp");
55         } else {
56             // 已经登陆,继续此次请求
57             chain.doFilter(request, response);
58         }
59 
60     }
61 
62     @Override
63     public void destroy() {
64         // TODO Auto-generated method stub
65 
66     }
67 
68 }
复制代码

2.然后在web.xml里配置需要登陆权限验证的JSP文件:
     a.如果是某个具体的JSP文件(如a.jsp)需要登陆验证

复制代码
 1 <!-- 配置登陆过滤器 -->
 2 <filter>
 3         <filter-name>login</filter-name>
 4         <filter-class>com.jingxing.oa.filter.LoginFilter</filter-class>
 5     </filter>
 6 
 7     <filter-mapping>
 8         <filter-name>login</filter-name>
 9         <url-pattern>/*</url-pattern>
10 </filter-mapping>
复制代码

b.如果是某一个目录(如a/目录)整个目录下的文件都需要登陆验证:

复制代码
 1 <!-- 配置登陆过滤器 -->
 2     <filter>
 3         <filter-name>login</filter-name>
 4         <filter-class>com.jingxing.oa.filter.LoginFilter</filter-class>
 5     </filter>
 6 
 7     <filter-mapping>
 8         <filter-name>login</filter-name>
 9         <url-pattern>/a/*</url-pattern>
10     </filter-mapping>
复制代码

你可能感兴趣的:(登录,验证,过滤器)