我是靠谱客的博主 无语手机,这篇文章主要介绍Shiro异常java.lang.IllegalArgumentException: Odd number of characters的解决方案,现在分享给大家,希望可以做个参考。

最近在做前后端分离,登录认证部分用到了Shiro,配置MD5加盐加密后,在登录的时候抛出的以下异常:

复制代码
1
2
3
4
5
6
7
8
java.lang.IllegalArgumentException: Odd number of characters. at org.apache.shiro.codec.Hex.decode(Hex.java:128) ~[shiro-core-1.3.2.jar:1.3.2] at org.apache.shiro.codec.Hex.decode(Hex.java:107) ~[shiro-core-1.3.2.jar:1.3.2] at org.apache.shiro.codec.Hex.decode(Hex.java:95) ~[shiro-core-1.3.2.jar:1.3.2] at org.apache.shiro.authc.credential.HashedCredentialsMatcher.getCredentials(HashedCredentialsMatcher.java:353) ~[shiro-core-1.3.2.jar:1.3.2] at org.apache.shiro.authc.credential.HashedCredentialsMatcher.doCredentialsMatch(HashedCredentialsMatcher.java:380) ~[shiro-core-1.3.2.jar:1.3.2] at org.apache.shiro.realm.AuthenticatingRealm.assertCredentialsMatch(AuthenticatingRealm.java:597) ~[shiro-core-1.3.2.jar:1.3.2] ...

对应Realm的代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class CustomRealm extends AuthorizingRealm { @Autowired UserMapper usermapper; @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; String username = String.valueOf(token.getUsername()); String password = String.valueOf(token.getPassword()); // 从数据库获取对应用户名的用户 User user = usermapper.findByUsername(username); if (user == null) { throw new AccountException("不存在此用户"); } // 获取盐值,即用户名 ByteSource salt = ByteSource.Util.bytes(username); return new SimpleAuthenticationInfo(token.getPrincipal(), password, salt, getName()); } }

错误就在最后一句代码:

复制代码
1
return new SimpleAuthenticationInfo(token.getPrincipal(), password, salt, getName());

在这里直接把明文密码当成参数传入了构造器

解决方案:将MD5加密后的密码传入构造器

修改后代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class CustomRealm extends AuthorizingRealm { @Autowired UserMapper usermapper; /** * 获取身份验证信息 Shiro中,最终是通过 Realm 来获取应用程序中的用户、角色及权限信息的。 * * @param authenticationToken * 用户身份信息 token * @return 返回封装了用户信息的 AuthenticationInfo 实例 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; String username = String.valueOf(token.getUsername()); // 从数据库获取对应用户名的用户 User user = usermapper.findByUsername(username); if (user == null) { throw new AccountException("不存在此用户"); } // 获取盐值,即用户名 ByteSource salt = ByteSource.Util.bytes(username); //注意,数据库中的user的密码必须是要经过md5加密,不然还是会抛出异常!!!!! return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), salt, getName()); } }

最后

以上就是无语手机最近收集整理的关于Shiro异常java.lang.IllegalArgumentException: Odd number of characters的解决方案的全部内容,更多相关Shiro异常java.lang.IllegalArgumentException:内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(112)

评论列表共有 0 条评论

立即
投稿
返回
顶部