【发布时间】:2018-11-20 01:22:00
【问题描述】:
根据这个answer,我应该将我的String BCrypt哈希密码passHash保存为BINARY或BINARY(60)(我选择BINARY(60))我的 MySQL 表(我将它存储在名为 passHash 的列中)。
现在,当我从表中选择 passHash 列值并将其检索到 Java 中时,它现在是 byte[] 数据类型。
然后我如何将它转换回它的 String 形式,以便我可以使用下面的validateLogin() 方法对其进行验证:
//Validate username and password login
public boolean validateLogin(String username, String userpass)
{
boolean status = false;
PreparedStatement pst = null;
ResultSet rs = null;
User user = new User(); //This is my Java bean class
try(Connection connect= DBConnection.getConnection())
{
//Here, passHash is stored as VARBINARY(60)
pst = connect.prepareStatement("SELECT * FROM user WHERE username=? and passHash=?;");
pst.setString(1, username);
pst.setString(2, userpass);
//Here's where I'm having difficulty because `passHash` column in my user table is VARBINARY(60) while `userpass` is a String
rs = pst.executeQuery();
status = rs.next();
}
catch (SQLException e)
{
e.printStackTrace();
}
return status; //status will return `true` if `username` and `userpass` matches what's in the columns
}
username 和 userpass 参数用于获取用户在我的Login.jsp 表单中的输入:
String username = request.getParameter("username");
String userpass = request.getParameter("userpass");
编辑:我的BCrypt代码如下:
//returns a hashed String value
public static String bCrypt (String passPlain) {
return BCrypt.hashpw(passPlain, BCrypt.gensalt(10));
}
//Returns a true if plain password is a match with hashed password
public static Boolean isMatch(String passPlain, String passHash){
return (BCrypt.checkpw(passPlain, passHash));
}
【问题讨论】:
-
根据docs.spring.io/spring-security/site/docs/4.2.5.RELEASE/apidocs/… 的值实际上是
Strings所以linked answer 可能不正确? -
@ScaryWombat 所以你建议我改用我的
passHash1 data type into aVARCHAR(60)`? -
其实我看不出有什么大惊小怪的,字符串到字节使用
String::getBytes字节到字符串使用new String (bytes)存储在数据库中,但是你想要
标签: java mysql jsp login password-hash