spring boot 使用過濾器
阿新 • • 發佈:2017-08-02
servlet str logs real pro onf keyword sca troy
//啟動類添加註解
@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; importjavax.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 implementsFilter { // 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 使用過濾器