Fork me on GitHub

Java web过滤器验证登录(避免未经登录进入主页)

    今天用ssh2写了个简单的系统,发现了一个问题,我这系统必须先登录成功才能进入主页,但我在浏览器里直接输入主页地址,发现也能进入,这个肯定不好,毫无安全性可言,后经查资料发现需要登录过滤器,就试了下,发现果然可以避免未经登录即可进入主页的危险,下面是我整理出的详细步骤:

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>
posted @ 2012-08-05 01:20  Alexia(minmin)  阅读(65775)  评论(4编辑  收藏  举报
无觅关联推荐,快速提升流量