No cache or cacheManager properties have been set. Authorization cache cannot be obtained.

参考:点击打开链接


问题:使用Shiro授权时制台在取得权限的时候老是打印出:org.apache.shiro.realm.AuthorizingRealm:248 - No cache or cacheManager properties have been set.  Authorization cache cannot be obtained.


原因是:没有配置权限缓存,但是在不配置权限缓存时需要怎么解决呢?


解决方法:关闭授权缓存域,把authorizationCachingEnabled设置为false。

<bean id="roleBaseAuthorizingRealm" class="com.company.web.RoleBaseAuthorizingRealm">
		<property name="adminService" ref="adminService"/>
	    <property name="authorizationCachingEnabled" value="false"/>
	</bean>



### 解决 Spring Framework 中 CacheManager 注入失败的问题 Spring Framework 的缓存管理器 (`CacheManager`) 是用于管理和提供缓存实例的核心组件之一。如果 `CacheManager` 注入失败,通常可能是由于配置错误、依赖冲突或其他上下文初始化问题引起的。 以下是可能导致注入失败的原因以及解决方案: #### 1. 配置不完整或错误 确保在 Spring 上下文中正确声明并定义了 `CacheManager` 实现类。例如,在 XML 配置文件中可以这样设置 Guava 缓存管理器[^2]: ```xml <bean id="cacheManager" class="org.springframework.cache.guava.GuavaCacheManager"> <property name="caches"> <set> <value>default</value> <value>books</value> </set> </property> </bean> ``` 对于基于 Java Config 的方式,则可以通过如下代码实现: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.google.common.cache.CacheBuilder; @Configuration public class CacheConfig { @Bean public org.springframework.cache.CacheManager cacheManager() { return new org.springframework.cache.concurrent.ConcurrentMapCacheManager("default", "books"); } } ``` #### 2. 缺少必要的依赖项 确认项目中已引入所需的库版本。例如,如果您使用的是 Guava 缓存管理器,则需要添加以下 Maven 或 Gradle 依赖项: Maven: ```xml <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1-jre</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> ``` Gradle: ```gradle implementation 'com.google.guava:guava:30.1-jre' implementation 'org.springframework.boot:spring-boot-starter-cache' ``` #### 3. 方法级缓存未启用 当您尝试通过注解(如 `@Cacheable`)来利用缓存功能时,需确保启用了方法级别的缓存支持。可以在主应用程序类上标注 `@EnableCaching` 来激活此特性[^1]: ```java import org.springframework.cache.annotation.EnableCaching; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableCaching public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` #### 4. 多个 CacheManager 定义引发歧义 如果有多个 `CacheManager` 被注册到 Spring 应用程序上下文中而没有指定默认使用的那个,可能会导致注入失败。在这种情况下,应明确指明哪个 `CacheManager` 将被优先考虑。例如,您可以为特定的方法或者字段显式地注入所需的一个 `CacheManager` 实例: ```java @Autowired @Qualifier("customCacheManager") private CacheManager customCacheManager; ``` 另外还可以通过自定义 Bean 名称的方式区分不同的缓存管理者对象。 --- ### 总结 上述分析涵盖了常见的几种原因及其对应的修复措施。实际操作过程中可以根据具体的异常堆栈信息进一步排查具体位置和细节上的差异之处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值