gpt4 book ai didi

java - 如何实现自定义spring security acl?

转载 作者:行者123 更新时间:2023-11-29 02:58:25 24 4
gpt4 key购买 nike

我正在使用 Spring 开发应用程序。在访问控制访问部分,我想使用 Spring Security Acl(我是 Acl 的新手)。我想基于两点在我的应用程序中实现 ACL:

  1. 应用程序应该有五个权限,readcreatemodifydeleteAdministrator
  2. 权限是层次化的,当一个用户有create权限时,它应该可以read,或者当它有modify权限时,它应该能够读取创建修改等。

这可能吗?怎么办?

更新:
我的应用程序基于 Spring MVC RESTFUL。当用户想要修改自己的信息时,他使用Ajax 发送一些json 数据。 json数据示例如下:

{
"id": 1,//user Id
"name": "my name",
"password": "my password",
"email": "email@email.com",
...
}

现在,恶意用户可以登录自己的帐户。该用户可以像所有其他用户一样修改其数据。在他发送数据之前,改变他的id,修改另一个账号的用户信息。我想使用 ACL 来防止这种颠覆性的工作。用户可以获得其他人的一些访问权限,其他人可以修改他的信息。

最佳答案

您可以使用 spring security 实现一个简单的解决方案。这个想法是创建一个实现 org.springframework.security.access.PermissionEvaluator 的类并重写 hasPermission 方法。看下一个例子:

@Component("permissionEvaluator")
public class PermissionEvaluator implements org.springframework.security.access.PermissionEvaluator {

/**
* @param authentication represents the user in question. Should not be null.
* @param targetDomainObject the domain object for which permissions should be
* checked. May be null in which case implementations should return false, as the null
* condition can be checked explicitly in the expression.
* @param permission a representation of the permission object as supplied by the
* expression system. Not null.
* @return true if the permission is granted, false otherwise
*/
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
if (authentication != null && permission instanceof String) {
User loggedUser = (User) authentication.getPrincipal();
String permissionToCheck = (String) permission;
// in this part of the code you need to check if the loggedUser has the "permission" over the
// targetDomainObject. In this implementation the "permission" is a string, for example "read", or "update"
// The targetDomainObject is an actual object, for example a object of UserProfile class (a class that
// has the profile information for a User)

// You can implement the permission to check over the targetDomainObject in the way that suits you best
// A naive approach:
if (targetDomainObject.getClass().getSimpleName().compareTo("UserProfile") == 0) {
if ((UserProfile) targetDomainObject.getId() == loggedUser.getId())
return true;
}
// A more robust approach: you can have a table in your database holding permissions to each user over
// certain targetDomainObjects
List<Permission> userPermissions = permissionRepository.findByUserAndObject(loggedUser,
targetDomainObject.getClass().getSimpleName());
// now check if in userPermissions list we have the "permission" permission.

// ETC...
}
//access denied
return false;
}

}

现在,通过此实现,您可以在您的服务层中使用 @PreAuthorize 注释,如下所示:

@PreAuthorize("hasPermission(#profile, 'update')")
public void updateUserProfileInASecureWay(UserProfile profile) {
//code to update user profile
}

@PreAuthorize 注释中的“hasPermission”从 updateUserProfileInASecureWay 方法的参数中接收 targetDomainObject #profile,我们还传递所需的权限(在本例中为“更新”)。

此解决方案通过实现“小型”ACL 避免了 ACL 的所有复杂性。也许它对您有用。

关于java - 如何实现自定义spring security acl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36696393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com