[Spring] 封装request 自定义添加parameter
通过Filter实现
1 封装reqeust
要点在于 public void setParameter(String name, Object value) 方法。 将参数放入到自己的map中去。
package com.autoyong.filter;
import java.io.IOException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class ParamServletRequestWrapper extends HttpServletRequestWrapper {
private Map<String , String[]> params = new HashMap<String, String[]>();
public ParamServletRequestWrapper(HttpServletRequest request) throws IOException{
super(request);
this.params.putAll(request.getParameterMap());
}
public void addAllParameters(Map<String, Object> extraParams) {
for (Map.Entry<String, Object> entry : extraParams.entrySet()) {
setParameter(entry.getKey(), entry.getValue());
}
}
public void setParameter(String name, Object value) {
if (value != null) {
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)});
}
}
}
@Override
public Enumeration<String> getParameterNames() {
Vector<String> vector = new Vector<String>(params.keySet());
return vector.elements();
}
/**
* 重写getParameter,代表参数从当前类中的map获取
* @param name
* @return
*/
@Override
public String getParameter(String name) {
String[]values = params.get(name);
if(values == null || values.length == 0) {
return null;
}
return values[0];
}
/**
* 重写getParameterValues方法,从当前类的 map中取值
* @param name
* @return
*/
@Override
public String[] getParameterValues(String name) {
return params.get(name);
}
}
通过filter封装请求来的request
要点
- @WebFilter 中的filterName 和类名要保持一致
- 不要添加@compent 添加后过滤器会被加载两次。 然后过滤路径urlPatterns 会失效!
package com.autoyong.filter;
import com.autoyong.common.utils.AESUtil;
import com.autoyong.common.utils.StringUtils;
import org.springframework.core.annotation.Order;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Order(1)
@WebFilter(
filterName = "BodyReaderRequestFilter", urlPatterns = "/xxxx/*")
public class BodyReaderRequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
ParamServletRequestWrapper requestWrapper = new ParamServletRequestWrapper(request);
if (requestWrapper == null) {
filterChain.doFilter(request, response);
} else {
String data = request.getParameter("data");
if (!StringUtils.isEmpty(data)) {
//校验数据项 是否数据不为空
try {
if (!StringUtils.isEmpty(data)) {
data = AESUtil.DecryptByDefaultKey(data.trim());
System.out.println(data);
requestWrapper.setParameter("dataDto", data);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
filterChain.doFilter(requestWrapper, response);
}
} else {
filterChain.doFilter(request, response);
}
}
}
@Override
public void destroy() {
}
}