栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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冲突了

        
            org.apache.shiro
            shiro-spring-boot-web-starter
            1.4.0-RC2
        
        或者
        
            org.crazycake
            shiro-redis-spring-boot-starter
            3.2.1
        

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

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

        Map 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 filters  = new HashMap<>();
        //设置我们自定义的JWT过滤器
        filters .put("jwt", new JwtFilter());
        shiroFilterFactoryBean.setFilters(filters );
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        Map filterMap = shiroFilterChainDefinition.getFilterChainMap();
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }

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

详细分析见:https://www.cnblogs.com/insaneXs/p/11028286.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770093.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号