Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.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 如何保护SpringBoot/Spring数据Rest,以便用户只能部分访问对象_Java_Spring Boot_Rbac - Fatal编程技术网

Java 如何保护SpringBoot/Spring数据Rest,以便用户只能部分访问对象

Java 如何保护SpringBoot/Spring数据Rest,以便用户只能部分访问对象,java,spring-boot,rbac,Java,Spring Boot,Rbac,我有一个spring-boot应用程序,它使用spring-data-rest实现基于角色的访问控制机制来确保基于用户角色的资源访问 要求是:具有特定角色的用户只能访问对象的某些部分 例如:具有staff角色的用户可以仅查看/更新对象user的基本信息(姓名、年龄),但具有admin角色的用户可以做任何事 我现在使用spring-security和pre/post注释再次检查用户权限,但它只控制对整个对象的访问 有没有其他方法可以这样做 安全的/@RolesAllowed注释用于指定方法上的角色

我有一个
spring-boot
应用程序,它使用
spring-data-rest
实现基于角色的访问控制机制来确保基于用户角色的资源访问

要求是:具有特定角色的用户只能访问对象的某些部分

例如:具有
staff
角色的用户可以仅查看/更新对象
user
的基本信息(姓名、年龄),但具有
admin
角色的用户可以做任何事

我现在使用
spring-security
pre/post
注释再次检查用户权限,但它只控制对整个对象的访问


有没有其他方法可以这样做

安全的
/
@RolesAllowed
注释用于指定方法上的角色列表。因此,用户只有在至少具有一个指定角色时才能访问该方法

您可能有不同的方法来执行特定任务,如查看/更新/或其他。例如:

@Secured({“角色查看器”、“角色编辑器”})/@RolesAllowed(“角色查看器”)
公共布尔值isValidUsername(字符串用户名){
返回userRoleRepository.isValidUsername(用户名);
}
除此之外,如果请求对象可用,还可以在原始java代码中以编程方式检查用户权限:
request.isUserInRole(“someAuthority”)
,然后执行操作或处理错误。

@Secured
/
@RolesAllowed
注释用于指定方法上的角色列表。因此,用户只有在至少具有一个指定角色时才能访问该方法

您可能有不同的方法来执行特定任务,如查看/更新/或其他。例如:

@Secured({“角色查看器”、“角色编辑器”})/@RolesAllowed(“角色查看器”)
公共布尔值isValidUsername(字符串用户名){
返回userRoleRepository.isValidUsername(用户名);
}
除此之外,如果请求对象可用,还可以在原始java代码中以编程方式检查用户权限:
request.isUserInRole(“someAuthority”)
,然后执行操作或处理错误。

您应该自定义规则。 访问资源集合时,您需要自行筛选资源。
访问单个资源时,可以在控制器或服务中引发异常。您可以使用
@ControllerAdvice
@ExceptionHandler(Exception.class)
捕获控制器。然后返回403错误。 比如

控制器建议

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(AccessErrorException.class)
   public void handleException(AccessErrorException e){
        LOGGER.error(e.getMessage(), e);
        // do something send.error(403)
    }
}
服务:

public interface RoleService {
   Role queryById() throw AccessErrorException;
}
你应该习惯规则。 访问资源集合时,您需要自行筛选资源。
访问单个资源时,可以在控制器或服务中引发异常。您可以使用
@ControllerAdvice
@ExceptionHandler(Exception.class)
捕获控制器。然后返回403错误。 比如

控制器建议

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(AccessErrorException.class)
   public void handleException(AccessErrorException e){
        LOGGER.error(e.getMessage(), e);
        // do something send.error(403)
    }
}
服务:

public interface RoleService {
   Role queryById() throw AccessErrorException;
}
->如果您使用mongodb,spring数据mongodb有一个选项,但不确定spring数据是否支持
$redact
运算符您可以使用jCasbin:->如果您使用mongodb,spring数据mongodb有一个选项,但不确定spring数据是否支持
$redact
运算符您可以使用jCasbin: