码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot 使用过滤器

时间:2017-08-02 19:06:06      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:str   config   except   color   name   let   object   spring   rem   

//启动类添加注解
@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 使用过滤器

标签:str   config   except   color   name   let   object   spring   rem   

原文地址:http://www.cnblogs.com/chenglc/p/7275223.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!