首页 > 编程语言 > 详细

spring boot 使用过滤器

时间:2017-08-02 18:29:12      阅读:320      评论:0      收藏:0      [点我收藏+]
//启动类添加注解
@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 使用过滤器

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!