找回密码
 立即注册
首页 业界区 安全 SpringBoot对所有PUT、DELETE接口追加POST请求方式 ...

SpringBoot对所有PUT、DELETE接口追加POST请求方式

堠秉 前天 00:15
背景

产品在某个项目进行私有化交付的时候,遇到WAF拦截PUT、DELETE请求的场景,只能将现有接口的请求方式修改为POST请求方式。
考虑到产品经过了多年的沉淀,涉及到的接口很多,手动修改可能存在改漏的情况。另外,基于标准的Restful接口定义规范,不同的业务动作应该通过请求方式进行区分。
所有,想通过底层架构优化的方式,在应用服务启动时,对定义为PUT和DELETE请求方式的接口,追加POST请求方式,具体实现代码如下:
通过RequestMappingHandlerMapping修改RequestMappingInfo接口定义
  1. public class CustomRequestMappingHandler extends RequestMappingHandlerMapping {
  2.     private static final String DATA_FRAME_PACKAGE_FOR_CN = "cn.xxx";
  3.     private static final String DATA_FRAME_PACKAGE_FOR_COM = "com.xxx";
  4.     private static final List<RequestMethod> REQUEST_METHODS = Arrays.asList(RequestMethod.PUT, RequestMethod.DELETE);
  5.     @Override
  6.     protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
  7.         // 对包路径为com.xxx、cn.xxx进行处理
  8.         // 仅处理web层接口,不处理feign接口
  9.         if ((StringUtils.startsWithIgnoreCase(handlerType.getName(), DATA_FRAME_PACKAGE_FOR_CN)
  10.                 || StringUtils.startsWithIgnoreCase(handlerType.getName(), DATA_FRAME_PACKAGE_FOR_COM))
  11.                 && AnnotatedElementUtils.hasAnnotation(method, RequestMapping.class)
  12.                 && !AnnotatedElementUtils.hasAnnotation(handlerType, FeignClient.class)) {
  13.             // 从父类方法获取接口定义
  14.             RequestMappingInfo requestMappingInfo = super.getMappingForMethod(method, handlerType);
  15.             // 匹配PUT、DELETE请求接口
  16.             if (CollectionUtils.containsAny(requestMappingInfo.getMethodsCondition().getMethods(), REQUEST_METHODS)
  17.                     && !requestMappingInfo.getMethodsCondition().getMethods().contains(RequestMethod.POST)) {
  18.                 // 获取原始请求方法
  19.                 Set<RequestMethod> methods = requestMappingInfo.getMethodsCondition().getMethods();
  20.                 // 添加POST方法
  21.                 methods.add(RequestMethod.POST);
  22.                 // 创建新的RequestMappingInfo
  23.                 return RequestMappingInfo.paths().build().combine(requestMappingInfo);
  24.             }
  25.         }
  26.         return super.getMappingForMethod(method, handlerType);
  27.     }
  28. }
复制代码
注册自定义的RequestMappingHandlerMapping
  1. @Configuration(proxyBeanMethods = false)
  2. @ConditionalOnProperty(name = "custom.openapi.registration-post-method.enabled", havingValue = "true", matchIfMissing = true)
  3. public class WebMvcConfig implements WebMvcRegistrations {
  4.     @Override
  5.     public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
  6.         RequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandler();
  7.         handlerMapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
  8.         return handlerMapping;
  9.     }
  10. }
复制代码
通过Swagger查看原有的DELETE接口定义:
1.jpeg

追加POST请求方式后的POST接口定义:
2.jpeg


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册