Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring数据rest存储库的安全方法_Java_Spring Boot_Spring Security_Spring Data Rest_Spring Security Rest - Fatal编程技术网

Java spring数据rest存储库的安全方法

Java spring数据rest存储库的安全方法,java,spring-boot,spring-security,spring-data-rest,spring-security-rest,Java,Spring Boot,Spring Security,Spring Data Rest,Spring Security Rest,我目前正在开发一个基于springboot的restapi服务器。得益于Spring Data Rest,10个ish实体可以通过一个简单的存储库轻松拥有自己的控制器(@RepositoryRestResourceplusJpaRepository和JpaSpecificationExecutor)。现在我需要将安全控制与@PreAuthorize集成 这里的问题是我应该使用哪种方法来限制GET/POST/etc 例如,如果我限制了delete的权限,它是否会对deleteById、delete

我目前正在开发一个基于springboot的restapi服务器。得益于Spring Data Rest,10个ish实体可以通过一个简单的存储库轻松拥有自己的控制器(
@RepositoryRestResource
plus
JpaRepository
JpaSpecificationExecutor
)。现在我需要将安全控制与
@PreAuthorize
集成

这里的问题是我应该使用哪种方法来限制GET/POST/etc

例如,如果我限制了
delete
的权限,它是否会对
deleteById
deleteInBatch
deleteAll
产生类似的影响?我在文档中看到导出的注释被放在
deleteById
delete
上,没有任何进一步的解释,这让我感到困惑

例如,如果我限制delete的权限,它是否会对deleteById、deleteInBatch、deleteAll产生类似的影响

据我所知:否。在授权搜索的地方检查此示例代码,但删除仅限于管理员:

public interface RecordRepository<T extends Record> extends MongoRepository<T, String> {

    // paginated queries
    @RestResource(path = "names", rel = "name")
    public Page<T> findByName(@Param("name") String name, Pageable pageable);
    @RestResource(path = "types", rel = "types")
    public Page<T> findByTypeIn(@Param("type") List<String> types, Pageable pageable);

    // restrict delete operations to administrators only

    @PreAuthorize("hasRole('ADMIN')")
    @Override
    void deleteById(String id);

    @PreAuthorize("hasRole('ADMIN')")
    @Override
    void delete(T entity);

    @PreAuthorize("hasRole('ADMIN')")
    @Override
    void deleteAll(Iterable<? extends T> records);

    @PreAuthorize("hasRole('ADMIN')")
    @Override
    void deleteAll();
}

请注意,这是一个快速且脏的复制粘贴,可能无法开箱即用(您可能需要配置一个)。

角色层次结构是一个好主意,我将研究它。例如,我已经通过创建一个通用超级接口实现了一个混合解决方案,该接口将由任何专用于特定角色的存储库子类化,考虑到Spring Security中权限控制注释的继承性事实上,我完全忘了提到这种可能性。
public class WebSecurityBaseConfiguration extends WebSecurityConfigurerAdapter {
    ...

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
          .antMatchers(HttpMethod.DELETE).hasRole("ADMIN");
    }

}