Shiro身份验证抛出AuthenticationException异常,解决方案

栏目: Java · 发布时间: 5年前

内容简介:在学习Shiro的时候,遇到Shiro抛出出现这个异常的原因是因为身份验证出错了,但是我觉得我写的Realm应该没什么错误,最后折腾了一会,翻看了一下Shiro的一些源码,终于知道出现这个问题的原因了,于是想将过程记录下来。我在
Shiro身份验证抛出AuthenticationException异常,解决方案

在学习Shiro的时候,遇到Shiro抛出 org.apache.shiro.authc.AuthenticationException 异常,完整异常如下:

org.apache.shiro.authc.AuthenticationException: Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - xue8, rememberMe=false].  Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).
复制代码

出现这个异常的原因是因为身份验证出错了,但是我觉得我写的Realm应该没什么错误,最后折腾了一会,翻看了一下Shiro的一些源码,终于知道出现这个问题的原因了,于是想将过程记录下来。

解决

我在 Realm 的身份证验证方法 doGetAuthenticationInfo 添加断点进行调试

Shiro身份验证抛出AuthenticationException异常,解决方案
发现是从 authenticationToken 获取用户 密码 getCredentials() 的时候出问题了,这就奇怪了,获取 用户名 getPrincipal() 的时候没问题,而且通过查看 authenticationToken

的源码,发现他们两个是一样的东西

Shiro身份验证抛出AuthenticationException异常,解决方案
Shiro身份验证抛出AuthenticationException异常,解决方案
用户名和密码传给 doGetAuthenticationInfo 的时候都是成功的,那为什么会出错呢?在调试 doGetAuthenticationInfo

的时候,发现了username和password的储存方式不一样

Shiro身份验证抛出AuthenticationException异常,解决方案
username是以字符串String的类型储存的,而password是以字符数组char[]类型储存的,那会不会是因为这个问题造成的呢,而且在 subject.login(token)

方法中,用户名和密码都是以字符串String储存在token中的,

Shiro身份验证抛出AuthenticationException异常,解决方案
为什么到了 doGetAuthenticationInfo 就变了呢,于是我继续查看了 UsernamePasswordToken

的源码,会不会是因为这个对象将密码的字符串类型转成char[]类型呢

Shiro身份验证抛出AuthenticationException异常,解决方案
通过查看源码,看到了确实是 UsernamePasswordToken 将传入的密码password从String字符串类型转成了char[]字符数组类型,到这里我也就明白了,在 RealmdoGetAuthenticationInfo 方法中,传入的 AuthenticationToken authenticationToken

用户名是以String方式储存的而密码是以Char[]储存的,用接收String的方式去接收Char[]类型数据肯定就不行了呢,所以我将密码改成用Char[]接收,如下

Shiro身份验证抛出AuthenticationException异常,解决方案

即可解决问题。

原文地址:ddnd.cn/2019/02/01/…


以上所述就是小编给大家介绍的《Shiro身份验证抛出AuthenticationException异常,解决方案》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Algorithm Design Manual

The Algorithm Design Manual

Steven S Skiena / Springer / 2011-11-14 / GBP 55.07

....The most comprehensive guide to designing practical and efficient algorithms.... Written by a well-known algorithms researcher who received the IEEE Computer Science and Engineering Teaching Aw......一起来看看 《The Algorithm Design Manual》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具