2015-09-26 45 views
3

所以我想在sha265密碼保存到蒙戈db和爲了使用Apache Shiro框架授權用戶登錄,而試圖從DB我拉「M越來越嗎啡運行時異常,這裏是代碼:無法從蒙戈拉分貝使用嗎啡 - 沒有可用的構造

用戶等級:

public class User implements Serializable{ 

     @Id 
     private ObjectId id; 
     private String name; 
     private String password; 
     private SimpleByteSource passwordSalt; 

     @Embedded 
     private TenantId tenantId; 
     private Set<String> roles = new HashSet<>(); 



     @Override 
     public String toString() { 
      return "User [id=" + id + ", name=" + name + ", password=" 
        + password + ", passwordSalt=" + passwordSalt 
        + ", tenantId=" + tenantId + ", roles=" + roles + "]"; 
     } 
     public SimpleByteSource getPasswordSalt() { 
      return passwordSalt; 
     } 
     public void setPasswordSalt(SimpleByteSource passwordSalt) { 
      this.passwordSalt = passwordSalt; 
     } 
     public ObjectId getId() { 
      return id; 
     } 
     public void setId(ObjectId id) { 
      this.id = id; 
     } 
     public String getName() { 
      return name; 
     } 
     public void setName(String name) { 
      this.name = name; 
     } 
     public String getPassword() { 
      return password; 
     } 
     public void setPassword(String password) { 
      this.password = password; 
     } 
     public TenantId getTenantId() { 
      return tenantId; 
     } 
     public void setTenantId(TenantId tenantId) { 
      this.tenantId = tenantId; 
     } 
     public Set<String> getRoles() { 
      return roles; 
     } 
     public void setRoles(Set<String> roles) { 
      this.roles = roles; 
     } 

} 

測試類:

public static void main(String[] args) { 
     MongoClient m_mongoClient = new MongoClient("127.0.0.1",27017); 
     Morphia m_morphiaMongoDB = new Morphia(); 
     Datastore ds = m_morphiaMongoDB.createDatastore(m_mongoClient, "test"); 

     RandomNumberGenerator rng = new SecureRandomNumberGenerator(); 
     SimpleByteSource salt = (SimpleByteSource) rng.nextBytes(); 
     String hashedPasswordBase64 = new Sha256Hash("1234", salt, 1024).toBase64(); 
     User temp = new User(); 

     temp.setName("Myname"); 
     //temp.setPassword("1234"); 
     temp.setPassword(hashedPasswordBase64); 
     temp.setPasswordSalt(salt); 

     ds.save(temp); 

     User user = ds.find(User.class).field("name").equal("Myname").get(); 
     System.out.println(user); 

    } 

我使用Shiro和MOR PHIA,這就是我得到:

Exception in thread "main" java.lang.RuntimeException: org.mongodb.morphia.mapping.MappingException: No usable constructor for org.apache.shiro.util.SimpleByteSource 
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:171) 
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:617) 
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:592) 
    at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:306) 
    at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:79) 
    at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:65) 
    at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:60) 
    at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:421) 
    at com.roi.shiro.realm.temp.main(temp.java:31) 
Caused by: org.mongodb.morphia.mapping.MappingException: No usable constructor for org.apache.shiro.util.SimpleByteSource 
    at org.mongodb.morphia.mapping.DefaultCreator.getNoArgsConstructor(DefaultCreator.java:140) 
    at org.mongodb.morphia.mapping.DefaultCreator.createInst(DefaultCreator.java:109) 
    at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:27) 
    at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:35) 
    at org.mongodb.morphia.mapping.DefaultCreator.createInstance(DefaultCreator.java:45) 
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:161) 
    ... 8 more 
Caused by: java.lang.NoSuchMethodException: org.apache.shiro.util.SimpleByteSource.<init>() 
    at java.lang.Class.getConstructor0(Class.java:3074) 
    at java.lang.Class.getDeclaredConstructor(Class.java:2170) 
    at org.mongodb.morphia.mapping.DefaultCreator.getNoArgsConstructor(DefaultCreator.java:136) 
    ... 13 more 

SimpleByteSource具有下面的構造:

public SimpleByteSource(byte[] bytes) { 
     this.bytes = bytes; 
    } 

什麼想法?

回答

5

不能保存與嗎啡該對象。 Morphia在填充其字段之前需要一個零參數構造函數來創建類。您需要將該字節[]保存在User類的字段中。

+0

我只是另一個想法。你也許可以寫爲字段會採取'的byte []',創建'SimpleByteSource',並返回一個轉換器。這也會起作用,並會保留你的對象模型。 – evanchooly