gpt4 book ai didi

mysql - Spring Security - Bcrypt 与 CAS、MySQL、SearchModeSearchDatabaseAuthenticationHandler 和 BasicDataSource

转载 作者:行者123 更新时间:2023-11-29 12:20:25 26 4
gpt4 key购买 nike

如果我有一个存储使用 Spring 的 Bcrypt 编码器加密的用户名和密码的数据库,我将如何使用当前的设置在 Spring 中将其解码回纯文本?

我的 MySQL 有这个表用于身份验证:

username     |  password     |  enabled
----------------------------------------
varchar(50) | varchar(100) | tinyint

密码字段将包含每个用户的 Bcrypt 密码。

现在介绍 CAS 的deployerConfigContext.xml。

<bean class="org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler">
<property name="tableUsers"><value>user_authentication</value></property>
<property name="fieldUser"><value>username</value></property>
<property name="fieldPassword"><value>password</value></property>
<property name="dataSource" ref="dataSource"/>
</bean>

我将数据源定义为:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/user_mgt</value>
</property>
<property name="username"><value>root</value></property>
<property name="password"><value>test_pw</value></property>
</bean>

我不确定是否可以在不进行定制的情况下实现 Bcrypt 解码。

最佳答案

当您使用散列密码时,不可能取回纯文本,因此我们使用散列算法来放置散列密码。要对用户进行身份验证,您必须将数据库中的散列密码与用户输入连接时的散列密码进行比较。Spring Security 会为您完成此操作,您只需告诉 Spring Security 您在配置中使用 BCryptPasswordEncoder 即可:

<authentication-manager>
<authentication-provider>
.......
<password-encoder red="encoder"/>
</authentication-provider>
</authentication-manager>
.........
<beans:bean id="encoder" class="org.springframework.security.crypto.password.BCryptPasswordEncoder"/>

请参阅Spring Security文档

关于mysql - Spring Security - Bcrypt 与 CAS、MySQL、SearchModeSearchDatabaseAuthenticationHandler 和 BasicDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29100500/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com