[英]Spring security RESTful path based role control
例如
/user/{userId}/* # Only user with userId and admin can access
/order/{orderId}/* # Only the order owner of orderId and admin can access
当前解决方案@Current
注释是自定义注入,它与传递给服务器的token
有关。 @PathVariable("user-id") UserEntity user
是通过Spring-Data的路径获得的
@PreAuthorize("#user.id == #u?.id")
public UserDTO access(@P("user") @Current UserEntity requestUser,
@P("u") @PathVariable("user-id") UserEntity user)
@PreAuthorize("#user.id == #uid && (#order == null || #order?.user?.id == #uid)")
public Message access(@Current @P("user") UserEntity user,
@PathVariable("user-id") @P("uid") Long uid,
@PathVariable("order-id") @P("order") OrderEntity order)
我们有太多注释,是否有任何简单的方法来配置它们?
试过了
.antMatchers("/user/[0-9]+/*").hasRole("ROLE_USER")
无法自定义用户检查。 我建议您使用方法安全性来实现细粒度的逻辑来加强资源访问。 我认为基于网址的身份验证仅对简单的用例有效。
如果授权逻辑需要几行代码,我还建议使用带有自定义批注的AOP来实现方法安全性(而不是使用@PreAuthorize
)。
例如,您可以拦截带注释的方法调用:
@Before("@annotation(your.annotations.AllowedToOwner) && @annotation(ann)")
public void checkOwner(JoinPoint joinPoint, AllowedToOwner ann) throws Throwable {
// check owner, throws AccessDeniedException if check fails...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.