• Consider defining a bean named 'authenticator' in your configuration.


    SpringBoot整合Shiro时出错:

    异常日志:

    o.s.b.d.LoggingFailureAnalysisReporter: 
    
    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Parameter 0 of method authorizationAttributeSourceAdvisor in com.csair.etm.manage.configuration.ShiroConfiguration required a bean named 'authenticator' that could not be found.
    
    
    Action:
    
    Consider defining a bean named 'authenticator' in your configuration.

    分析原因 :

         1: 你可能使用了网上的 SSM 整合 Shiro代码,

          2: 你使用了较新的Shiro 版本,

       不同的依赖解决的办法不用,当前有两种整合方式:

    解决办法一

          下面依赖解决办法

    <dependency>
                   <groupId>org.apache.shiro</groupId>
                   <artifactId>shiro-spring-boot-web-starter</artifactId>
                   <version>1.5.1</version>
               </dependency>

           上面的依赖 由于使用 了自动配置, 还导入了Servlet支持, 可能出现thymleaf不兼容的情况

       部分依赖如下:

    <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
         

    解决办法:

    import org.apache.shiro.mgt.SecurityManager;
    @Configuration
    public class ShiroConfig {
        private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
        @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
        logger.info("启动shiroFilter--时间是:" + new Date());
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //shiro拦截器
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();
        //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->
    
        // 配置不被拦截的资源及链接
        filterChainDefinitionMap.put("/static/**", "anon");
        // 退出过滤器
        filterChainDefinitionMap.put("/logout", "logout");
    
        // 如果不设置默认会自动寻找Web工程根目录下的"/login"页面,即本文使用的login.html
        shiroFilterFactoryBean.setLoginUrl("/login");
        // 登录成功后要跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/index");
    
        //未授权界面
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
    
        //配置需要认证权限的
        filterChainDefinitionMap.put("/**", "authc");
    
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
    
        //自定义身份认证Realm(包含用户名密码校验,权限校验等)
        @Bean
        public MyShiroRealm myShiroRealm(){
            MyShiroRealm myShiroRealm = new MyShiroRealm();
            return myShiroRealm;
        }
    
    
        @Bean(name = "securityManager")// 容器中自动配置了SecurityManager  所以我们使用SessionSecurityManager 覆盖
        public SessionSecurityManager securityManager(){
            DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();
            securityManager.setRealm(myShiroRealm());
            return securityManager;
        }
    
        //开启shiro aop注解支持,不开启的话权限验证就会失效
        @Bean
        public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
            AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
            authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
            return authorizationAttributeSourceAdvisor;
        }
    
        //配置异常处理,不配置的话没有权限后台报错,前台不会跳转到403页面
        @Bean(name="simpleMappingExceptionResolver")
        public SimpleMappingExceptionResolver
        createSimpleMappingExceptionResolver() {
            SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
            Properties mappings = new Properties();
            mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理
            mappings.setProperty("UnauthorizedException","403");
            simpleMappingExceptionResolver.setExceptionMappings(mappings);  // None by default
            simpleMappingExceptionResolver.setDefaultErrorView("error");    // No default
            simpleMappingExceptionResolver.setExceptionAttribute("ex");     // Default is "exception"
            return simpleMappingExceptionResolver;
        }
    }

      application.xml 增加配置

    spring.main.allow-bean-definition-overriding=true


    解决办法二:

        如果你导入了 以下依赖

    <dependency>
               <groupId>org.apache.shiro</groupId>
               <artifactId>shiro-spring</artifactId>
               <version>1.5.1</version>
          </dependency>


    则使用下面的配置文件:

    @Configuration
    public class ShiroConfig {
    
        private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
        @Bean("shiroFilterFactoryBean")
        public ShiroFilterFactoryBean shirFilter(@Qualifier("securityManager")SecurityManager securityManager) {
            logger.info("启动shiroFilter--时间是:" + new Date());
            ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
            shiroFilterFactoryBean.setSecurityManager(securityManager);
            //shiro拦截器
            Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();
            //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
            //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->
    
    
            // 如果不设置默认会自动寻找Web工程根目录下的"/login"页面,即本文使用的login.html
            shiroFilterFactoryBean.setLoginUrl("/login");
            // 登录成功后要跳转的链接
            shiroFilterFactoryBean.setSuccessUrl("/main");
            //错误页面,认证不通过跳转
            shiroFilterFactoryBean.setUnauthorizedUrl("/error");
            //未授权界面
            shiroFilterFactoryBean.setUnauthorizedUrl("/403");
    
    
            // 配置不被拦截的资源及链接
            filterChainDefinitionMap.put("/static/**", "anon");
            // 退出过滤器
            filterChainDefinitionMap.put("/logout", "logout");
            //开启注册页面不需要权限
            filterChainDefinitionMap.put("/register", "anon");
    
            filterChainDefinitionMap.put("/user/login", "anon");
            filterChainDefinitionMap.put("/user/register", "anon");
    
            //配置需要认证权限的
            filterChainDefinitionMap.put("/user/**", "authc");
    
    
            shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
            return shiroFilterFactoryBean;
        }
    
        /**
         * 配置shiro的生命周期
         * @return
         */
        @Bean
        public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
            return new LifecycleBeanPostProcessor();
        }
    
        //自定义身份认证Realm(包含用户名密码校验,权限校验等)
        @Bean
        public ShiroRealm myShiroRealm(){
            ShiroRealm myShiroRealm = new ShiroRealm();
            return myShiroRealm;
        }
    
        @Bean
        public ShiroDialect shiroDialect() {
            return new ShiroDialect();
        }
    
        @Bean(name = "securityManager")
        public SecurityManager securityManager(){
            DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();
            securityManager.setRealm(myShiroRealm());
            return securityManager;
        }
    
        //开启shiro aop注解支持,不开启的话权限验证就会失效
        @Bean
        public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
            DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
            defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
            return defaultAdvisorAutoProxyCreator;
        }
    
        @Bean
        public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) {
            AuthorizationAttributeSourceAdvisor sourceAdvisor = new AuthorizationAttributeSourceAdvisor();
            sourceAdvisor.setSecurityManager(securityManager);
            return sourceAdvisor;
        }
    
        //配置异常处理,不配置的话没有权限后台报错,前台不会跳转到403页面
        @Bean(name="simpleMappingExceptionResolver")
        public SimpleMappingExceptionResolver
        createSimpleMappingExceptionResolver() {
            SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
            Properties mappings = new Properties();
            mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理
            mappings.setProperty("UnauthorizedException","403");
            simpleMappingExceptionResolver.setExceptionMappings(mappings);  // None by default
            simpleMappingExceptionResolver.setDefaultErrorView("403");    // No default
            simpleMappingExceptionResolver.setExceptionAttribute("ex");     // Default is "exception"
            return simpleMappingExceptionResolver;
        }
    }
  • 相关阅读:
    Could not obtain transaction-synchronized Session for current thread
    Spring 具名参数NamedParameterJdbcTemplate
    Could not load driverClass jdbc:mysql://localhost:3306/spring
    Spring 使用AspectJ的三种方式
    Excel 若某列包含某个字符则特定列显示特定字符
    使用Spring的隐式注解和装配以及使用SpringTest框架
    Eclipse Oxygen 解决 自动导包的问题
    C3P0 WARN: Establishing SSL connection without server's identity verification is not recommended
    Eclipse 多行复制并且移动失效
    正则表达式 使用代码
  • 原文地址:https://www.cnblogs.com/dgwblog/p/12584411.html
Copyright © 2020-2023  润新知