Springboot整合Shiro的错误记录:Could not autowire. There is more than one bean of ‘Realm‘ type.

今天在学习springboot整合shiro,在学习到创建安全管理器的时候出现了如下的错误,刚开始百思不得其解?
Springboot整合Shiro的错误记录:Could not autowire. There is more than one bean of ‘Realm‘ type._第1张图片
Springboot整合Shiro的错误记录:Could not autowire. There is more than one bean of ‘Realm‘ type._第2张图片我用我蹩脚的四级英语大概的读了一下,意思是无法自动配置,“realm”类型的bean不止一个 ,原来是类型为realm的类不止一个,spring无法自动注入。

package com.config;



import com.shiro.BaseRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.HashMap;

//用来整合sshiro相关的类
@Configuration
public class ShiroConfigBase {
     

    //1.创建shiro
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultSecurityManager defaultSecurityManager){
     
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给filter设置安全管理器,负责拦截所有请求
        shiroFilterFactoryBean.setSecurityManager(defaultSecurityManager);
        //配置系统受限资源
        HashMap<String, String> map = new HashMap<>();
        map.put("/index.jsp","authc");//authc代表请求这个资源需要认证和授权
        shiroFilterFactoryBean.setLoginUrl("/login.jsp");//设置默认认证路径
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        //配置系统公共资源
        return shiroFilterFactoryBean;
    }
    //2.创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager( Realm realm){
     
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //给安全管理器设置realm
        defaultWebSecurityManager.setRealm(realm);
        return defaultWebSecurityManager;
    }
    //3.创建自定义realm
   @Bean
    public Realm getRealm(){
     
        BaseRealm baseRealm = new BaseRealm();
        return baseRealm;
   }
}

一开始我还觉得这个错误应该是idea编译器的问题,面向百度编程,百度了一下,在网上有这个问题的小伙伴少之又少,于是我开始自己动手解决,解决办法也很简单,我需要注入的是下面getRealm类,于是我在爆红的地方加了一个注解***@Qualifier(“getRealm”)***,于是乎完美运行

package com.config;



import com.shiro.BaseRealm;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.HashMap;

//用来整合sshiro相关的类
@Configuration
public class ShiroConfigBase {
     

    //1.创建shiro
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultSecurityManager defaultSecurityManager){
     
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给filter设置安全管理器,负责拦截所有请求
        shiroFilterFactoryBean.setSecurityManager(defaultSecurityManager);
        //配置系统受限资源
        HashMap<String, String> map = new HashMap<>();
        map.put("/index.jsp","authc");//authc代表请求这个资源需要认证和授权
        shiroFilterFactoryBean.setLoginUrl("/login.jsp");//设置默认认证路径
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        //配置系统公共资源
        return shiroFilterFactoryBean;
    }
    //2.创建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("getRealm") Realm realm){
     
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //给安全管理器设置realm
        defaultWebSecurityManager.setRealm(realm);
        return defaultWebSecurityManager;
    }
    //3.创建自定义realm
   @Bean
    public Realm getRealm(){
     
        BaseRealm baseRealm = new BaseRealm();
        return baseRealm;
   }
}

Springboot整合Shiro的错误记录:Could not autowire. There is more than one bean of ‘Realm‘ type._第3张图片

你可能感兴趣的:(踩坑,shiro,spring,spring,boot,bug)