Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/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 Hibernate/Spring:是否存在行级安全性?_Java_Hibernate_Spring Security_Access Control - Fatal编程技术网

Java Hibernate/Spring:是否存在行级安全性?

Java Hibernate/Spring:是否存在行级安全性?,java,hibernate,spring-security,access-control,Java,Hibernate,Spring Security,Access Control,我不确定我是否正确使用了Spring Security的功能 我的问题是,我想。但我能找到的每个教程都是关于一个简单的。但是我怎样才能用它来摆脱 if(item .getStore().getId() == store.getId()) { /* .. */ } 在本例中: // StoreService.java @Transactional public ItemDTO deleteItem(String sessionId, Long storeId, ItemDTO itemDto)

我不确定我是否正确使用了Spring Security的功能

我的问题是,我想。但我能找到的每个教程都是关于一个简单的。但是我怎样才能用它来摆脱

if(item .getStore().getId() == store.getId()) { /* .. */ }
在本例中:

// StoreService.java

@Transactional
public ItemDTO deleteItem(String sessionId, Long storeId, ItemDTO itemDto) {

    // sessionId is the cookie I have placed in my database
    // This way I want to ensure that I am only accessing a store
    // that is associated with the logged in store owner (the user basically)
    Store store = this.storeOwnerRepository.getStore(sessionId, storeId);

    Item item = ConvertDTO.convertItem(store, itemDto);

    // THIS CHECK IS WHAT I WANT TO GET RID OF:
    // Check if the store ID that I got using the cookie is the
    // same ID as the store ID from the item that should be deleted
    if(item.getStore().getId() == store.getId()) {
        item = this.storeOwnerRepository.deleteItem(item);
    } else {
        // If this didn't work we have a potentially hostile user:
        throw new RuntimeException("Is somebody trying to delete items from a store he doesn't own?");
    }

    itemDto = ConvertEntity.convertItem(item);
    return itemDto;
}
使用Spring注释?这在Spring Security中是可能的吗

另一个可能有效的方法是,但我不确定是否希望数据库了解数据的安全方面


所以我很困惑如何正确地做到这一点。有什么想法吗?

您可能应该实现Spring安全性和工作角色及权限,这样您就可以确保不会收到非管理员用户的请求(通过使用
@Secured(“ROLE\u SOMEROLE”))
,这可以帮助您在将来拥有其他角色的情况下

然后,您应该更多地使用角色权限

然后添加对存储的读写权限。您可以将许多权限与用户关联,因此,您可以只对所需的存储进行读/写/任何操作

检查本教程,它可以帮助您


我认为你所说的更多的是验证,而不是安全性

只要在同一数据库中存储多个客户端/客户的数据,就必须小心防止用户无意(或恶意)访问彼此的数据


我建议您在web服务层执行此验证,并将业务逻辑的重点放在需要执行的操作的细节上。

我们已使用在域对象上实现了这种安全性。这包括:

  • 创建Spring的
    org.springframework.security.acls.model.AclService
    接口的实现,该接口知道如何返回给定主体对给定域对象的权限。例如,如果主体与该域对象具有关系foo,则授予读写权限;如果是关系栏,则授予读取、写入和删除权限
  • 向对域对象进行操作的服务方法添加注释,如定义要强制执行的访问控制断言的
    org.springframework.security.access.prepost.PreAuthorize
    org.springframework.security.access.prepost.PreAuthorize
    。例如,此方法要求当前经过身份验证的用户对类型X的参数具有“写入”权限,或者该方法要求当前经过身份验证的用户对返回对象具有“读取”权限。如果任一断言失败,将抛出一个
    AccessDeniedException
  • 调整Spring社交配置以启用方法级安全性。我在SpringSecurity的XML名称空间中使用了
    全局方法安全性
    元素
虽然有很多细节需要考虑,但是我们在多个Web应用中使用这种方法效果很好。它允许您将“谁获得哪些对象的哪些权限”逻辑与“执行此操作逻辑所需的权限”逻辑分开,并使这两者远离您的数据库查询

当然,在某些情况下,您可能希望在查询中强制执行访问控制,而不是先查询,然后过滤结果。我见过术语“早期绑定”用于描述数据库查询中访问控制的实施,“后期绑定”用于描述对查询结果的访问控制。SpringSecurityACLAPI是一个非常好的、健壮的后期绑定解决方案

您最终将得到如下业务服务方法:

@PostAuthorize("hasPermission(returnObject, 'READ')")
public MyItem getMyItem(Long id) {
    return dao.getMyItem(id);
}

@PreAuthorize("hasPermission(#toDelete, 'DELETE')")
public void deleteMyItem(MyItem toDelete) {
    dao.delete(toDelete);
}
以及具有以下方法的AclService:

public Acl readAclById(ObjectIdentity objectIdentity, List<Sid> sids) throws NotFoundException {
    /*
examines objectIdentity which identifies domain object in question, and sids which identifies the principal who wants permissions on the domain object, then returns an ACL instance with permission grants on that domain object for that/those principals
    */
    return new AclImpl(...);
}

如果查询数据库以查看用户是否与试图修改的存储关联,如果是,则执行查询,如果不是,则返回其他内容如何?@jpganz18这就是我在示例代码中使用
item.getStore().getId()==store.getId()执行的操作
但我认为Spring Security可以帮助我摆脱这种代码。Spring Security中有没有与特定商店相关的角色?@jpganz18没有。目前我没有使用Spring Security,因为我不知道如何做到这一点。但是如果有角色,那么
店主
(在我的代码中由
storeOwnerRepository
表示)实际上是“拥有”一个或多个店铺的登录用户。这个用户基本上有一个角色
ADMIN
,但因为我只有一个角色,所以我不确定这是否重要。问题是,这个用户可以简单地向我发送任意ID并修改他不拥有的存储,除非我在那里进行检查,但我找不到任何Spring安全性示例代码可以帮助我。1。您是否关心与其他用户“共享”实体,还是只关心用户(或管理员)看到他们自己的东西?2.哪个数据库品牌,您对在db中使用它有什么感觉?3.春季安全4?对不起,我不明白这怎么能满足我的要求。假设我只有一个角色,那就是
ADMIN
。我页面上的任何人都是管理员,但每个管理员都有自己的资源。我不知道SpringSecurity在哪里阻止管理员向我发送StoreID3,即使他只拥有Store1和Store2。我还得在那里做检查——否则我真的不明白了?我需要短信。类似于
@RejectAlwaysIf(“storeOwner.id!=store.storeOwner.id”)公共void deleteItem(/*..*/){/*..*/}-但当然,这个约束应该是配置的一部分或其他任何内容。您可以关联对存储的权限,如write-store-1、write-store-2、write-store-3,然后检查哪些权限具有,但我会对每个存储都这样做,对吗?因为会有很多商店^^嗯,在这种情况下,是的,我真的没有看到任何只有spring security拥有的解决方案,因为它只是一个安全层,这更像是一个业务逻辑问题,这很奇怪-我真的认为spring可以在这里帮助我。信息技术
<beans:bean id="permissionEvaluator"
    class="org.springframework.security.acls.AclPermissionEvaluator">
    <beans:constructor-arg ref="aclServiceImpl" />
</beans:bean>
<beans:bean id="expressionHandler"
    class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
    <beans:property name="permissionEvaluator" ref="permissionEvaluator" />
</beans:bean>
<global-method-security pre-post-annotations="enabled">
    <expression-handler ref="expressionHandler" />
</global-method-security>