我有一个 JSF + JPA Web 应用程序,其登录机制如下。
- 用户名已加密
- 密码经过哈希处理
- 使用过 Jasypt
- 当用户尝试登录时,所有用户都会循环。
- 每个用户的用户名都会被解密并与输入的用户名进行匹配。
- 如果匹配,则会对密码进行哈希处理,并使用存储的哈希密码进行检查。
在另一个预计用户数量较多的应用程序中,我从前三个字母中过滤用户,该字母存储为单独的字段。
如果您能指出我使用的次优方法并指导我采取正确的行动,我将不胜感激。
列出与加密相关的 Controller
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.jasypt.util.password.BasicPasswordEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;
@ManagedBean
@SessionScoped
public class SecurityController implements Serializable {
private static final long serialVersionUID = 1L;
public SecurityController() {
}
public String encrypt(String word) {
BasicTextEncryptor en = new BasicTextEncryptor();
en.setPassword("health");
try {
return en.encrypt(word);
} catch (Exception ex) {
return null;
}
}
public String hash(String word) {
try {
BasicPasswordEncryptor en = new BasicPasswordEncryptor();
return en.encryptPassword(word);
} catch (Exception e) {
return null;
}
}
public boolean matchPassword(String planePassword, String encryptedPassword) {
BasicPasswordEncryptor en = new BasicPasswordEncryptor();
return en.checkPassword(planePassword, encryptedPassword);
}
public String decrypt(String word) {
BasicTextEncryptor en = new BasicTextEncryptor();
en.setPassword("health");
try {
return en.decrypt(word);
} catch (Exception ex) {
return null;
}
}
}
这就是我检查自动化的方式。
private boolean checkUsers() {
String temSQL;
temSQL = "SELECT u FROM WebUser u WHERE u.retired = false";
List<WebUser> allUsers = getFacede().findBySQL(temSQL);
for (WebUser u : allUsers) {
if (getSecurityController().decrypt(u.getName()).equalsIgnoreCase(userName)) {
if (getSecurityController().matchPassword(passord, u.getWebUserPassword())) {
setLoggedUser(u);
setLogged(Boolean.TRUE);
setActivated(u.isActivated());
setRole(u.getRole());
getMessageController().setDefLocale(u.getDefLocale());
getMeController().createMenu();
getWebUserBean().setLoggedUser(u);
UtilityController.addSuccessMessage("Logged successfully");
return true;
}
}
}
return false;
}
最佳答案
如果用户名敏感;
- 使用静态盐对用户名进行哈希处理(系统使用单一盐,但应该足以保护用户名)
- 使用与密码一起保存在数据库中的动态盐(针对每个用户)对密码进行哈希处理。
当用户尝试登录时;
使用静态盐对登录时给出的用户名进行哈希处理,并在数据库中搜索结果。这将是精确匹配搜索,将给出单个结果(如果用户名不存在,则不会给出结果)
从找到的行中获取动态盐,并使用它对登录时给出的密码进行哈希处理。如果它与找到的数据库行中的散列密码匹配,则让用户进入。
考虑到这种安全性,没有人(甚至是您)可以列出系统中的所有用户名。如果您知道用户名,则可以验证其存在并找到该用户。
关于java - 优化 JSF + JPA Web 应用程序中的身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17641031/