过滤器的使用(权限过滤器)

Posted 初夏的一棵歪脖子树

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了过滤器的使用(权限过滤器)相关的知识,希望对你有一定的参考价值。

1.实体类

技术分享
 1 package com.zdsofe.work;
 2 
 3 public class User {
 4     
 5     
 6     public String userName;
 7     public String password;
 8       
 9     public User(String userName, String password) {
10         
11         this.userName = userName;
12         this.password = password;
13     }
14     public String getUserName() {
15         return userName;
16     }
17     public void setUserName(String userName) {
18         this.userName = userName;
19     }
20     public String getPassword() {
21         return password;
22     }
23     public void setPassword(String password) {
24         this.password = password;
25     }
26 }
View Code

2.web.xml的配置

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>webProject4</display-name>
 4   <filter>
 5   <filter-name>code</filter-name>
 6   <filter-class>com.zdsofe.work.UserFliter</filter-class>
 7   </filter>
 8   <filter-mapping>
 9   <filter-name>code</filter-name>
10   <url-pattern>/pages/*</url-pattern>
11   </filter-mapping>
12   <welcome-file-list>
13     <welcome-file>index.html</welcome-file>
14     <welcome-file>index.htm</welcome-file>
15     <welcome-file>index.jsp</welcome-file>
16     <welcome-file>default.html</welcome-file>
17     <welcome-file>default.htm</welcome-file>
18     <welcome-file>default.jsp</welcome-file>
19   </welcome-file-list>
20 </web-app>
View Code

3.主页面

技术分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 
10 <body>
11 <form action="loginServlet">
12 用户名:<input type="text" name="userName"/><br/>
13 密码:<input type="password" name="password"/><br/>
14 <button type="submit">登录</button>
15 </form>
16 </body>
17 </html>
View Code

4.登录成功页面

技术分享
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <h1>欢迎您!!!</h1>
11 </body>
12 </html>
View Code

5.过滤器

技术分享
 1 package com.zdsofe.work;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 
14 import org.apache.commons.lang.StringUtils;
15 
16 
17 
18 public class UserFliter implements Filter {
19 
20     @Override
21     public void destroy() {
22         // TODO Auto-generated method stub
23 
24     }
25 
26     @Override
27     public void doFilter(ServletRequest arg0, ServletResponse arg1,
28             FilterChain arg2) throws IOException, ServletException {
29         /*//过滤器
30          HttpServletRequest request=(HttpServletRequest)arg0;
31         HttpServletResponse response=( HttpServletResponse)arg1;
32         request.setCharacterEncoding("utf-8");
33         response.setCharacterEncoding("utf-8");
34         response.setContentType("text/html charset=utf-8");        
35         arg2.doFilter(arg0, arg1);*/
36     
37         //权限验证过滤器
38       HttpServletRequest request=(HttpServletRequest)arg0;
39       HttpServletResponse response=( HttpServletResponse)arg1;
40       User user= (User)request.getSession().getAttribute("user");
41       
42  
43       if(null!=user)
44       {
45            String userName=user.getUserName();
46           if(StringUtils.isNotBlank(userName)&&userName.contains("admin"))
47           {
48               System.out.println("登录成功");
49               arg2.doFilter(request, response);
50           } else
51           {
52               response.sendRedirect(request.getContextPath()+"/login.jsp");
53           }           
54        }else
55        {
56             response.sendRedirect(request.getContextPath()+"/login.jsp");
57         } 
58 
59     }
60 
61     @Override
62     public void init(FilterConfig arg0) throws ServletException {
63       
64     }
65 
66 }
View Code

6.服务器

技术分享
 1 package com.zdsofe.work;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.HttpSession;
11 
12 
13 
14 /**
15  * Servlet implementation class loginServlet
16  */
17 @WebServlet("/loginServlet")
18 public class loginServlet extends HttpServlet {
19     private static final long serialVersionUID = 1L;
20        
21     /**
22      * @see HttpServlet#HttpServlet()
23      */
24     public loginServlet() {
25         super();
26         // TODO Auto-generated constructor stub
27     }
28 
29     /**
30      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
31      */
32     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33         /*String name=request.getParameter("userName");
34         response.getWriter().print(name);*/
35         
36         //获取用户名
37         String userName=request.getParameter("userName");
38         //获取密码
39         String password=request.getParameter("password");
40         //创建用户对象
41         User user=new User(userName,password);
42         //创建session对象
43         HttpSession session=request.getSession(true);
44         session.setAttribute("user", user);
45         response.sendRedirect(request.getContextPath()+"/pages/login1.jsp");
46     }
47 
48     /**
49      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
50      */
51     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
52         // TODO Auto-generated method stub
53     }
54 
55 }
View Code

注意:主页面login.jsp直接在webcontent下,登陆成功页面在webcontent的子目录pages下,防止方法重定向死循环。

以上是关于过滤器的使用(权限过滤器)的主要内容,如果未能解决你的问题,请参考以下文章

过滤作为 ViewPager 片段一部分的 ListView

在Pandoc lua过滤器中连接字符串片段

用servlet和过滤器实现简单的权限管理和敏感词过滤功能

用servlet和过滤器实现简单的权限管理和敏感词过滤功能

spring的权限控制,过滤器

在底部导航栏中保存片段状态