I am currently working on a project that involves integrating Spring Security with an Angular client. I have encountered an issue where, despite checking for null values in the login form on the Angular side before sending it to the Java application, the Java app returns a message stating that the input is null.
I would greatly appreciate any assistance in resolving this issue as I am unable to pinpoint what may be causing this error. Thank you!
Below are the relevant code snippets and logs:
Log: 2019-07-15 15:30:12.127 WARN 8156 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 0 in method: public org.springframework.http.ResponseEntity com.grokonez.jwtauthentication.controller.AuthRestAPIs.authenticateUser(com.grokonez.jwtauthentication.message.request.LoginForm), with 1 error(s): [Field error in object 'loginForm' on field 'username': rejected value [null]; codes [NotBlank.loginForm.username,NotBlank.username,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [loginForm.username,username]; arguments []; default message [username]]; default message [не может быть пусто]] ]
Java code:
Model:
public class LoginForm {
@NotBlank
@Size(min=3, max = 60)
private String username;
@NotBlank
@Size(min = 6, max = 40)
private String password; ...
Controller:
@PostMapping("/signin")
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginForm loginRequest) {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
String jwt = jwtProvider.generateJwtToken(authentication);
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return ResponseEntity.ok(new JwtResponse(jwt, userDetails.getUsername(), userDetails.getAuthorities()));
}
Angular part:
Service:
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
@Injectable({
providedIn: 'root'
})
export class AuthService {
private loginUrl = 'http://localhost:8080/api/auth/signin';
private registrationUrl = 'http://localhost:8080/api/auth/signup';
constructor(private http: HttpClient) { }
signUp(registrationInfo: RegistrationInfo): Observable<string> {
return this.http.post<string>(this.registrationUrl, registrationInfo, httpOptions);
}
login(loginInfo: LoginInfo): Observable<JwtInfo> {
console.log(loginInfo)
return this.http.post<JwtInfo>(this.loginUrl, loginInfo, httpOptions);
}
Component:
login(data) {
this.loginInfo = new LoginInfo(data.userName, data.password);
console.log(data.userName, data.password);
this.authService.login(this.loginInfo).subscribe(
(jwtResponse: JwtInfo) => {
this.tokenStorage.saveToken(jwtResponse.accessToken);
this.tokenStorage.saveUserName(jwtResponse.username);
this.tokenStorage.saveAutorities(jwtResponse.authorities);
this.username = this.tokenStorage.getUserName();
this.authorities = this.tokenStorage.getAutorities();
this.isLoggedIn = true;
this.reloadPage();
}, error => {
this.warningMessage = error.error.message;
this.isLoginFailed = true;
}
);
}
Log with problem: 2019-07-15 15:30:12.127 WARN 8156 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument at index 0 in method: public org.springframework.http.ResponseEntity com.grokonez.jwtauthentication.controller.AuthRestAPIs.authenticateUser(com.grokonez.jwtauthentication.message.request.LoginForm), with 1 error(s): [Field error in object 'loginForm' on field 'username': rejected value [null]; codes [NotBlank.loginForm.username,NotBlank.username,NotBlank.java.lang.String,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [loginForm.username,username]; arguments []; default message [username]]; default message [не может быть пусто]] ]