1

I have a very simple registration form. I am having difficulty understand why my password field is rejecting values. Maybe I am not setting my User bean correctly? I have two password field one is just what the user inputs, encryptedPassword uses " org.springframework.security.crypto.password.PasswordEncoder; " to encrypt the password

I am using Spring-MVC 4.x, Hibernate 5.x

register.jsp

<sf:form id="registerForm" method="POST" action="${pageContext.request.contextPath}/register/user" commandName="user">
...
<label for="password">Password: </label>
<sf:input type="text" id="password" name="password" path="password"/>
<sf:errors path="password" cssClass="password"></sf:errors>

<label for="confirmPassword">Confirm Password: </label>
<input type="text" id="confirmPassword" name="confirmPassword"/>
<span class="confirmCheck"></span>
....
<input type="submit" value="Submit">
</sf:form>

User

@Entity
@Table(name="users")
@Component
public class User {

 @Transient
 @Autowired
 private PasswordEncoder passwordEncoder;

 @Id
 @Column(name = "username")
 private String username;

 @ValidEmail
 private String email;

 @Transient
 @NotBlank
 private String password;

 @Column(name="password")
 @NotBlank
 private String encryptedPassword;

 public String getPassword() {
    return password;
 }
 public void setPassword(String password) {
    this.password = password;
    this.setEncryptedPassword(password);
 }

 public String getEncryptedPassword() {
    return encryptedPassword;
}

public void setEncryptedPassword(String encryptedPassword) {
    this.encryptedPassword = (String) passwordEncoder.encode(encryptedPassword).toString();

}
 ....getter ans setters
}

This is my actual error messages

[Field error in object 'user' on field 'password': rejected value [12345678]; codes [methodInvocation.user.password,methodInvocation.password,methodInvocation.java.lang.String,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.password,password]; arguments []; default message [password]]; default message [Property 'password' threw exception; nested exception is java.lang.NullPointerException], Field error in object 'user' on field 'encryptedPassword': rejected value [null]; codes [NotBlank.user.encryptedPassword,NotBlank.encryptedPassword,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [user.encryptedPassword,encryptedPassword]; arguments []; default message [encryptedPassword]]; default message [may not be empty]]
shazin
  • 21,379
  • 3
  • 54
  • 71
Eric Huang
  • 1,114
  • 3
  • 22
  • 43
  • When do you get this Exception? On Form Submit? On Persisting to Database? – shazin Jun 06 '16 at 09:18
  • 2
    It seems that the passwordEncoder is not being injected correctly. I think that the encoder should be in the DAO and not in the entity itself but that's my opinion. – RubioRic Jun 06 '16 at 09:22
  • Please make the reference of this link For validation in spring. http://stackoverflow.com/questions/7545231/bean-validation-notnull-notblank-and-notempty-does-not-work-in-jsftomcat – veeramani sundaram Jun 06 '16 at 11:30
  • @shazin I did a few test... Actually on form submit before persisting to db my encryptedPassword = null. But in my setPassword method i actually call this.setEncryptedPassword(password) <- is this not a right place to do this? – Eric Huang Jun 06 '16 at 22:09
  • @RubioRic hummm I actually moved the encoder to the DAO as you suggest it works now. But what was wrong with what I had that makes it not work? – Eric Huang Jun 06 '16 at 22:10
  • I'm no Spring expert but I think that the entity classes are not treated as Spring bean objects, the dependences are not being injected there. Maybe you can configure Spring to do it someway but it seems not natural. Let the entities to JPA/Hibernate and the DAO, Service, Controller, etc to Spring. – RubioRic Jun 07 '16 at 06:59
  • @RubioRic Okey thank you – Eric Huang Jun 15 '16 at 21:44

0 Answers0