首页 > 代码库 > spring boot 使用过滤器

spring boot 使用过滤器

//启动类添加注解
@ServletComponentScan
package com.baiwang.invoice.utils;import java.io.IOException;import java.util.HashMap;import java.util.Map;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;/** * Created by ChengLuchao on 2017/8/1. */
// 过滤器添加注解
@WebFilter(filterName="myFilter",urlPatterns="/*")public class KeyWordFilter implements Filter {// private FilterConfig filterConfig; public static HashMap keyMap = null; public static String path; @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("-----------------------------------------启动");// this.filterConfig = filterConfig;// String keyWordPath = filterConfig.getInitParameter("key"); path = filterConfig.getServletContext().getRealPath("keyword.properties"); System.out.println(path); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("----------------------------------------执行"); HttpServletRequest req = (HttpServletRequest) request;// if (keyMap == null) {// keyMap = (HashMap) PropertiesUtil.readProperties(path);// } keyMap = new HashMap(); keyMap.put("ccc","clclclclclcl"); if (req.getMethod().equals("POST")) { chain.doFilter(new KeyWordRequestWrapper(req, keyMap), response); Map map = request.getParameterMap(); for (Object key: map.keySet()) { if (keyMap.get(key) != null) { map.put(keyMap.get(key),map.get(key)); map.remove(key); } } request.setAttribute("map",map); } else { chain.doFilter(request, response); } } @Override public void destroy() { // this.filterConfig = null; System.out.println("----------------------------------------销毁"); }}

 

spring boot 使用过滤器