springboot修改请求参数

在开发中有时会遇到需要统一修改请求参数的需求,比如前端对某个请求参数进行了base64加密,后端需要统一进行解密。

这个时候只需要定义一个RequestParameterWrapper,再写一个Filter即可。

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.HashMap;
import java.util.Map;

/**
 * 请求参数修改
 */
public class RequestParameterWrapper extends HttpServletRequestWrapper {

    private Map<String, String[]> params = new HashMap<>();

    RequestParameterWrapper(HttpServletRequest request) {
        super(request);
        //将现有parameter传递给params
        this.params.putAll(request.getParameterMap());
    }


    void addParameters(Map<String, Object> extraParams) {
        for (Map.Entry<String, Object> entry : extraParams.entrySet()) {
            addParameter(entry.getKey(), entry.getValue());
        }
    }

    /**
     * 重写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];
    }

    /**
     * 同上
     *
     * @param name
     * @return
     */
    @Override
    public String[] getParameterValues(String name) {
        return params.get(name);
    }

    /**
     * 添加参数
     *
     * @param name
     * @param value
     */
    private void addParameter(String name, Object value) {
        if (value != null) {
            // System.out.println(value);
            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)});
            }
        }
    }
}

Filter


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 请求设置
 */
@Component
@Slf4j
public class ParamFilter extends OncePerRequestFilter {

   

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain chain) throws IOException, ServletException {

		// 获取你需要的参数
        String param = request.getParameter("key");
        if (StringUtils.isBlank(param)) {
            chain.doFilter(request, response);
        } else {
            byte[] bytes = new BASE64Decoder().decodeBuffer(param);
            String decode = new String(bytes);
            Map<String, Object> map = new HashMap<>();
            map.put("key", new String[]{decode});
            RequestParameterWrapper wrapper = new RequestParameterWrapper(request);
            wrapper.addParameters(map);
            chain.doFilter(wrapper, response);
        }

    }

}

这个样在controller获取的key就是base64解密后的字符串了

©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页