shiro整合springboot报错--解决方法

代码下载

报错信息

//测试权限认证访问http://localhost:8080/myController/userLogin?name=zhangsan&pwd=123报错
org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.
	at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) ~[shiro-core-1.9.0.jar:1.9.0]
	at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:626) ~[shiro-core-1.9.0.jar:1.9.0]
	at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56) ~[shiro-core-1.9.0.jar:1.9.0]
	at com.lotus.shiro.controller.FirstController.controll(FirstController.java:26) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.31.jar:5.3.31]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.31.jar:5.3.31]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.31.jar:5.3.31]
	at org.springframework.web.servlet.mvc.method.annotation
<think>我们面对的问题是:SpringBoot与MybatisPlus集成时,由于依赖版本过高导致的错误。 根据用户提供的引用内容,我们可以知道MybatisPlus可以简化Mybatis的使用,提高开发效率。但是版本不兼容会导致错误。 解决方案思路: 1. 确定SpringBoot版本 2. 选择与SpringBoot版本兼容的MybatisPlus版本 3. 排除冲突的依赖(如果有) 步骤: 1. 查看当前SpringBoot项目的版本(在pom.xml中<parent>标签内) 2. 根据SpringBoot版本,选择对应的MybatisPlus版本。可以参考MybatisPlus官方文档的版本说明。 3. 在pom.xml中引入MybatisPlus依赖,并指定版本号。 4. 如果还存在其他依赖冲突,使用Maven的排除依赖功能(exclusion)排除冲突的依赖。 参考MybatisPlus官方文档的版本匹配说明: 例如,SpringBoot 2.2.x 对应 MybatisPlus 3.3.1 或 3.3.2 而SpringBoot 2.4.x 对应 MybatisPlus 3.4.0 以上 具体步骤: 步骤1:确定SpringBoot版本 例如,在pom.xml中: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <!-- 这里就是SpringBoot版本 --> <relativePath/> <!-- lookup parent from repository --> </parent> ``` 步骤2:根据SpringBoot版本选择MybatisPlus版本 可以查看MybatisPlus官方提供的版本对应关系:https://baomidou.com/guide/install.html#release 例如,对于SpringBoot 2.3.x,我们可以使用MybatisPlus 3.4.0以下版本,比如3.3.2。 步骤3:在pom.xml中引入MybatisPlus依赖,并指定版本 ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> <!-- 这里指定版本 --> </dependency> </dependencies> ``` 步骤4:如果引入后仍然有依赖冲突,可以使用exclusion排除冲突的依赖 例如,排除掉某个冲突的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> </exclusions> </dependency> ``` 另外,用户引用[2]中提到了集成MybatisPlus和Swagger,如果同时使用Swagger,也需要注意Swagger的版本兼容性。 补充:如果使用SpringBoot 2.5.x或更高版本,可以选择MybatisPlus 3.4.x或更高版本。例如: - SpringBoot 2.5.x: MybatisPlus 3.4.3.4 - SpringBoot 2.6.x: MybatisPlus 3.5.1 注意:具体版本对应关系请以官方文档为准。 如果无法确定,可以尝试在Maven仓库中查看mybatis-plus-boot-starter的版本,通常会有说明支持的SpringBoot版本。 示例:解决依赖版本过高的问题,我们通常选择降低依赖版本,但要注意与其他依赖的兼容性。 另外,用户引用[3]中提到了集成Shiro,如果同时集成Shiro,也要注意Shiro的版本兼容性。 总结:核心是版本匹配。如果版本不匹配,可能会出现各种奇怪的错误。 最后,建议在修改版本后,使用Maven命令清理并重新构建项目: ``` mvn clean install ``` 如果使用IDE,可以刷新Maven依赖。 如果问题仍然存在,可以查看具体的错误日志,进一步分析。</think>### SpringBoot与MybatisPlus集成依赖版本过高解决方案 当SpringBoot与MybatisPlus集成时,因依赖版本过高导致的常见错误包括: - `ClassNotFoundException`(类找不到) - `NoSuchMethodError`(方法不存在) - `BeanCreationException`(Bean创建失败) - 配置文件加载异常 #### 解决步骤: 1. **确定版本兼容性** - 查看MybatisPlus官方版本兼容表([MybatisPlus Release Notes](https://baomidou.com/pages/5b8340/)) - 示例兼容组合: - SpringBoot 2.3.x → MybatisPlus 3.4.0 - SpringBoot 2.5.x → MybatisPlus 3.4.3.4 - SpringBoot 2.7.x → MybatisPlus 3.5.2 2. **强制指定版本(pom.xml)** ```xml <properties> <mybatis-plus.version>3.4.3.4</mybatis-plus.version> <!-- 按需调整 --> </properties> <dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> <!-- 排除冲突依赖 --> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </exclusion> </exclusions> </dependency> </dependencies> ``` 3. **处理依赖冲突** ```shell # 查看依赖树定位冲突 mvn dependency:tree -Dincludes=com.baomidou,org.mybatis ``` 若存在冲突,在`<dependency>`中添加`<exclusions>`排除旧版本依赖。 4. **检查启动类注解** 确保添加`@MapperScan`注解: ```java @SpringBootApplication @MapperScan("com.example.mapper") // 指定Mapper接口路径 public class Application { ... } ``` 5. **验证配置(application.yml)** ```yaml mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 开启SQL日志 global-config: db-config: id-type: auto # ID生成策略 ``` #### 常见问题处理: 1. **`mybatis-spring`版本冲突** 添加显式依赖: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> <!-- 兼容版本 --> </dependency> ``` 2. **JDK版本不兼容** MybatisPlus 3.4+ 需JDK 1.8+,检查`pom.xml`配置: ```xml <properties> <java.version>1.8</java.version> </properties> ``` 3. **SpringBoot 3.0+特殊处理** 需使用MybatisPlus 3.5+并添加适配依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.3.1</version> </dependency> ``` > **验证成功标志**:启动时看到`MybatisPlus`版本日志: > ``` > Started Application in 3.215 seconds (JVM running for 4.512) > MybatisPlus v3.4.3.4 [Worker: main] > ``` > 若仍报错,检查`mvn clean install`后是否更新依赖[^1]。 #### 最佳实践: 1. 使用Spring Initializr创建项目时选择匹配版本 2. 新项目推荐组合: - SpringBoot 2.7.x + MybatisPlus 3.5.2 - SpringBoot 3.0.x + MybatisPlus 3.5.3.1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值