通过实现Filter与HttpServletRequestWrapper修改servlet请求中的getParameterMap参数

重写HttpServletRequestWrapper中ParameterMap相关的方法

public class CustomParameterRequestWrapper extends HttpServletRequestWrapper {
    private Map<String, String[]> params;

    /**
     * Constructs a request object wrapping the given request.
     *
     * @param request the {@link HttpServletRequest} to be wrapped.
     * @throws IllegalArgumentException if the request is null
     */
    public CustomParameterRequestWrapper(HttpServletRequest request, Map<String, String[]> params) {
        super(request);
        this.params = params;
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return params;
    }

    @Override
    public Enumeration<String> getParameterNames() {
        Vector<String> l = new Vector<>(params.keySet());
        return l.elements();
    }

    @Override
    public String[] getParameterValues(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            return (String[]) v;
        } else if (v instanceof String) {
            return new String[]{(String) v};
        } else {
            return new String[]{v.toString()};
        }
    }

    @Override
    public String getParameter(String name) {
        Object v = params.get(name);
        if (v == null) {
            return null;
        } else if (v instanceof String[]) {
            String[] strArr = (String[]) v;
            if (strArr.length > 0) {
                return strArr[0];
            } else {
                return null;
            }
        } else if (v instanceof String) {
            return (String) v;
        } else {
            return v.toString();
        }
    }

}

实现Filter接口

@Component
@WebFilter(filterName = "customServletFilter", urlPatterns = "/*")
public class CustomServletFilter implements Filter {
    @Autowired
    private AdminUserAuthRedis userAuthRedis;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String token = request.getHeader("token");
        AdminUser adminUser = userAuthRedis.getUserLoginInfo(token);
        if (adminUser == null) {
            chain.doFilter(request, response);
            return;
        }
        //将请求中已存在的参数赋值到newParams中
        Map<String, String[]> newParams = new HashMap<>(request.getParameterMap());
        //设置用户相关信息
        newParams.put("adminUserNick", new String[]{adminUser.getAdminUserNick()});
        newParams.put("roleId", new String[]{adminUser.getRoleId().toString()});
        newParams.put("adminUserId", new String[]{adminUser.getAdminUserId().toString()});
        CustomParameterRequestWrapper wrapRequest = new CustomParameterRequestWrapper(request, newParams);
        request.getParameterMap();
        request = wrapRequest;
        chain.doFilter(request, response);

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}

效果展示

通过实现Filter与HttpServletRequestWrapper修改servlet请求中的getParameterMap参数_第1张图片

通过实现Filter与HttpServletRequestWrapper修改servlet请求中的getParameterMap参数_第2张图片

你可能感兴趣的:(servlet,java,开发语言)