/* *作者:呆萌老师 *☑csdn认证讲师 *☑51cto高级讲师 *☑腾讯课堂认证讲师 *☑网易云课堂认证讲师 *☑华为开发者学堂认证讲师 *☑爱奇艺千人名师计划成员 *在这里给大家分享技术、知识和生活 *各种干货,记得关注哦! *vx:it_daimeng */
过滤器是处于客户端与服务器资源文件之间的一道过滤网,在访问资源文件之前,通过一系列的过滤器对请求进行修改、判断等,把不符合规则的请求在中途拦截或修改。也可以对响应进行过滤,拦截或修改响应。
Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。
它主要用于对用户请求进行预处理,也可以对HttpServletResponse 进行后处理。使用Filter 的完整流程:Filter 对用户请求进行预处理,接着将请求交给Servlet 进行处理并生成响应,最后Filter 再对服务器响应进行后处理。
过滤器实现自动登录
package com.test.filter; import java.io.IOException; import java.net.URLDecoder; import java.sql.SQLException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.test.model.User; public class AutoLoginFilter implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; HttpSession session = req.getSession(); //获得cookie中用户名和密码 进行登录的操作 //定义cookie_username String cookie_username = null; //定义cookie_password String cookie_password = null; //获得cookie Cookie[] cookies = req.getCookies(); if(cookies!=null){ for(Cookie cookie : cookies){ //获得名字是cookie_username和cookie_password if("username".equals(cookie.getName())){ cookie_username = cookie.getValue(); //恢复中文用户名 cookie_username = URLDecoder.decode(cookie_username, "UTF-8"); System.out.println("拿到用户名"); } if("password".equals(cookie.getName())){ cookie_password = cookie.getValue(); System.out.println("拿到密码"); } } } //判断username和password是否是null if(cookie_username!=null&&cookie_password!=null){ System.out.println(cookie_username); System.out.println(cookie_password); //登录的代码 if(cookie_username.equals("zhangsan") && cookie_password.equals("123")) { //User user=new User(cookie_username,"127.0.0.1"); //将登录的用户的user对象存到session中 session.setAttribute("login_user", "zhangsan"); System.out.println("放行"); } } //放行 chain.doFilter(req, resp); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } } package com.test.filter; import java.io.IOException; import java.net.URLDecoder; import java.sql.SQLException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.test.model.User; public class AutoLoginFilter implements Filter{ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; HttpSession session = req.getSession(); //获得cookie中用户名和密码 进行登录的操作 //定义cookie_username String cookie_username = null; //定义cookie_password String cookie_password = null; //获得cookie Cookie[] cookies = req.getCookies(); if(cookies!=null){ for(Cookie cookie : cookies){ //获得名字是cookie_username和cookie_password if("username".equals(cookie.getName())){ cookie_username = cookie.getValue(); //恢复中文用户名 cookie_username = URLDecoder.decode(cookie_username, "UTF-8"); System.out.println("拿到用户名"); } if("password".equals(cookie.getName())){ cookie_password = cookie.getValue(); System.out.println("拿到密码"); } } } //判断username和password是否是null if(cookie_username!=null&&cookie_password!=null){ System.out.println(cookie_username); System.out.println(cookie_password); //登录的代码 if(cookie_username.equals("zhangsan") && cookie_password.equals("123")) { //User user=new User(cookie_username,"127.0.0.1"); //将登录的用户的user对象存到session中 session.setAttribute("login_user", "zhangsan"); System.out.println("放行"); } } //放行 chain.doFilter(req, resp); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
注册:
<filter> <filter-name>AutoLoginFilter</filter-name> <filter-class>com.test.filter.AutoLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>AutoLoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>