Spring-web源码解析之Filter-HiddenHttpMethodFilter

本文涉及的产品
云解析 DNS,旗舰版 1个月
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
简介: 基于4.1.7.RELEASE就如同它的名字,该类负责解析隐藏的HttpMethod,用了这个Filter之后,你可以在页面上POST时指定_method参数,该Filter会根据参数指定的值将Request包装成为指定的HttpMethod的request。

基于4.1.7.RELEASE

就如同它的名字,该类负责解析隐藏的HttpMethod,用了这个Filter之后,你可以在页面上POST时指定_method参数,该Filter会根据参数指定的值将Request包装成为指定的HttpMethod的request。需要注意的有两点

1 必须是POST方式才进行处理

2 可以通过设置methodParam来更改参数名字,默认为_method。

主要代码如下

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {

   String paramValue = request.getParameter(this.methodParam);
   if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
      String method = paramValue.toUpperCase(Locale.ENGLISH);
      HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
      filterChain.doFilter(wrapper, response);
   }
   else {
      filterChain.doFilter(request, response);
   }
}

根据注意事项1,如果是GET则直接进行下一个Filter处理。如果是POST,_method参数所传递进来的所有值都会被转换为大写。

附包装器代码

private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {

   private final String method;

   public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
      super(request);
      this.method = method;
   }

   @Override
   public String getMethod() {
      return this.method;
   }
}


目录
相关文章
|
15天前
|
存储 NoSQL Redis
redis 6源码解析之 object
redis 6源码解析之 object
43 6
|
4天前
|
XML Java 数据格式
Spring Cloud全解析:注册中心之zookeeper注册中心
使用ZooKeeper作为Spring Cloud的注册中心无需单独部署服务器,直接利用ZooKeeper服务端功能。项目通过`spring-cloud-starter-zookeeper-discovery`依赖实现服务注册与发现。配置文件指定连接地址,如`localhost:2181`。启动应用后,服务自动注册到ZooKeeper的`/services`路径下,形成临时节点,包含服务实例信息。
|
8天前
|
开发者 Python
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
深入解析Python `httpx`源码,探索现代HTTP客户端的秘密!
32 1
|
8天前
|
开发者 Python
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
深入解析Python `requests`库源码,揭开HTTP请求的神秘面纱!
21 1
|
15天前
|
NoSQL Redis
redis 6源码解析之 ziplist
redis 6源码解析之 ziplist
16 5
|
3天前
|
算法 安全 Java
深入解析Java多线程:源码级别的分析与实践
深入解析Java多线程:源码级别的分析与实践
|
2月前
|
XML Java 数据格式
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
67 3
|
22天前
|
负载均衡 Java Spring
@EnableFeignClients注解源码解析
@EnableFeignClients注解源码解析
48 14
|
22天前
|
负载均衡 Java API
Feign 进行rpc 调用时使用ribbon负载均衡源码解析
Feign 进行rpc 调用时使用ribbon负载均衡源码解析
38 11
|
22天前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
60 11

热门文章

最新文章

推荐镜像

更多