首页 > 代码库 > 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 使用过滤器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。