多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 1.字符编码过滤器类 ``` package com.baishenghua200.filter; import java.io.IOException; 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.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * CharacterEncodingFilter300.java(字符编码过滤器类) * @desc 主要统一字符编码 * @author 柏圣华 * @date 2021-10-21 * */ @WebFilter(urlPatterns="/*",initParams= {@WebInitParam(name="CharsetEncoding",value="utf-8")}) public class CharacterEncodingFilter200 implements Filter { private static String encoding;//定义变量接收初始化的值 /** * Default constructor. */ public CharacterEncodingFilter200() { } public void destroy() { } public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)res; //设置字符编码链锁 request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); if(encoding != null) { chain.doFilter(request, response); } } public void init(FilterConfig fConfig) throws ServletException { //接收web.xml配置文件中或者注解中的初始参数 encoding = fConfig.getInitParameter("CharsetEncoding"); } } ``` ### 2.用户登录权限验证过滤器类 ``` package com.baishenghua200.filter; import java.io.IOException; 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.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; @WebFilter("/*") public class LoginFilter200 implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { System.out.println(req); // 0.强制转换 HttpServletRequest request = (HttpServletRequest) req; // 1.获取资源请求路径 String uri = request.getRequestURI(); // 2.判断是否包含登录相关资源路径,要注意排除掉 css/js/图片/验证码等资源 if (uri.contains("/login.jsp") || uri.contains("/LoginServlet200") || uri.contains("/css/") || uri.contains("/js/") || uri.contains("/fonts/") || uri.contains("/images/") || uri.contains("/CheckCode200") || uri.contains("/IndexServlet200") || uri.contains("/index.jsp") || uri.contains("/index200.jsp") || uri.contains("/about.jsp") || uri.contains("/news.jsp") || uri.contains("/contact.jsp") || uri.contains("/portfolio.jsp") || uri.contains("/product.jsp") || uri.contains("/server.jsp") || uri.contains("/talent.jsp") || uri.contains("/online.jsp")) { // 包含,用户就是想登录。放行 chain.doFilter(req, resp); } else { // 不包含,需要验证用户是否登录 // 3.从获取session中获取user Object user = request.getSession().getAttribute("user"); if (user != null) { // 登录了。放行 chain.doFilter(request, resp); } else { // 没有登录。跳转登录页面 request.setAttribute("login_msg", "您尚未登录,请登录"); request.getRequestDispatcher("/Manager/login.jsp").forward(request, resp); } } } public void init(FilterConfig fConfig) throws ServletException { } } ``` ### 3.敏感词汇过滤器类 ``` package com.baishenghua200.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List; /** * 敏感词汇过滤器 */ @WebFilter("/*") public class SensitiveWordsFilter200 implements Filter { public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { //1.创建代理对象,增强getParameter方法 ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //增强getParameter方法 //判断是否是getParameter方法 if(method.getName().equals("getParameter")){ //增强返回值 //获取返回值 String value = (String) method.invoke(req,args); if(value != null){ for (String str : list) { if(value.contains(str)){ value = value.replaceAll(str,"***"); } } } return value; } //判断方法名是否是 getParameterMap //判断方法名是否是 getParameterValue return method.invoke(req,args); } }); //2.放行 chain.doFilter(proxy_req, resp); } private List<String> list = new ArrayList<String>();//敏感词汇集合 public void init(FilterConfig config) throws ServletException { try{ //1.获取文件真实路径 ServletContext servletContext = config.getServletContext(); String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感词汇.txt"); //2.读取文件 BufferedReader br = new BufferedReader(new FileReader(realPath)); //3.将文件的每一行数据添加到list中 String line = null; while((line = br.readLine())!=null){ list.add(line); } br.close(); System.out.println(list); }catch (Exception e){ e.printStackTrace(); } } public void destroy() { } } ```