Modify request request parameters

Keywords: Java JSON

In essence, the parameters in the request cannot be changed, added or deleted;
But in the background program, generally, the operation of request parameters is performed by getParameter, getParameterNames, getParameterValues and other methods of request; therefore, if we can override these methods, the request parameters of request will be changed from the side. As it happens, the servlet provides an HttpServletRequestWrapper class for everyone to inherit (this class is the encapsulation class of HttpServletRequest, public class HttpServletRequestWrapper extensions servletrequestwrapper implements
HttpServletRequest {}), overriding the request related method.

The whole process of modifying the request parameter is: an interceptor will be defined. When a request enters the project, the interceptor will block the request and put it back after processing.

The cases are as follows:

//@WebFilter(filterName = "paramFilter", urlPatterns = "/*")//Interceptor intercepts all requests
//@Order(100)//The larger the number in brackets, the higher the execution order of multiple interceptors
public class ParamFilter extends OncePerRequestFilter{

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        
        HttpServletRequest wrapper = new RequestWrapper(request);//Define a new request(The name is wrapper),
        filterChain.doFilter(wrapper, response);//Will be modified request(wrapper)Put back
    }

    private static class RequestWrapper extends HttpServletRequestWrapper {//The internal class is used here, or not
        
        private Map<String, String[]> params = new HashMap<String, String[]>();//take request After the parameters in the object are modified, they are placed in this collection, and then all the parameters of the project are taken Parameter All from this set
        
        public RequestWrapper(HttpServletRequest request) {//Define constructor
            super(request);//take request To parent class

            //First pass request Native method, traversing the obtained parameters
            Enumeration enu=request.getParameterNames(); 
            while (enu.hasMoreElements()) {
                String paraName=(String)enu.nextElement();
                
                Map<String, Object> paraObj = (Map<String, Object>)JSONUtils.parse(paraName);//Because it's from my front desk json Format parameters
                Set<Entry<String, Object>> entrySet = paraObj.entrySet();
                for (Entry<String, Object> entry : entrySet) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    params.put(key, value);//
                }
            }
            //Modify here params Data in, whether added or modified
            ......
            
            //Will be modified params Reinsert RequestWrapper In object
            addParameters(request,params);
        }
        public void addAllParameters(Map<String , Object>otherParams) {//Add multiple parameters
            for(Map.Entry<String , Object>entry : otherParams.entrySet()) {
                addParameter(entry.getKey() , entry.getValue());
            }
        }
        @Override
        public String getParameter(String name) {
             String[]values = params.get(name);
             if(values == null || values.length == 0) {
                 return null;
             }
             return StringEscapeUtils.escapeHtml4(values[0]);
        }
        @Override
        public Enumeration<String> getParameterNames() {
            Vector<String> v = new Vector<String>();
            Set<Entry<String, String[]>> entrySet = params.entrySet();
            for (Entry<String, String[]> entry : entrySet) {
                v.add(entry.getKey());
            }
            Enumeration<String> en =  v.elements();

            return v.elements();
        }
        
        @Override
        public String[] getParameterValues(String name) {
            return params.get(name);
        }
    }
}

Posted by Jonob on Mon, 06 Jan 2020 13:19:10 -0800