ssm 加入shiro 后报错

在SSM(Spring、SpringMVC、MyBatis)框架中集成Shiro进行权限管理时,可能会遇到各种报错问题,以下是一些常见报错及其解决方法:

ssm 加入shiro 后报错
(图片来源网络,侵删)

1、Bean定义重复

在整合Shiro时,可能会遇到Bean定义重复的问题,在配置文件中定义了SecurityManager,而Shiro的自动配置也定义了相同的Bean,这会导致启动时抛出异常:

The bean 'securityManager', defined in class path resource [org/apache/shiro/spring/config/web/autoconfigure/ShiroWebAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/xlhj/shiro/config/ShiroConfig.class] and overriding is disabled.

解决方法:

更改自定义配置文件中的Bean名称,避免与Shiro自动配置的Bean名称相同。

application.propertiesapplication.yml文件中设置spring.main.allowbeandefinitionoverriding=true,允许覆盖Bean定义。

2、认证失败

在集成Shiro进行认证时,可能会遇到认证失败的错误:

Submitted credentials for token [org.apache.shiro.authc.UsernamePasswordToken 1300, rememberMefalse] did not match the expected credentials.

解决方法:

确认用户名和密码是否正确。

检查自定义Realm中的认证方法是否正确实现,例如密码加密和比对逻辑是否正确。

查看日志,分析认证失败的具体原因。

3、NoClassDefFoundError

在配置自定义Realm时,可能会遇到以下错误:

java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

解决方法:

检查项目依赖是否完整,确保已导入相关依赖包(如commonslogging)。

4、IllegalArgumentException: Odd number of characters

在配置Shiro密码加密时,可能会遇到以下错误:

java.lang.IllegalArgumentException: Odd number of characters.

解决方法:

确保在doGetAuthenticationInfo方法中,使用SimpleAuthenticationInfo时传入的密码是加密后的密码,而不是明文密码。

5、No SecurityManager accessible

在配置Shiro Filter时,可能会遇到以下错误:

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.

解决方法:

确认在web.xml中已正确配置Shiro Filter。

检查Shiro Filter的配置顺序,确保Shiro Filter位于其他Filter之前,如Struts2 Filter。

如果使用Spring,检查applicationContextshiro.xml中是否已正确配置SecurityManager

SSM框架中集成Shiro时,遇到报错问题首先要查看日志,确定具体的错误原因,然后根据错误提示,分析可能的解决方案,常见的报错问题主要包括Bean定义重复、认证失败、依赖缺失、配置错误等,在解决报错问题时,要注意代码细节和配置文件的正确性,同时也要关注框架的版本兼容性,通过不断调试和优化,最终能够成功整合Shiro,实现项目的权限管理功能。

原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/361831.html

(0)
酷盾叔订阅
上一篇 2024-03-21 20:52
下一篇 2024-03-21 20:53

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购  >>点击进入