Spring Boot 3升级后Spring ACL相关类适配问题求助
Spring Boot 3 下 Spring ACL 缓存适配方案
核心问题原因
Spring Boot 3 配套的 Spring Security 6+ 移除了对 EhCache 2.x 的原生支持,原有的 EhCacheFactoryBean、EhCacheManagerFactoryBean、EhCacheBasedAclCache 类已被废弃或移除,转而推荐使用 EhCache 3.x 或其他符合 Spring Cache 抽象的缓存实现。
方案1:切换到 EhCache 3.x
1. 替换依赖
移除原 EhCache 2.x 依赖,添加 EhCache 3.x 及 Spring 适配依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>
2. 配置 EhCache 3.x 缓存管理器
不再使用旧的工厂Bean,改用 JCacheCacheManager 对接 EhCache 3:
@Configuration @EnableCaching public class AclCacheConfig { @Bean public CacheManager cacheManager() throws URISyntaxException { return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager( getClass().getResource("/ehcache.xml").toURI(), getClass().getClassLoader() )); } // 自定义 ACL 缓存实现,替代 EhCacheBasedAclCache @Bean public AclCache aclCache(CacheManager cacheManager, AclAuthorizationStrategy aclAuthorizationStrategy, PermissionGrantingStrategy permissionGrantingStrategy) { Cache aclCache = cacheManager.getCache("aclCache"); return new SpringCacheBasedAclCache( aclCache, permissionGrantingStrategy, aclAuthorizationStrategy ); } }
3. 编写 EhCache 3.x 配置文件(ehcache.xml)
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ehcache.org/v3" xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd"> <cache alias="aclCache"> <key-type>org.springframework.security.acls.domain.ObjectIdentityImpl</key-type> <value-type>org.springframework.security.acls.domain.AclImpl</value-type> <expiry> <ttl unit="minutes">30</ttl> </expiry> <heap unit="entries">1000</heap> </cache> </config>
方案2:改用 Caffeine 缓存(轻量替代)
如果不想依赖 EhCache,Caffeine 是 Spring Boot 默认推荐的缓存实现,配置更简洁:
1. 添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency>
2. 配置 ACL 缓存
@Configuration @EnableCaching public class AclCacheConfig { @Bean public CacheManager cacheManager() { CaffeineCacheManager cacheManager = new CaffeineCacheManager("aclCache"); cacheManager.setCaffeine(Caffeine.newBuilder() .expireAfterWrite(30, TimeUnit.MINUTES) .maximumSize(1000)); return cacheManager; } @Bean public AclCache aclCache(CacheManager cacheManager, AclAuthorizationStrategy aclAuthorizationStrategy, PermissionGrantingStrategy permissionGrantingStrategy) { Cache aclCache = cacheManager.getCache("aclCache"); return new SpringCacheBasedAclCache( aclCache, permissionGrantingStrategy, aclAuthorizationStrategy ); } }
后续 ACL 配置调整
原有的 ACL 数据源、权限策略等配置基本无需改动,只需确保 AclService 注入新的 AclCache 实例:
@Bean public JdbcMutableAclService aclService(DataSource dataSource, AclCache aclCache, PermissionGrantingStrategy permissionGrantingStrategy) { JdbcMutableAclService service = new JdbcMutableAclService(dataSource, aclCache, permissionGrantingStrategy); // 保留原有的 lookupStrategy、AclClassService 等配置 return service; }
内容的提问来源于stack exchange,提问作者Cristhian Won
相关产品推荐
相关产品推荐

