因爲我找不到任何內建的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);
}
}
}
來源
2013-10-09 13:13:26
JWK