Spring Mvc过滤器Filter实现登陆验证

过滤器实现用户登陆判断,如果用户登陆成功跳转到首页,如果用户未登陆,跳转到登陆页面。

 1.用户登陆

   首先我们天添加两个页面,一个是登陆页面login.jsp,一个是登陆成功后的index.jsp.

在login.jsp中我们添加两个用户名和密码文本输入文本框

 index.jsp

 

[html]  view plain  copy
  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.  欢迎你,登陆成功  
  11. </body>  
  12. </html>  


login.jsp

[html]  view plain  copy
  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.    <form action="<%=request.getContextPath()%>/Home/userLogin"  method="post">  
  11.    用户名:<input type="text" name="username">   
  12.    <Br>  
  13.   密码: <input type="password" name="pwd">   
  14.     
  15.   <input type="submit" value="登陆">  
  16.     
  17.   </form>  
  18.     
  19.     
  20. </body>  
  21. </html>  


 在后台我们添加controller方法

扫描二维码关注公众号,回复: 294527 查看本文章
[java]  view plain  copy
  1. @Controller  
  2. @RequestMapping("/Home")  
  3. public class HomeController {  
  4.   
  5.           
  6.     @RequestMapping(value="index")  
  7.     public String Index()  
  8.     {  
  9.                   
  10.         return "index";  
  11.     }  
  12.       
  13.       
  14.     @RequestMapping(value="login")  
  15.     public String Login()  
  16.     {  
  17.         return "login";  
  18.           
  19.     }  
  20.       
  21.     @RequestMapping(value="userLogin")  
  22.     public String UserLogin(HttpServletRequest request,HttpServletResponse respnose,HttpSession httpSession) throws IOException  
  23.     {  
  24.         //登陆成功后,写入session  
  25.         String userName=request.getParameter("username");  
  26.         String pwd=request.getParameter("pwd");  
  27.         System.out.println("登陆成功!"+"--"+userName);  
  28.         httpSession.setAttribute("username", userName);  
  29.         return "redirect:/Home/index";    
  30.     }  
  31. }  
  32. }  

   我们添加过滤器filterFirst

[java]  view plain  copy
  1. package com.filter;  
  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. public class filterFirst implements Filter {  
  15.   
  16.     private  FilterConfig config;  
  17.       
  18.     public void destroy() {  
  19.         // TODO Auto-generated method stub  
  20.          System.out.println("destroy");  
  21.     }  
  22.   
  23.     public void doFilter(ServletRequest arg0, ServletResponse arg1,  
  24.             FilterChain arg2) throws IOException, ServletException {  
  25.         // TODO Auto-generated method stub  
  26.          System.out.println("doFilter");  
  27.            
  28.          HttpServletRequest request=(HttpServletRequest)arg0;  
  29.            
  30.          HttpServletResponse response=(HttpServletResponse)arg1;  
  31.            
  32.          //获取初始化参数  
  33.         String para=config.getInitParameter("nofilterpath");  
  34.         System.out.println(para);  
  35.            
  36.          if(request.getRequestURI().indexOf("login")!=-1||request.getRequestURI().indexOf("Home/userLogin")!=-1)  
  37.          {  
  38.              arg2.doFilter(arg0, arg1);  
  39.              return;  
  40.          }  
  41.            
  42.          if(request.getSession().getAttribute("username")==null)  
  43.          {  
  44.                
  45.             response.sendRedirect(request.getContextPath()+"/Home/login");  
  46.          }  
  47.          else  
  48.          {  
  49.                
  50.              arg2.doFilter(arg0, arg1);  
  51.          }  
  52.            
  53.     }  
  54.   
  55.     public void init(FilterConfig arg0) throws ServletException {  
  56.         // TODO Auto-generated method stub  
  57.         System.out.println("init");  
  58.         config=arg0;  
  59.           
  60.     }  
  61.   
  62. }  

配置web.xml

[html]  view plain  copy
  1. <!-- 自定义过滤器 -->  
  2.   <filter>    
  3.       <filter-name>firstfilter</filter-name>    
  4.       <filter-class>com.filter.filterFirst</filter-class>    
  5.       <!-- 设置初始化参数 -->  
  6.        <init-param>    
  7.           <param-name>nofilterpath</param-name>    
  8.           <param-value>login</param-value>    
  9.       </init-param>    
  10.         
  11.   </filter>    
  12.   <filter-mapping>    
  13.       <filter-name>firstfilter</filter-name>    
  14.       <url-pattern>/*</url-pattern>    
  15.   </filter-mapping>    


 2.初始化参数

   在filter中我们可以设置初始化参数,在web.xml中通过<init-parm>来进行设置,param-name表示参数名称,parm-value表示参数值

  在filter中,我们可以获取参数,使用FilterConfig进行获取,在init中进行初始化

[java]  view plain  copy
  1. public void init(FilterConfig arg0) throws ServletException {  
  2.         // TODO Auto-generated method stub  
  3.         System.out.println("init");  
  4.         config=arg0;  
  5.           
  6.     }  

 

 





猜你喜欢

转载自blog.csdn.net/xiaoyangsavvy/article/details/79968657