Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用BCrypt加载测试数据_Java_Authentication_Junit_Playframework_Bcrypt - Fatal编程技术网

Java 用BCrypt加载测试数据

Java 用BCrypt加载测试数据,java,authentication,junit,playframework,bcrypt,Java,Authentication,Junit,Playframework,Bcrypt,我使用BCrypt加密密码,我有一个模型,我正试图为它编写测试。我制作了一个YAML文件,其中包含一些基本的测试数据,但我似乎无法正确地获取代码 我的账户型号: package models; import javax.persistence.*; import org.mindrot.jbcrypt.BCrypt; import play.data.validation.Constraints.Required; import play.db.ebean.Model; import play

我使用BCrypt加密密码,我有一个模型,我正试图为它编写测试。我制作了一个YAML文件,其中包含一些基本的测试数据,但我似乎无法正确地获取代码

我的
账户
型号:

package models;

import javax.persistence.*;
import org.mindrot.jbcrypt.BCrypt;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;
import play.db.ebean.Model.Finder;

@Entity
public class Account extends Model {

    @Id
    public Long id;
    public String email;
    public String username;
    public String passwordhash;

    public Account(String email, String username, String password) {
        this.email = email;
        this.username = username;
        this.passwordhash = BCrypt.hashpw(password, BCrypt.gensalt()); 
    }

    public static Finder<Long, Account> find = new Finder<Long, Account>(Long.class, Account.class);

    public static Account create(
            String username, String email, String password, 
            String confirmPassword
    ) {
        Account account = new Account(username, email, password);
        account.save();
        return account;
    }

    public static Account authenticate (String username, String password) {
        Account account = Account.find.where().eq("username", username).findUnique();
        if (account != null && BCrypt.checkpw(password, account.passwordhash)) {
            return account;
        } else {
            return null;
        }
    }

}
以及我的测试数据YAML:

# Accounts

- &melvin !!models.Account
    email: mxtcapps@maxtrace.co.uk
    username: MelvIsntNormal
    password: HashedPassword

- &emma !!models.Account
    email: isthatastrawhat@maxtrace.co.uk
    username: isthatastrawhat
    password: Supernatural

- &paul !!models.Account
    email: palug@maxtrace.co.uk
    username: palug
    password: Oxford4EVA
运行测试时,出现以下错误:

[error] Test ModelTests.AccountTest.authentication failed: org.yaml.snakeyaml.constructor.ConstructorException: null; Can't construct a java object for tag:yaml.org,2002:models.Account; exception=Cannot create property=password for JavaBean=models.Account@1c8d2d9; Unable to find property 'password' on class: models.Account;  in 'reader', line 3, column 3:
[error]     - &melvin !!models.Account
[error]       ^
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:333)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequenceStep2(BaseConstructor.java:276)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructSequence(BaseConstructor.java:247)
[error]     at org.yaml.snakeyaml.constructor.SafeConstructor$ConstructYamlSeq.construct(SafeConstructor.java:442)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
[error]     at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
[error]     at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:481)
[error]     at org.yaml.snakeyaml.Yaml.load(Yaml.java:412)
[error]     at play.libs.Yaml.load(Yaml.java:31)
[error]     at play.libs.Yaml.load(Yaml.java:18)
[error]     at ModelTests.AccountTest.setUp(AccountTest.java:18)
[error]     ...
[error] Caused by: org.yaml.snakeyaml.error.YAMLException: Cannot create property=password for JavaBean=models.Account@1c8d2d9; Unable to find property 'password' on class: models.Account
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:299)
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:189)
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:331)
[error]     ... 48 more
[error] Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'password' on class: models.Account
[error]     at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:132)
[error]     at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:121)
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:308)
[error]     at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:240)
[error]     ... 50 more
[info] ModelTests.AccountTest
[info] x authentication
[info] 
[info] 
[info] Total for test ModelTests.AccountTest
[info] Finished in 0.039 seconds
[info] 1 tests, 1 failures, 0 errors
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]     ModelTests.AccountTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 26 s, completed 01-Feb-2014 07:59:02
我理解错误是由于密码字段不存在造成的。但是,我不太确定如何让测试调用Account方法,以便对密码进行加密。有人能帮忙吗

问题答案(谢谢@Joe)

我必须为密码字段添加一个setter方法:

public void setPassword(String password) {
        this.passwordhash = BCrypt.hashpw(password, BCrypt.gensalt());
}
我的
帐户
模型现在看起来如下: 包装模型

import javax.persistence.*;
import org.mindrot.jbcrypt.BCrypt;
import play.data.validation.Constraints.Required;
import play.db.ebean.Model;
import play.db.ebean.Model.Finder;

@Entity
public class Account extends Model {

    @Id
    public Long id;
    public String email;
    public String username;
    public String passwordhash;

    public Account(String email, String username, String password) {
        this.email = email;
        this.username = username;
        this.passwordhash = BCrypt.hashpw(password, BCrypt.gensalt()); 
    }

    public static Finder<Long, Account> find = new Finder<Long, Account>(Long.class, Account.class);

    public static Account create(
            String username, String email, String password, 
            String confirmPassword
    ) {
        Account account = new Account(username, email, password);
        account.save();
        return account;
    }

    public void setPassword(String password) {
        this.passwordhash = BCrypt.hashpw(password, BCrypt.gensalt());
    }

    public static Account authenticate (String username, String password) {
        Account account = Account.find.where().eq("username", username).findUnique();
        if (account != null && BCrypt.checkpw(password, account.passwordhash)) {
            return account;
        } else {
            return null;
        }
    }

}
import javax.persistence.*;
导入org.mindrot.jbcrypt.BCrypt;
导入play.data.validation.Constraints.Required;
导入play.db.ebean.Model;
导入play.db.ebean.Model.Finder;
@实体
公共类帐户扩展模型{
@身份证
公共长id;
公共字符串电子邮件;
公共字符串用户名;
公共字符串密码哈希;
公共帐户(字符串电子邮件、字符串用户名、字符串密码){
this.email=电子邮件;
this.username=用户名;
this.passwordhash=BCrypt.hashpw(密码,BCrypt.gensalt());
}
公共静态查找器find=新查找器(Long.class、Account.class);
公共静态帐户创建(
字符串用户名、字符串电子邮件、字符串密码、,
字符串确认密码
) {
帐户=新帐户(用户名、电子邮件、密码);
account.save();
返回帐户;
}
public void setPassword(字符串密码){
this.passwordhash=BCrypt.hashpw(密码,BCrypt.gensalt());
}
公共静态帐户身份验证(字符串用户名、字符串密码){
Account Account=Account.find.where().eq(“用户名”,用户名).findUnique();
if(account!=null&&BCrypt.checkpw(密码,account.passwordhash)){
返回帐户;
}否则{
返回null;
}
}
}

该错误表示您的
模型.Account
类没有
password
属性。SnakeYAML正在寻找:

默认情况下,包含标准JavaBean属性和公共字段

它发现两者都不是:

[error] Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'password' on class: models.Account
相反,它有一个
passwordHash
字段。这表明了两种可能性:

  • 在测试数据中定义
    passwordHash
  • models.Account
    上定义一个
    setPassword(String)
    方法,该方法将散列密码并将其存储在
    passwordHash

  • 测试应该做一件事,所以我建议第一件。您正在根据已知密码测试身份验证。将散列定义为测试的一部分意味着您可以在测试数据中看到已知密码的散列,并避免在遇到bug时通过测试,例如
    hashpw
    方法总是返回空字符串。

    实现第二个(用于不同的测试)这仅仅是为
    passwordhash
    创建一个setter方法吗?它将是一个
    setPassword
    方法,然后对给定密码进行散列并将其存储在
    passwordhash
    中。但是在本测试中,如何在不重写YAML的情况下将密码传递给该方法?您的YAML定义了一个
    password
    属性。SnakeYAML试图通过找到一个名为
    setPassword
    的setter来应用它,这是您将要编写的内容。感谢您清除这些内容,我会在我的笔记本电脑上尝试
    [error] Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'password' on class: models.Account