写一个用Filter进行用户授权的例子即过滤request

---恢复内容开始---

  1 LoginFilter.java 
  3 代码如下:
  4 public class LoginFilter implements Filter {  
  5     private String permitUrls[] = null; 
  6 
  7     private String gotoUrl = null; 
  8 
  9     public void destroy() { 
 10 
 11         // TODO Auto-generated method stub 
 12 
 13         permitUrls = null; 
 14 
 15         gotoUrl = null; 
 16 
 17     } 
 18 
 19     public void doFilter(ServletRequest request, ServletResponse response, 
 20 
 21             FilterChain chain) throws IOException, ServletException { 
 22 
 23         // TODO Auto-generated method stub 
 24 
 25         HttpServletRequest res=(HttpServletRequest) request; 
 26 
 27         HttpServletResponse resp=(HttpServletResponse)response; 
 28 
 29         if(!isPermitUrl(request)){ 
 30 
 31             if(filterCurrUrl(request)){ 
 32 
 33                 System.out.println("--->请登录"); 
 34 
 35                 resp.sendRedirect(res.getContextPath()+gotoUrl); 
 36 
 37                 return; 
 38 
 39             } 
 40 
 41         } 
 42 
 43         System.out.println("--->允许访问"); 
 44 
 45         chain.doFilter(request, response); 
 46 
 47     } 
 48 
 49     public boolean filterCurrUrl(ServletRequest request){ 
 50 
 51         boolean filter=false; 
 52 
 53         HttpServletRequest res=(HttpServletRequest) request; 
 54 
 55         User user =(User) res.getSession().getAttribute("user"); 
 56 
 57         if(null==user) 
 58 
 59             filter=true; 
 60 
 61         return filter;  
 62     }       
 63 
 64     public boolean isPermitUrl(ServletRequest request) { 
 65 
 66         boolean isPermit = false; 
 67 
 68         String currentUrl = currentUrl(request); 
 69 
 70         if (permitUrls != null && permitUrls.length > 0) { 
 71 
 72             for (int i = 0; i < permitUrls.length; i++) { 
 73 
 74                 if (permitUrls[i].equals(currentUrl)) { 
 75 
 76                     isPermit = true; 
 77 
 78                     break; 
 79 
 80                 } 
 81 
 82             } 
 83 
 84         } 
 85 
 86         return isPermit; 
 87 
 88     }        
 89 
 90     //请求地址 
 91 
 92     public String currentUrl(ServletRequest request) {   
 93 
 94         HttpServletRequest res = (HttpServletRequest) request; 
 95 
 96         String task = request.getParameter("task"); 
 97 
 98         String path = res.getContextPath(); 
 99 
100         String uri = res.getRequestURI(); 
101 
102         if (task != null) {// uri格式 xx/ser 
103 
104             uri = uri.substring(path.length(), uri.length()) + "?" + "task="
105 
106                     + task; 
107 
108         } else { 
109 
110             uri = uri.substring(path.length(), uri.length()); 
111 
112         } 
113 
114         System.out.println("当前请求地址:" + uri); 
115 
116         return uri; 
117 
118     } 
119 
120     public void init(FilterConfig filterConfig) throws ServletException { 
121 
122         // TODO Auto-generated method stub 
123 
124         String permitUrls = filterConfig.getInitParameter("permitUrls"); 
125 
126         String gotoUrl = filterConfig.getInitParameter("gotoUrl"); 
127 
128   
129 
130         this.gotoUrl = gotoUrl; 
131 
132   
133 
134         if (permitUrls != null && permitUrls.length() > 0) { 
135 
136             this.permitUrls = permitUrls.split(","); 
137 
138         } 
139 
140     } 
141 
142 }
143 
144 Web.xml
145 
146  代码如下:
147 <filter>  
148     <filter-name>loginFilter</filter-name> 
149 
150     <filter-class>filter.LoginFilter</filter-class> 
151 
152   
153 
154     <init-param> 
155 
156         <param-name>ignore</param-name> 
157 
158         <param-value>false</param-value> 
159 
160     </init-param> 
161 
162     <init-param> 
163 
164         <param-name>permitUrls</param-name> 
165 
166         <param-value>/,/servlet/Loginservlet?task=login,/public.jsp,/login.jsp</param-value> 
167 
168     </init-param> 
169 
170     <init-param> 
171 
172         <param-name>gotoUrl</param-name> 
173 
174         <param-value>/login.jsp</param-value> 
175 
176     </init-param> 
177 
178 </filter> 
179 
180 <filter-mapping> 
181 
182     <filter-name>loginFilter</filter-name> 
183 
184     <url-pattern>/*</url-pattern> 
185 
186 </filter-mapping>

 

---恢复内容结束---

posted @ 2018-04-02 15:34  赵艳、Z  阅读(146)  评论(0编辑  收藏  举报