加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

使用JDBCRealm对Shiro进行身份验证

发布时间:2020-12-14 19:27:29 所属栏目:Java 来源:网络整理
导读:我正在尝试使用Shiro验证在Tomcat 6中运行的servlet. 我有以下shiro.ini文件: [main]ps = org.apache.shiro.authc.credential.DefaultPasswordServicepm = org.apache.shiro.authc.credential.PasswordMatcherpm.passwordService = $psaa = org.apache.shir
我正在尝试使用Shiro验证在Tomcat 6中运行的servlet.

我有以下shiro.ini文件:

[main]
ps = org.apache.shiro.authc.credential.DefaultPasswordService
pm = org.apache.shiro.authc.credential.PasswordMatcher
pm.passwordService = $ps

aa = org.apache.shiro.authc.credential.AllowAllCredentialsMatcher
sm = org.apache.shiro.authc.credential.SimpleCredentialsMatcher

jof = org.apache.shiro.jndi.JndiObjectFactory
jof.resourceName = jdbc/UserDB
jof.requiredType = javax.sql.DataSource
jof.resourceRef = true

realm = org.apache.shiro.realm.jdbc.JdbcRealm
realm.permissionsLookupEnabled = true
realm.credentialsMatcher = $pm
; Note factories are automatically invoked via getInstance(),;   see org.apache.shiro.authc.config.ReflectionBuilder::resolveReference
realm.dataSource = $jof

securityManager.realms = $realm

[urls]
/rest/** = authcBasic
/prot/** = authcBasic

以下在我的数据库中:

mysql> select * from users;
+----------+------------------+----------+----------------------------------------------+--------------------------+
| username | email            | verified | password                                     | password_salt            |
+----------+------------------+----------+----------------------------------------------+--------------------------+
| admin    | a.muys@********* |        1 | ojSiTecNwRF0MunGRvz3DRSgP7sMF9EAR77Ol/2IAY8= | eHp9XedrIUa5sECfOb+KOA== |
+----------+------------------+----------+----------------------------------------------+--------------------------+
1 row in set (0.00 sec)

如果我使用SimpleCredentialsManager,它会对users表中的明文密码进行身份验证.尝试使用PasswordMatcher非常令人沮丧.

password和password_salt是通过shiro-tools Hasher实用程序获得的.

当我尝试使用我用于测试(path = rest / hello,context = / ws)的基本HelloWorld servlet进行身份验证时,我在日志中得到以下内容:

15:35:38.667 [http-8080-2] TRACE org.apache.shiro.util.ClassUtils - Unable to load clazz named [ojSiTecNwRF0MunGRvz3DRSgP7sMF9EAR77Ol/2IAY8=] from class loader [WebappClassLoader
  context: /ws
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@79ddd026
]

(全日志https://gist.github.com/recurse/5915693)

它似乎试图加载我的散列密码作为一个类名.这是一个bug,还是配置错误?如果这是一个bug,我该如何解决?如果这是一个配置错误,我什么失踪?

解决方法

首先,感谢提供了很多关于这个问题的信息,这使得提供一个很简单的答案.

通过查看您的示例数据库行列表,在执行散列密码比较时,不会出现PasswordService需要输出的输出.例如:

$java -jar ~/.m2/repository/org/apache/shiro/tools/shiro-tools-hasher/1.2.2/shiro-tools-hasher-1.2.2-cli.jar -p
Password to hash:
Password to hash (confirm):
$shiro1$SHA-256$500000$uxaA2ngfdxdXpvSWzpuFdg==$hOJZc+3+bFYYRgVn5wkbQL+m/FseeqDtoM5mOiwAR3E=

以$shiro1 $开头的字符串是您将保存到数据库中的密码列.没有必要单独的盐柱,因为Shiro需要的所有信息都在$shiro1 $… String中.

DefaultPasswordService使用相同的默认配置参数(SHA-256,500,000次迭代等),因此如果您使用Hasher CLI工具(如上所示)(无额外的散列算法配置),则不需要自定义DefaultPasswordService POJO任何进一步.但是,如果更改CLI上的散列参数,则需要确保在DefaultPasswordService bean(和/或其内部HashingService)上配置相同的参数.

如果您仍然在测试并且可以更改您的数据库模式,我建议现在使用一个密码字段来存储$shiro1 $…字符串.然后,您使用PasswordService,如下所示:

http://shiro.apache.org/static/current/apidocs/org/apache/shiro/authc/credential/PasswordService.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读