admin管理员组

文章数量:1130349

spring整合shiro出现Consider renaming one of the beans or enabling overriding by setting和Consider defining a bean named ‘authenticator’ in your configuration.问题

用spring boot整合shiro是出现如下问题:

配置文件如下:

package com.hdit.demo.util;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class Config {

    @Bean
    public MyRealm myShiroRealm() {
        MyRealm myRealm = new MyRealm();
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return myRealm;
    }
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }
    //MD5加密
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //散列算法
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //散列计算次数
        hashedCredentialsMatcher.setHashIterations(2);
        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }
    //Filter工厂,设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //设置拦截器
        Map<String, String> filterMap = new LinkedHashMap<String,String>();
        filterMap.put("/user/text","authc");
        filterMap.put("/user/text2","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        //登陆验证失败跳转页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthor.html");
        shiroFilterFactoryBean.setLoginUrl("/login.html");
        return shiroFilterFactoryBean;
    }

}

导入的依赖是

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

当根据错误将spring.main.allow-bean-definition-overriding=true这句话写入application中后又出现如下错误

解决方法

第一种解决方法:更换pom.xml中导入的依赖包将

更换为

第二种解决方法:,不用更改pom.xml中的依赖,更改shiro配置文件代码将

返回值类型SecurityMannager改为他的子类DefaultWebSecurityManager

最后成功运行

spring整合shiro出现Consider renaming one of the beans or enabling overriding by setting和Consider defining a bean named ‘authenticator’ in your configuration.问题

用spring boot整合shiro是出现如下问题:

配置文件如下:

package com.hdit.demo.util;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class Config {

    @Bean
    public MyRealm myShiroRealm() {
        MyRealm myRealm = new MyRealm();
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return myRealm;
    }
    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }
    //MD5加密
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //散列算法
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //散列计算次数
        hashedCredentialsMatcher.setHashIterations(2);
        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }
    //Filter工厂,设置对应的过滤条件和跳转条件
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //设置拦截器
        Map<String, String> filterMap = new LinkedHashMap<String,String>();
        filterMap.put("/user/text","authc");
        filterMap.put("/user/text2","authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        //登陆验证失败跳转页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthor.html");
        shiroFilterFactoryBean.setLoginUrl("/login.html");
        return shiroFilterFactoryBean;
    }

}

导入的依赖是

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

当根据错误将spring.main.allow-bean-definition-overriding=true这句话写入application中后又出现如下错误

解决方法

第一种解决方法:更换pom.xml中导入的依赖包将

更换为

第二种解决方法:,不用更改pom.xml中的依赖,更改shiro配置文件代码将

返回值类型SecurityMannager改为他的子类DefaultWebSecurityManager

最后成功运行

本文标签: ShirorenamingSpringbootoverriding