Since Jhipster(Angular + Springboot Scaffolding) uses JWT for authentication, we have a custom login model which checks user authentication. Now the code goes something like this(UserJWTController.java):
@PostMapping("/authenticate")
@Timed
public ResponseEntity<JWTToken> authorize(@Valid @RequestBody LoginVM loginVM) {
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(loginVM.getUsername(), loginVM.getPassword());
Authentication authentication = this.authenticationManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
boolean rememberMe = (loginVM.isRememberMe() == null) ? false : loginVM.isRememberMe();
String jwt = tokenProvider.createToken(authentication, rememberMe);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(JWTConfigurer.AUTHORIZATION_HEADER, "Bearer " + jwt);
return new ResponseEntity<>(new JWTToken(jwt), httpHeaders, HttpStatus.OK);
}
Its observed that when a user enters invalid credentials an error is thrown. And front-end (angular) handles the error by showing Text within the modal itself.
Now, suppose I need to add a custom validation as well. For example, I need to check if that user's Last Name is "Mike", then throw a Custom Error saying "Mike can't log in".
The problem is authenticate() (org.springframework.security.authentication.AuthenticationManager) method throws DisabledException,LockedException and BadCredentialsException by default.
I am not Experienced with Spring Security so I don't really understand how can we Override Classes and add the required functionality.
Moreover, Angular catches a generic Exception, so my guess would be that we need to add another constraint:
.catch(() => {
this.authenticationError = true;
});
Any help would be greatly appreciated.
Thanks in Advance.