【问题标题】:Export PATCH/PUT but not POST in @RepositoryRestResource在@RepositoryRestResource 中导出 PATCH/PUT 但不是 POST
【发布时间】:2017-12-09 10:26:51
【问题描述】:

我正在使用 spring-data-rest 并公开端点,以便通过存储库对我的实体进行 CRUD。
其中一个实体应该可以使用 PATCH/PUT 方法更新,但不能使用 POST 方法创建新实例。

似乎这两个动作都通过save 方法,所以似乎不可能只导出一些请求:

@RestResource(exported = ?)
@Override
<S extends User> S save(S s);

实现这一目标的最佳方法是什么?
我应该覆盖save 方法吗?写自定义Validator?

【问题讨论】:

    标签: spring spring-data spring-data-jpa spring-data-rest


    【解决方案1】:

    你可以使用

    前两个应该听BeforeCreateEvent

    【讨论】:

    • 使用其中任何一种解决方案都会导致 OPTIONS 请求在不正确的标头中返回“允许:POST,...”,对吗?
    • 我想是的。我添加了第三个选项,您还可以在其中操作 OPTIONS-Mapping。
    【解决方案2】:

    一种解决方案是扩展 WebSecurityConfigurerAdapter(在 spring-security-config 中提供)以拒绝对目标 url 的 POST 请求的访问:

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers(HttpMethod.POST, "/path_to_target_url").denyAll();
        }
    
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 2014-09-30
      • 2012-10-13
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多