Spring MVC过滤器-登录过滤

以下代码是继承OncePerRequestFilter实现登录过滤的代码:

 1 package com.test.spring.filter;
 2   
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5   
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10   
11 import org.springframework.web.filter.OncePerRequestFilter;
12   
13 /**
14  * 登录过滤
15  * 
16  * @author geloin
17  * @date 2012-4-10 下午2:37:38
18  */
19 public class SessionFilter extends OncePerRequestFilter {
20   
21     /*
22      * (non-Javadoc)
23      * 
24      * @see
25      * org.springframework.web.filter.OncePerRequestFilter#doFilterInternal(
26      * javax.servlet.http.HttpServletRequest,
27      * javax.servlet.http.HttpServletResponse, javax.servlet.FilterChain)
28      */
29     @Override
30     protected void doFilterInternal(HttpServletRequest request,
31             HttpServletResponse response, FilterChain filterChain)
32             throws ServletException, IOException {
33   
34         // 不过滤的uri
35         String[] notFilter = new String[] { "login.html", "index.html" };
36   
37         // 请求的uri
38         String uri = request.getRequestURI();
39   
40         // uri中包含background时才进行过滤
41         if (uri.indexOf("background") != -1) {
42             // 是否过滤
43             boolean doFilter = true;
44             for (String s : notFilter) {
45                 if (uri.indexOf(s) != -1) {
46                     // 如果uri中包含不过滤的uri,则不进行过滤
47                     doFilter = false;
48                     break;
49                 }
50             }
51             if (doFilter) {
52                 // 执行过滤
53                 // 从session中获取登录者实体
54                 Object obj = request.getSession().getAttribute("loginedUser");
55                 if (null == obj) {
56                     // 如果session中不存在登录者实体,则弹出框提示重新登录
57                     // 设置request和response的字符集,防止乱码
58                     request.setCharacterEncoding("UTF-8");
59                     response.setCharacterEncoding("UTF-8");
60                     PrintWriter out = response.getWriter();
61                     String loginPage = "....";
62                     StringBuilder builder = new StringBuilder();
63                     builder.append("<script type=\"text/javascript\">");
64                     builder.append("alert('网页过期,请重新登录!');");
65                     builder.append("window.top.location.href='");
66                     builder.append(loginPage);
67                     builder.append("';");
68                     builder.append("</script>");
69                     out.print(builder.toString());
70                 } else {
71                     // 如果session中存在登录者实体,则继续
72                     filterChain.doFilter(request, response);
73                 }
74             } else {
75                 // 如果不执行过滤,则继续
76                 filterChain.doFilter(request, response);
77             }
78         } else {
79             // 如果uri中不包含background,则继续
80             filterChain.doFilter(request, response);
81         }
82     }
83   
84 }

写完过滤器后,需要在web.xml中进行配置:

<filter>
    <filter-name>sessionFilter</filter-name>
    <filter-class>com.test.spring.filter.SessionFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>sessionFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

 

posted @ 2016-05-30 16:44  魔流剑  阅读(17232)  评论(0编辑  收藏  举报