由于我找不到任何内置的 Spring 支持,我最终得到了以下自定义代码。不是最有效的,但它可以满足我的需要。
如果有人知道更好的解决方案或有改进以下代码的建议,请告诉我。
自定义 JDBC ACL 服务以按类型 (acl_class) 检索所有现有对象身份:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.acls.domain.GrantedAuthoritySid;
import org.springframework.security.acls.domain.ObjectIdentityImpl;
import org.springframework.security.acls.domain.PrincipalSid;
import org.springframework.security.acls.model.ObjectIdentity;
import org.springframework.security.acls.model.Sid;
import javax.sql.DataSource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class CustomJdbcAclService {
private final JdbcTemplate jdbcTemplate;
private final static String SELECT_EXISTING_BY_CLASS_AND_SID =
"SELECT DISTINCT " +
"acl_object_identity.object_id_identity " +
"FROM acl_object_identity " +
"LEFT JOIN acl_entry on acl_entry.acl_object_identity = acl_object_identity.id " +
"LEFT JOIN acl_class on acl_class.id = acl_object_identity.object_id_class " +
"LEFT JOIN acl_sid on acl_entry.sid = acl_sid.id " +
"WHERE acl_class.class = ? && acl_sid.sid = ? && acl_entry.granting = 1";
public CustomJdbcAclService(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public List<ObjectIdentity> getExistingObjectIdentities(String type, Sid sid) {
String sidName = getSidName(sid);
List<Map<String, Object>> results = jdbcTemplate.queryForList(SELECT_EXISTING_BY_CLASS_AND_SID, type, sidName);
List<ObjectIdentity> oids = new ArrayList<>();
for (Map<String, Object> result : results) {
oids.add(new ObjectIdentityImpl(type, (Serializable) result.get("object_id_identity")));
}
return oids;
}
private String getSidName(Sid sid) {
String sidName;
if (sid instanceof PrincipalSid) {
sidName = ((PrincipalSid) sid).getPrincipal();
} else if (sid instanceof GrantedAuthoritySid) {
sidName = ((GrantedAuthoritySid) sid).getGrantedAuthority();
} else {
throw new IllegalArgumentException("Unsupported implementation of Sid");
}
return sidName;
}
}
我从另一个 ACL 服务调用上述内容:
public class MyAclService {
public void grantPermissionsToExisting(String type, Sid securityIdentity, Permission... permissions) {
List<ObjectIdentity> existingOids = customJdbcAclService.getExistingObjectIdentities(type, securityIdentity);
for (ObjectIdentity oid : existingOids) {
// grantPermissions() gets the ACL of the object identity and inserts the new ACEs
grantPermissions(oid, securityIdentity, permissions);
}
}
}