在SpringBoot中使用Shiro发生循环依赖

问题

下图是控制台的报错信息,产生了循环依赖。
在这里插入图片描述

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   objectMapperConfigurer defined in class path resource [springfox/documentation/spring/web/SpringfoxWebMvcConfiguration.class]
      ↓
   authorizationAttributeSourceAdvisor defined in class path resource [org/apache/shiro/spring/boot/autoconfigure/ShiroAnnotationProcessorAutoConfiguration.class]
┌─────┐
|  defaultWebSecurityManager defined in class path resource [com/zust/ysc/config/shiro/ShiroConfig.class]
↑     ↓
|  shiroFilterFactoryBean defined in class path resource [com/zust/ysc/config/shiro/ShiroConfig.class]
└─────┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

原因

具体原因找了很久也没有找到,网上有一些解决方式,但都没有说原因,我也不浪费时间了。有些人提到了下面这两个依赖的问题,问题确实是在这里。我最初学Shiro是看的狂神的视频,他用的是第二个依赖,我自己实现时自作聪明换成了第一个,所以就报了上面的问题。这两个依赖去实现ShiroConfig类所要实现的方法是不一样的。比如第二个依赖需要实现ShiroFilterFactoryBean,而第一个不需要这个,而是ShiroFilterChainDefinition,具体可以去看官网,网上也有基于第一个依赖实现的方式。

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

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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot项目使用Shiro实现接口授权,需要进行以下步骤: 1. 添加Shiro依赖 在pom.xml文件添加Shiro依赖: ``` <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.5.0</version> </dependency> ``` 2. 配置Shiro 在Spring Boot的配置文件添加Shiro的配置,如下所示: ``` shiro: filter-chain-definitions: /** = anon /login = anon /logout = logout /api/** = authc security-manager: realm: type: org.apache.shiro.realm.jdbc.JdbcRealm authentication-query: SELECT password FROM users WHERE username = ? user-roles-query: SELECT role_name FROM user_roles WHERE username = ? permissions-query: SELECT permission FROM roles_permissions WHERE role_name = ? ``` 该配置文件配置了Shiro的过滤链,以及Shiro的安全管理器和Realm。 3. 编写Shiro Realm 编写一个继承自JdbcRealm的Realm类,并实现doGetAuthenticationInfo()和doGetAuthorizationInfo()两个方法,如下所示: ``` public class MyRealm extends JdbcRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); String password = new String(upToken.getPassword()); // 根据用户名和密码查询数据库,如果查询到了用户,则返回一个封装了该用户信息的AuthenticationInfo对象 // 如果没有查询到用户,则返回null } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); String username = (String) principals.getPrimaryPrincipal(); // 根据用户名查询用户的角色和权限信息,并将其添加到authorizationInfo return authorizationInfo; } } ``` 4. 配置ShiroFilterFactoryBean 在Spring Boot的配置文件配置ShiroFilterFactoryBean,如下所示: ``` @Bean public ShiroFilterFactoryBean shiroFilterFactoryBean(@Autowired MyRealm myRealm) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(new DefaultWebSecurityManager(myRealm)); Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); filterChainDefinitionMap.put("/login", "anon"); filterChainDefinitionMap.put("/logout", "logout"); filterChainDefinitionMap.put("/api/**", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } ``` 该配置文件配置了一个ShiroFilterFactoryBean,并将其与安全管理器和过滤链绑定在一起。 5. 编写接口控制器 编写一个接口控制器,并在该控制器添加需要授权的接口方法,如下所示: ``` @RestController public class ApiController { @GetMapping("/api/hello") public String hello() { return "Hello, world!"; } @RequiresRoles("admin") @PostMapping("/api/admin") public String admin() { return "Hello, admin!"; } } ``` 上述代码,hello()方法不需要授权,而admin()方法需要授予admin角色才能访问。 6. 测试接口授权 启动Spring Boot应用程序,并使用curl或Postman等工具测试接口授权。例如,可以使用以下命令测试admin接口: ``` curl -X POST http://localhost:8080/api/admin -H 'Authorization: Basic YWRtaW46YWRtaW4=' ``` 其,Authorization头的值是用户名和密码的Base64编码,上述例子的用户名和密码都是admin。如果授权成功,服务器将返回"Hello, admin!"。如果授权失败,则返回401 Unauthorized错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值