Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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
Spring security 如何使@PostAuthorize失败导致@Transactional回滚?_Spring Security_Spring Boot - Fatal编程技术网

Spring security 如何使@PostAuthorize失败导致@Transactional回滚?

Spring security 如何使@PostAuthorize失败导致@Transactional回滚?,spring-security,spring-boot,Spring Security,Spring Boot,我的应用程序中有一些复杂的访问限制,这基本上需要考虑用户角色的组合,以及域对象的一些深层属性,才能做出访问决策 对于我的一些方法(特别是getItem(Integer-id)和updateItem(Integer-id,FormBean-form)),我无法提前知道它们是否被允许访问该项目,因为我还没有,所以我一直在使用@PostAuthorize 然而,后一个例子,updateItem(id,form)提出了一个挑战。我只想允许他们在某些特定情况下更新表单。现在,我确实看到了@PostAuth

我的应用程序中有一些复杂的访问限制,这基本上需要考虑用户角色的组合,以及域对象的一些深层属性,才能做出访问决策

对于我的一些方法(特别是
getItem(Integer-id)
updateItem(Integer-id,FormBean-form)
),我无法提前知道它们是否被允许访问该项目,因为我还没有,所以我一直在使用@PostAuthorize

然而,后一个例子,
updateItem(id,form)
提出了一个挑战。我只想允许他们在某些特定情况下更新表单。现在,我确实看到了
@PostAuthorize
,当他们做了不应该做的事情时,导致他们得到HTTP 403响应,但数据库更改不会回滚

在这种情况下,
@PreAuthorize
@Transactional
@PostAuthorize
是否可以很好地协同工作?(我想也许可以调整一些建议的顺序……但我不完全清楚该如何进行排序)


或者,我的系统是否变得足够复杂,以至于我真的应该在域ACL上咬紧牙关?不幸的是,关于这些内容的文档感觉相当单薄……

为什么不使用
@PreAuthorize(“hasPermission(#id,'read')”)
@PreAuthorize(“hasPermission(#id,'update')”)

但是如果您确实希望使用
@PostAuthorize
,那么您可以尝试调整安全和事务方面的顺序

我的猜测是:

// perform security check first, will throw exception if bad
@EnableGlobalMethodSecurity(prePostEnabled = true, order = 0)

// apply tm around security check and update, allowing for rollback
@EnableTransactionManagement(order = 1) 
Spring框架用于定义bean的顺序。 明确定义@Transactional和@PostAuthorize顺序的最简单方法是通过注释:

@EnableTransactionManagement(order = 0)
@EnableGlobalMethodSecurity(prePostEnabled = true, order = 1)

谢谢,我同意这可能是最好的办法。不幸的是,一旦我开始使用该接口管理多个域类,它可能会导致一些混乱的代码。你有什么建议的处理模式吗?(我想我希望注册一个类来处理每个域类的hasPermission;每个类都需要
@Autowire
a
FooRepository
类等等)您可以有一个通用基类,其中type参数将是实体类型,您还可以有一个repository字段,让Spring autowire为您提供。基类将有一个抽象方法,该方法将返回给定ID和身份验证的一组特权,然后子类将实现该方法。基类将获取该集并检查它是否包含所需的权限。