我发现使用
hasPermission这种要求很方便。特别,
- 通过注释配置类来启用方法安全性
@EnableGlobalMethodSecurity(prePostEnabled = true)
- 在控制器中获取客户,然后调用服务方法来传递客户。
注释服务方法
@PreAuthorize
@PreAuthorize("hasPermission(#customer, 'edit')")public void updateCustomer(Customer customer, …) {
…您应该已经配置了
Permissionevaluator
,如下所示:@Component
public class PermissionevaluatorImpl implements Permissionevaluator {
@Override
public boolean hasPermission(Authentication auth,
Object entity, Object permission) {// return true only if auth has the given // permission for the customer. // Current user can be obtained from auth.
}
…
}
作为一种更简洁的模式,在上述方法中,您可以将权限检查委托给实体类,如下所示:
baseEntity baseEntity = (baseEntity) entity;
return entity.hasPermission(Util.getUser(auth), (String) permission);
请参阅此了解更多详情。



