org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code
关于SpringBoot整合Shiro时遇到的:调用代码无法访问 SecurityManager 错误
我的解决方式:
在 DefaultWebSecurityManager 类中添加以下代码
SecurityUtils.setSecurityManager(defaultWebSecurityManager);
//配置SecurityManager
@Bean
public DefaultWebSecurityManager defaultWebSecurityManager(){
//创建 defaultWebSecurityManager 对象
DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
//创建加密对象 设置相关属性
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//采用md5加密
hashedCredentialsMatcher.setHashAlgorithmName("md5");
//采用迭代加密
hashedCredentialsMatcher.setHashIterations(3);
//将加密对象存储到 myRealm
myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
//将myRealm存入defaultWebSecurityManager对象
defaultWebSecurityManager.setRealm(myRealm);
//将SecurityManager实例(这里是defaultWebSecurityManager)设置到SecurityUtils中。
SecurityUtils.setSecurityManager(defaultWebSecurityManager);
//返回
return defaultWebSecurityManager;
}