SoFunction
Updated on 2025-03-08

Shiro integrates Springboot and redis,jwt error shiroFilterChainDefinition problem

Shiro integrates Springboot and redis, jwt process error shiroFilterChainDefinition

During the process of integrating Springboot and redis, jwt, after writing ShiroConfig, start the project and report an error

Description:

Field shiroFilterChainDefinition in required a bean of type '' that could not be found.

The injection point has the following annotations:
    - @(required=true)

Action:

Consider defining a bean of type '' in your configuration.

reason

After using the following dependencies, the customized Realm actually conflicts with the authorizer

        <dependency>
            <groupId></groupId>
            <artifactId>shiro-spring-boot-web-starter</artifactId>
            <version>1.4.0-RC2</version>
        </dependency>
        or
        <dependency>
            <groupId></groupId>
            <artifactId>shiro-redis-spring-boot-starter</artifactId>
            <version>3.2.1</version>
        </dependency>

Solution

Annotate @Bean("shiroFilterFactoryBean") on the getShiroConfig file

@Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        (defaultWebSecurityManager);

        Map<String,String> map = new HashMap<>();
        ("/user/login","anon");
        ("/**", "authc");
        (map);

        return shiroFilterFactoryBean;
    }

If it cannot be resolved, write a separate bean of ShiroFilterChainDefinition in ShiroConfig

@Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager,ShiroFilterChainDefinition shiroFilterChainDefinition){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        (defaultWebSecurityManager);

        // Add your own filter and name it jwt        Map&lt;String, Filter&gt; filters  = new HashMap&lt;&gt;();
        //Set our custom JWT filter        filters .put("jwt", new JwtFilter());
        (filters );
        (defaultWebSecurityManager);

        Map&lt;String, String&gt; filterMap = ();
        (filterMap);

        return shiroFilterFactoryBean;
    }

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        Map&lt;String, String&gt; filterMap = new LinkedHashMap&lt;&gt;();
        ("/**", "jwt"); // Mainly verify permissions through annotation        (filterMap);
        return chainDefinition;
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.