So i'm trying to save the password in sha265 into mongo db and use Apache Shiro
framework in order to authorize the user login, while trying to pull from the DB i'm getting morphia run time exception, here is the code :
User class:
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;
}
}
test class:
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);
}
I'm using Shiro
and Morphia, this is what i'm getting:
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 has the following constructor:
public SimpleByteSource(byte[] bytes) {
this.bytes = bytes;
}
any idea?
You can't save that object with Morphia. Morphia needs a zero-arg constructor to create the class before populating its fields. You'll need to save that byte[] on a field in your User class instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With