Java Spring安全-密码哈希
我使用Spring安全性来处理应用程序中的授权。在我的配置中,我有以下内容:Java Spring安全-密码哈希,java,spring,security,spring-mvc,hash,Java,Spring,Security,Spring Mvc,Hash,我使用Spring安全性来处理应用程序中的授权。在我的配置中,我有以下内容: <security:authentication-manager> <security:authentication-provider> <security:password-encoder hash="md5"/> <security:jdbc-user-service id="userService"
<security:authentication-manager>
<security:authentication-provider>
<security:password-encoder hash="md5"/>
<security:jdbc-user-service id="userService"
data-source-ref="dataSource"
users-by-username-query="select phone, password, true from users where phone=?"
authorities-by-username-query="select phone,'ROLE_USER' from users where phone=?" />
</security:authentication-provider>
</security:authentication-manager>
当我删除
行并将原始密码存储在DB中时,授权工作正常。但当我试图存储在DB哈希密码中,并使用这一行时,授权失败。我做错什么了吗
另请注意,DB中的密码哈希值是100%正确的<密码>202cb962ac59075b964b07152d234b70用于
123
密码。我可以建议您创建测试类并在那里创建哈希
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
public class Test {
public static void main(String[] args) {
Md5PasswordEncoder encoderMD5 = new Md5PasswordEncoder();
String securePass = encoderMD5.encodePassword("admin", null);
System.out.println(encoderMD5.isPasswordValid(securePass,"admin", null));
}
}
在xml中的使用
<bean name="md5" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/>
<security:password-encoder ref="md5"/>
可能会很有用小当数据库密码被散列时,你会对输入的密码进行散列吗?不,我想这就是我需要使用的原因,不对吗?你如何将输入的密码与数据库中的密码进行比较?请用你的代码更新你的问题。我不比较。Spring Security必须做到这一点:我的应用程序中不使用salt,所以我在本测试中尝试使用“”和null作为salt。这两个案例都是成功的,所以我认为问题不在于散列错误。是的,我知道bcrypt比md5好得多,但出于某些原因,我只需要使用md5。@gth99请与您的db表用户共享
<bean name="bcryptEncode" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<constructor-arg value="12"></constructor-arg>
</bean>
<security:password-encoder ref="bcryptEncode"/>
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
public class PrintBCryptString {
public static void main(String[] args) {
PasswordEncoder encoder = new BCryptPasswordEncoder(12);
System.out.println(encoder.matches("type here some string", encoder.encode("type here some string")));
System.out.println(encoder.encode("type here some string"));
}
}