验证中...
Languages: Java
Categories: Web开发技术
Latest update 2019-09-11 16:56
ModifyParametersFilter
Raw Copy
import java.io.IOException;
import java.util.*;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zgn.api.common.utils.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;
public class ModifyParametersFilter extends OncePerRequestFilter {
private Logger logger = LoggerFactory.getLogger(ModifyParametersFilter.class);
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String ip = IpUtils.getIpAddress(request);
ModifyParametersWrapper mParametersWrapper = new ModifyParametersWrapper(request, ip);
filterChain.doFilter(mParametersWrapper, response);
}
/**
* 继承HttpServletRequestWrapper,创建装饰类,以达到修改HttpServletRequest参数的目的
*/
private class ModifyParametersWrapper extends HttpServletRequestWrapper {
private Map<String, String[]> parameterMap = new HashMap<>(); // 所有参数的Map集合
private String ip;
public ModifyParametersWrapper(HttpServletRequest request, String ip) {
super(request);
parameterMap.putAll(request.getParameterMap());
this.ip = ip;
if (parameterMap.get("param") == null) {
parameterMap.put("param", new String[]{"{\"ip\":\""+ this.ip +"\"}"});
}
}
// 重写几个HttpServletRequestWrapper中的方法
/**
* 获取所有参数名
*
* @return 返回所有参数名
*/
@Override
public Enumeration<String> getParameterNames() {
Vector<String> vector = new Vector<String>(parameterMap.keySet());
return vector.elements();
}
/**
* 获取指定参数名的值,如果有重复的参数名,则返回第一个的值 接收一般变量 ,如text类型
*
* @param name
* 指定参数名
* @return 指定参数名的值
*/
@Override
public String getParameter(String name) {
String[] results = parameterMap.get(name);
if (results == null || results.length <= 0)
return null;
else {
return modify(results[0], name);
}
}
/**
* 获取指定参数名的所有值的数组,如:checkbox的所有数据
* 接收数组变量 ,如checkobx类型
*/
@Override
public String[] getParameterValues(String name) {
String[] results = parameterMap.get(name);
if (results == null || results.length <= 0)
return new String[]{"{\"ip\":\""+ this.ip +"\"}"};
else {
int length = results.length;
for (int i = 0; i < length; i++) {
results[i] = modify(results[i], name);
}
return results;
}
}
/**
* 自定义的一个简单修改原参数的方法,即:给原来的参数值前面添加了一个修改标志的字符串
*
* @param string
* 原参数值
* @return 修改之后的值
*/
private String modify(String string, String param) {
if ("param".equals(param)) {
JSONObject json = JSON.parseObject(string);
if (!json.containsKey("ip")) {
StringBuffer stringBuilder=new StringBuffer(string);
stringBuilder.insert(string.length()-1,",\"ip\":\""+ this.ip +"\"");
string = stringBuilder.toString();
}
}
return string;
}
}
}
import com.zgn.api.common.filter.*;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import javax.servlet.Filter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class FilterConfig {
/**
* 拦截器注册
*
*
*/
@Bean
public Filter parametersFilter(){
return new ModifyParametersFilter();
}
@Bean
public FilterRegistrationBean paramsRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(parametersFilter());
registration.addUrlPatterns("/*");// 拦截路径
registration.setName("ParametersFilter");// 拦截器名称
registration.setOrder(9);// 顺序
return registration;
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search