当我的域标识符为 String 类型时,如何使用 Spring Security ACL?

Dmi*_*kin 5 java spring-security-acl

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

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

String id = UUID.randomUUID().toString();
Run Code Online (Sandbox Code Playgroud)

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

ObjectIdentity identity = new ObjectIdentityImpl(clazz, id);
aclService.createAcl(identity);
Run Code Online (Sandbox Code Playgroud)

之后,我收到以下异常:

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

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

所以,问题是:

  1. 在这种情况下,最佳实践是什么(例如,我是否需要使用对象的哈希码作为标识符,或者其他什么)?
  2. 为什么Serializable到处都提到,但实际上它必须很长?

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

小智 4

已经过去三年多了,但我将把这个留给那些仍在为此苦苦挣扎的人:

从 2017-2018 年开始(特别是从这个提交https://github.com/spring-projects/spring-security/commit/6decf1c8ef8e31b0d9de9a2f2b364ce682d8b166#diff-bdb889847e56650fc7c52f9de584ba22开始)Spring security 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;
    }
Run Code Online (Sandbox Code Playgroud)

当使用上述配置时,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)
);
Run Code Online (Sandbox Code Playgroud)