Issue
I am using spring boot with spring security as authentication for my web application. I have a controller which takes authenticationRequest(POJO) parameters as @RequestBody
Calling end point /authenticate from postman by passing username and password in JSON format But when I print the values(both username and password) I can only see password getting printed. After many attempts couldn't find the reason why username is not populating.
@RequestMapping(value="/authenticate",method=RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody AuthenticationRequest authenticationRequest) throws Exception{
System.out.println("authenticationRequest.getUserName()"+authenticationRequest.getUserName());
System.out.println("authenticationRequest.getPassword()"+authenticationRequest.getPassword());
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUserName(), authenticationRequest.getPassword()));
}
POJO Class
public class AuthenticationRequest {
private String username;
private String password;
public AuthenticationRequest(){
}
public AuthenticationRequest(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUserName() {
return username;
}
public void setUserName(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
JSON (raw) { "username":"user", "password":"pass" }
Console
authenticationRequest.getUserName()null
authenticationRequest.getPassword()pass
Solution
The problem is in your getter and setter:
public String getUserName() {
return username;
}
public void setUserName(String username) {
this.username = username;
}
The letter n should be small because your JSON contains username
with small n. Try like this:
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
Answered By - George Lvov