SpringCloud使用Filter过滤器实现@RequestParam参数加密
一、添加拦截器
@WebFilter中的filterName首字母一定要小写!!!
我们提供的服务有:做网站、成都网站建设、微信公众号开发、网站优化、网站认证、凤冈ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的凤冈网站制作公司
package com.xiaohang.socialcard.pre.filter;
import com.xiaohang.socialcard.pre.utils.SM4Util;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@WebFilter(urlPatterns = {"/api/v1/get/getToken", "/api/v1/get/query", "/api/v1/get/page"}, filterName = "signAutheFilter")
public class SignAutheFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String param = request.getParameter("param");
try {
param = SM4Util.encryptEcb("1234567890", param);
} catch (Exception e) {
e.printStackTrace();
}
MyRequestWrapper parameterRequestWrapper = new MyRequestWrapper(request);
parameterRequestWrapper.addParameter("param", param);
chain.doFilter(parameterRequestWrapper, res);// 替换原始请求参数
}
@Override
public void destroy() {
}
}
class MyRequestWrapper extends HttpServletRequestWrapper {
private Map params = new HashMap();
@SuppressWarnings("unchecked")
public MyRequestWrapper(HttpServletRequest request) {
// 将request交给父类,以便于调用对应方法的时候,将其输出,其实父亲类的实现方式和第一种new的方式类似
super(request);
//将参数表,赋予给当前的Map以便于持有request中的参数
this.params.putAll(request.getParameterMap());
}
//重载一个构造方法
public MyRequestWrapper(HttpServletRequest request, Map extendParams) {
this(request);
addAllParameters(extendParams);//这里将扩展参数写入参数表
}
@Override
public String getParameter(String name) {//重写getParameter,代表参数从当前类中的map获取
String[] values = params.get(name);
if (values == null || values.length == 0) {
return null;
}
return values[0];
}
public String[] getParameterValues(String name) {//同上
return params.get(name);
}
public void addAllParameters(Map otherParams) {//增加多个参数
for (Map.Entry entry : otherParams.entrySet()) {
addParameter(entry.getKey(), entry.getValue());
}
}
public void addParameter(String name, Object value) {//增加参数
if (value != null) {
if (value instanceof ArrayList) {
String value1 = String.valueOf(value).substring(1, String.valueOf(value).length());
value = value1.substring(0, value1.length() - 1);
params.put(name, new String[]{(String) value});
} else if (value instanceof String[]) {
params.put(name, (String[]) value);
} else if (value instanceof String) {
params.put(name, new String[]{(String) value});
} else {
params.put(name, new String[]{String.valueOf(value)});
}
}
}
}
二、Application.class这里一定要加上ServletComponentScan注解用于扫描Filter过滤器
package com.xiaohang.socialcard.pre;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
文章题目:SpringCloud使用Filter过滤器实现@RequestParam参数加密
标题来源:http://scyanting.com/article/pdoioe.html