• 关于HiddenHttpMethodFilter


    这个类的代码比较少,所以把整个类的代码都复制过来。在注释中添加上自己的理解。

    public class HiddenHttpMethodFilter extends OncePerRequestFilter {
    
        /** Default method parameter: {@code _method} */
        public static final String DEFAULT_METHOD_PARAM = "_method";
    
        private String methodParam = DEFAULT_METHOD_PARAM;
    
    
        /**
         * Set the parameter name to look for HTTP methods.
         * @see #DEFAULT_METHOD_PARAM
         */
        public void setMethodParam(String methodParam) {
            Assert.hasText(methodParam, "'methodParam' must not be empty");
            this.methodParam = methodParam;
        }
    
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
                throws ServletException, IOException {
    
            HttpServletRequest requestToUse = request;
            //如果这是一个POST请求
            if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
                //接着获取参数值比如:PUT、DELETE等
                String paramValue = request.getParameter(this.methodParam);
                //封装成的相应的请求
                if (StringUtils.hasLength(paramValue)) {
                    requestToUse = new HttpMethodRequestWrapper(request, paramValue);
                }
            }
            //最后将请求发送给下一个filter
            filterChain.doFilter(requestToUse, response);
        }
    
    
        /**
         * Simple {@link HttpServletRequest} wrapper that returns the supplied method for
         * {@link HttpServletRequest#getMethod()}.
         */
        private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
    
            private final String method;
        
            public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
                super(request);
                this.method = method.toUpperCase(Locale.ENGLISH);
            }
    
            @Override
            public String getMethod() {
                return this.method;
            }
        }
  • 相关阅读:
    BestCoder Round #84
    codeforces#689BMike and Shortcuts
    POJ2985 并查集+线段树 求第k大的数
    Trie树模板 POJ1056
    新建zabbix数据库
    python输出菱形
    wmi获取计算机信息
    python测试IP地址是否ping通
    手机安装python环境
    Centos 8 安装zabbix 爬坑
  • 原文地址:https://www.cnblogs.com/GooPolaris/p/7919666.html
Copyright © 2020-2023  润新知