5

尝试使用MutableAclService.createAcl(ObjectIdentity objectIdentity).

问题是ObjectIdentity使用Serializable类型作为标识符。同时,我的域String为此目的使用类型。id 是这样生成的:

String id = UUID.randomUUID().toString();

然后我尝试使用以下结构添加 ACL:

ObjectIdentity identity = new ObjectIdentityImpl(clazz, id);
aclService.createAcl(identity);

之后我得到以下异常:

java.lang.NumberFormatException:对于输入字符串:“ad169805-a2d1-4324-ba11-c98cc679e594”

我发现 Spring Security ACL 使用Long类型作为标识符。

所以,问题是:

  1. 在这种情况下,最佳实践是什么(我是否需要使用,例如,我的对象的哈希码作为标识符,或者其他)?
  2. 为什么Serializable处处提到,而实际上却一定是长篇大论?

PS 标识符的 SQL 数据类型也是数字 - bigserial。

4

1 回答 1

1

已经三年多了,但我会把这个留给还在为此苦苦挣扎的人:

截至 2017-2018 年(特别是从这个提交https://github.com/spring-projects/spring-security/commit/6decf1c8ef8e31b0d9de9a2f2b364ce682d8b166#diff-bdb889847e56650fc7c52f9de584ba22等开始)Spring 安全 ACL 开始实施类来解决这个问题。

我目前正在使用 Spring Security ACL 5.2.2.RELEASE,它将这个问题的解决方案缩小到 2 个简单的配置修改:

  @Bean
    public LookupStrategy lookupStrategy() {
        BasicLookupStrategy basicLookupStrategy = new BasicLookupStrategy(
                dataSource,
                aclCache(),
                aclAuthorizationStrategy(),
                new ConsoleAuditLogger()
        );
        basicLookupStrategy.setAclClassIdSupported(true); // <--- this line
        return basicLookupStrategy;
    }

    @Bean
    public JdbcMutableAclService aclService() {
        JdbcMutableAclService jdbcMutableAclService = new JdbcMutableAclService(dataSource,lookupStrategy(),aclCache());
        jdbcMutableAclService.setAclClassIdSupported(true); //<-- And this line.
        return jdbcMutableAclService;
    }

使用上述配置时,spring acl 假设您的表“acl_class”中有一个名为“class_id_type”的额外字段,其中包含您的实体 ID 的类型信息。例如我对这个表的 PostgreSQL 定义如下:

create table if not exists acl_class(
    id bigserial not null primary key,
    class varchar(100) not null,
    class_id_type varchar(100),
    constraint unique_uk_2 unique(class)
);
于 2020-04-30T12:22:27.240 回答