1.实现Filter接口
package com.perye.dokit.filters;
import com.perye.dokit.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns={"/app/*"},filterName="corsFilterParameter", asyncSupported = true)//urlPatterns过滤的哪些url
public class CorsFilterParameter implements Filter {
private String filterName;
@Autowired
RedisUtils redisUtils;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse)servletResponse;
HttpServletRequest request = (HttpServletRequest)servletRequest;
String path = ((HttpServletRequest) request).getRequestURI();//请求路径
}
@Override
public void destroy() {
}
}
2.对request中所有参数值进行解密
package com.perye.dokit.filters;
import com.alibaba.fastjson.JSONObject;
import com.perye.dokit.utils.DES3Util;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;
@Slf4j
public class RequestWrapper extends HttpServletRequestWrapper {
Map<String, String[]> params = null;
public RequestWrapper(HttpServletRequest request, Map inParam) {
super(request);
params = new HashMap(inParam);
getJSONParam(params);
}
public void setParameter(String key, String value) {
params.put(key, new String[]{value});
}
public void setParameter(String key, String[] values) {
params.put(key, values);
}
@Override
public String getParameter(String name) {
Object v = params.get(name);
if (v == null) {
return null;
} else if (v instanceof String[]) {
String[] strArr = (String[]) v;
if (strArr.length > 0) {
return strArr[0];
} else {
return null;
}
} else {
return v.toString();
}
}
@Override
public Map<String, String[]> getParameterMap() {
return params;
}
@Override
public Enumeration<String> getParameterNames() {
Vector l = new Vector(params.keySet());
return l.elements();
}
@Override
public String[] getParameterValues(String name) {
return params.get(name);
}
public void getJSONParam(Map<String, String[]> parameterMap){
// Map<String, String[]> parameterMap = request.getParameterMap();
// JSONObject returnObject = new JSONObject();
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
String value = "";
String[] values = entry.getValue();
if (values != null){
for (String s : values) {
value = s + ",";
}
value = value.substring(0, value.length() - 1);
}
value= DES3Util.DESDecrypt(value, "935fc32262a64692adcb126f16b1ebad");//解密
// returnObject.put(entry.getKey(), value);
setParameter(entry.getKey(),value);
}
}
}
3在启动类上注册该过滤器信息
@ServletComponentScan(basePackages = {"com.perye.dokit.filters"})
public class DokitApplication {
springBoot项目Filter中修改过滤器中的参数或者解密加密等操作
最新推荐文章于 2024-10-18 21:16:57 发布