java - 在类中保存密码的最理想方式 - Java

标签 java design-patterns passwords abstract-class password-protection

我有两个类“用户配置文件”和“指纹配置文件”,它们扩展了一个抽象类“配置文件”。

轮廓:

/**
 * Template for User profiles or Fingerprint profiles
 */
public abstract class Profile {

    /**
     * Profile Name
     */
    private String name;

    /**
     * Profile id
     */
    private int id;

    /**
     * Set the name of this profile
     * @param name
     */
    public void setProfileName(String name) {
        this.name = name;
    }

    /**
     * Set the id of this profile
     * @param name
     */
    public void setIdNumber(int id) {
        this.id = id;
    }

    /**
     * Get the name of this profile
     */
    public String getProfileName() {

        return name;

    }

    /**
     * Get the id of this profile
     */
    public int getIdNumber() {
        return id;
    }

}

我的下一个类是 UserProfile
public class UserProfile extends Profile {

    /**
     * Users password
     */
    private char[] password;

    /**
     * Set User password
     * @param password
     */
    public void setPassword(char[] password){

        this.password = password;

    }

    /**
     * Get User password
     */
    public char[] getPassword(){

        return password;

    }

}

仅仅看这个类似乎很狡猾,能够像这样检索密码似乎是完全错误的(即使 get 方法是 private )。

在制作我的 FingerPrintProfile 类以及包含本质上也需要安全的“FingerprintData”对象时,似乎我将面临同样的问题。

有没有人知道一种安全的方法,或者最好是人们用来解决这种情况的模式?

谢谢 !

奖金问题

我创建了抽象类来为两种类型的配置文件提供模板,指纹数据和文本密码之间似乎有一个共同点。但是,不可能创建一个抽象字段“密码”,它可能是一个字符数组或一个 FingerprintData 对象。有任何想法吗??

最佳答案

使用密码时,您可能应该使用某种形式的加密,这样密码就不会以纯文本形式存储。您可以阅读它的工作原理 here .

Here是 Bcrypt 的 Java 实现的链接,它至少应该可以帮助您入门。

关于java - 在类中保存密码的最理想方式 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13955515/

相关文章:

c# - DTO 加上 UnitOfWork 模式是为 Web 应用程序设计 DAL 的好方法吗?

php - 为新的 LDAP 用户设置密码

java - 在Java中计算闰年

java - 使用 Java 访问在线数据库

design-patterns - 何时以及如何应用策略模式代替装饰器模式?

java - 这是用不同单位实现距离的好方法吗

MySQL 5.6.26 (Windows 8.1 x64) : Trouble with setting a password for the root MySQL user

java - 生成必须包含字母、数字和特殊字符(6-10 位数字)的随机字符串

java - 如何使图像对背景透明

java - hibernate 恩弗斯 : Log user ip address