springboot统⼀请求参数修改(可复制HttpServletRequest流)springboot统⼀请求参数修改
http请求对象HttpServletRequest中的数据要么通过流(json请求时)或者参数的形式(form表单)向后台传输数据,如果是通过流的⽅式向后端传输数据,那么在后端读取数据都是⼀次性的,如果在或者AOP中读取过,则不能映射到controller的参数。但可以通过在过滤器中定义可复制的request进⾏读取,⽽不影响controller参数的映射。本⽂主要介绍如何通过过滤器实现可复制流来解决实际业务中遇到的相关问题。
假设这种场景:我需要将请求参数中的⽤户名进⾏解密。下述内容可以解决这个问题。
定义body请求包装类
public class BodyRequestWrapper extends HttpServletRequestWrapper {
private String body;
public BodyRequestWrapper(HttpServletRequest request, String context){
super(request);
body = context;
}
@Override
public ServletInputStream getInputStream()throws IOException {
final ByteArrayInputStream byteArrayInputStream =new Bytes("UTF-8"));
ServletInputStream servletInputStream =new ServletInputStream(){
@Override
public int read()throws IOException {
springboot aopad();
}
@Override
public boolean isFinished(){
return false;
}
@Override
public boolean isReady(){
return false;
}
@Override
public void setReadListener(ReadListener listener){
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader()throws IOException {
return new BufferedReader(new InputStream()));
}
}
定义form表单请求包装类
public class ParameterRequestWrapper extends HttpServletRequestWrapper {
private Map<String, String[]> params =new HashMap<String, String[]>();
public ParameterRequestWrapper(HttpServletRequest request)throws IOException {
super(request);
this.params.ParameterMap());
}
/**
* 重载⼀个构造⽅法
*
* @param request
* @param extendParams
*/
public ParameterRequestWrapper(HttpServletRequest request, Map<String, String[]> extendParams)throws IOException { this(request);
addAllParameters(extendParams);
}
@Override
public Enumeration<String>getParameterNames(){
umeration(params.keySet());
}
@Override
public String getParameter(String name){
String[] values = (name);
if(values ==null|| values.length ==0){
return null;
}
return values[0];
}
@Override
public String[]getParameterValues(String name){
(name);
}
public void addAllParameters(Map<String, String[]> otherParams){
for(Map.Entry<String, String[]> entry : Set()){
Key(), Value());
}
}
public void addParameter(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 Map<String, String[]>getParameterMap(){
return this.params;
}
}
定义过滤器
@Component
@WebFilter(filterName ="myFilter", urlPatterns ="/")
@Order(10000)
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig)throws ServletException {
}
@Override
public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain)throws IOException, ServletException { if(req instanceof HttpServletRequest){
HttpServletRequest request =(HttpServletRequest) req;
String contentType = ContentType();
//body形式(json)
if(contentType.equals(MediaType.APPLICATION_JSON_UTF8_VALUE)
|| contentType.equals(MediaType.APPLICATION_JSON_VALUE)){
//获取request的body参数
String postContent =getBody(request);
//如果body中存在数据放⼊HttpServletRequest
if(StringUtils.isNotEmpty(postContent)){
//修改、新增、删除参数
JSONObject jsStr = JSONObject.parseObject(postContent);
ainsKey("userName")){
jsStr.put("userName","对⽤户名进⾏解密");
}
postContent = JSONString();
//将参数放⼊重写的⽅法中
request =new BodyRequestWrapper(request, postContent);
}
}
/
/form表单形式
if((contentType.equals(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
|| ains(MediaType.MULTIPART_FORM_DATA_VALUE))
&&!ParameterMap().isEmpty()){
//修改、新增、删除参数
Map<String, String[]> parameterMap = ParameterMap();
//对请求参数进⾏处理
ainsKey("userName")){
parameterMap.put("userName",new String[]{"对⽤户名进⾏解密"});
}
//将参数放⼊重写的⽅法中
request =new ParameterRequestWrapper(request, parameterMap);
}
chain.doFilter(request, response);
}else{
filterChain.doFilter(req, response);
}
}
//获取Request的body数据
private String getBody(ServletRequest request){
StringBuilder stringBuilder =new StringBuilder();
BufferedReader bufferedReader =null;
InputStream inputStream =null;
try{
inputStream = InputStream();
if(inputStream !=null){
bufferedReader =new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer =new char[128];
int bytesRead =-1;
while((bytesRead = ad(charBuffer))>0){
while((bytesRead = ad(charBuffer))>0){
stringBuilder.append(charBuffer,0, bytesRead);
}
}else{
stringBuilder.append("");
}
}catch(IOException ex){
}finally{
if(inputStream !=null){
try{
inputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(bufferedReader !=null){
try{
bufferedReader.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
String();
}
@Override
public void destroy(){
}
}
测试controller
@RestController
@RequestMapping("/user")
@Slf4j
public class IccAuthCallBackController {
@PostMapping("test1")
public Response test1(@RequestBody User user){
Response response=null;
return response;
}
@PostMapping("test2")
public Response test2(@RequestParam("userName")String userName){ IccAuthResponse response =null;
return response;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论