您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
@WebFilter中的filterName首字母一定要小写!!!
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<String, String[]> params = new HashMap<String, String[]>();
@SuppressWarnings("unchecked")
public MyRequestWrapper(HttpServletRequest request) {
// 将request交给父类,以便于调用对应方法的时候,将其输出,其实父亲类的实现方式和第一种new的方式类似
super(request);
//将参数表,赋予给当前的Map以便于持有request中的参数
this.params.putAll(request.getParameterMap());
}
//重载一个构造方法
public MyRequestWrapper(HttpServletRequest request, Map<String, Object> 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<String, Object> otherParams) {//增加多个参数
for (Map.Entry<String, Object> 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)});
}
}
}
}
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);
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。