java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Shiro整合Springboot和redis,jwt过程错误shiroFilterChainDefinition

Shiro整合Springboot和redis,jwt过程中的错误shiroFilterChainDefinition问题

作者:文戌

这篇文章主要介绍了Shiro整合Springboot和redis,jwt过程中的错误shiroFilterChainDefinition问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Shiro整合Springboot和redis,jwt过程错误shiroFilterChainDefinition

在Shiro整合Springboot和redis,jwt过程中,编写完成ShiroConfig之后启动项目报错

Description:

Field shiroFilterChainDefinition in org.apache.shiro.spring.web.config.AbstractShiroWebFilterConfiguration required a bean of type 'org.apache.shiro.spring.web.config.ShiroFilterChainDefinition' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'org.apache.shiro.spring.web.config.ShiroFilterChainDefinition' in your configuration.

原因

使用了如下依赖之后,自定义的Realm竟然和authorizer冲突了

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring-boot-web-starter</artifactId>
            <version>1.4.0-RC2</version>
        </dependency>
        或者
        <dependency>
            <groupId>org.crazycake</groupId>
            <artifactId>shiro-redis-spring-boot-starter</artifactId>
            <version>3.2.1</version>
        </dependency>

解决方法

在ShiroConfig文件中的getShiroFilterFactoryBean方法上加注解@Bean(“shiroFilterFactoryBean”)

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

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

        return shiroFilterFactoryBean;
    }

如果无法解决,则在ShiroConfig中单独写一个ShiroFilterChainDefinition的bean

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

        // 添加自己的过滤器并且取名为jwt
        Map<String, Filter> filters  = new HashMap<>();
        //设置我们自定义的JWT过滤器
        filters .put("jwt", new JwtFilter());
        shiroFilterFactoryBean.setFilters(filters );
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        Map<String, String> filterMap = shiroFilterChainDefinition.getFilterChainMap();
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/**", "jwt"); // 主要通过注解方式校验权限
        chainDefinition.addPathDefinitions(filterMap);
        return chainDefinition;
    }

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文