【问题标题】:Can I specifically disable PATCH in spring-data-rest repository?我可以在 spring-data-rest 存储库中专门禁用 PATCH 吗?
【发布时间】:2017-07-06 16:23:50
【问题描述】:

我们 API 的客户端不使用补丁,我想避免使用它来增加维护开销。我不想禁用 POST 或 PUT。

【问题讨论】:

  • 最简单的选择可能是在 HTTP 级别使用 Spring Security 或拒绝所有 PATCH 请求的简单 Servlet 过滤器进行处理。

标签: spring-boot spring-data-rest


【解决方案1】:

可以在安全级别进行处理,方法是扩展 WebSecurityConfigurerAdapter(在 spring-security-config 中可用)并覆盖 configure(HttpSecurity http) 以拒绝对目标 url 的 PATCH 请求:

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(HttpMethod.PATCH, "/path_to_target_url").denyAll();
    }

}

任何 PATCH 到目标 URL 的尝试都将失败,并出现 401 Unauthorized 错误。

【讨论】:

    【解决方案2】:

    或许需要把插件放到REST控制器中……例如:

    @RestController
    @RequestMapping("/api/...")
    @ExposesResourceFor(...)
    public class MyController {
    ...
        @PatchMapping
        HttpEntity<?> patch() {
            return new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2015-04-04
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      • 2018-01-08
      相关资源
      最近更新 更多