3
3
import org .springframework .context .annotation .Profile ;
4
4
import org .springframework .http .ResponseEntity ;
5
5
import org .springframework .security .authentication .AuthenticationManager ;
6
- import org .springframework .security .authentication .BadCredentialsException ;
7
- import org .springframework .security .authentication .DisabledException ;
8
6
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
9
7
import org .springframework .security .core .Authentication ;
10
8
import org .springframework .security .core .userdetails .UserDetails ;
@@ -30,19 +28,9 @@ public class JwtAuthenticationController {
30
28
31
29
@ PostMapping ("/authenticate" )
32
30
public ResponseEntity <?> createAuthenticationToken (@ RequestBody JwtRequest authenticationRequest ) throws Exception {
33
- Authentication authentication = authenticate (authenticationRequest .getUsername (), authenticationRequest .getPassword ());
31
+ Authentication authentication = authenticationManager . authenticate (new UsernamePasswordAuthenticationToken ( authenticationRequest .getUsername (), authenticationRequest .getPassword () ));
34
32
final UserDetails userDetails = (UserDetails ) authentication .getPrincipal ();
35
33
final String token = jwtTokenUtil .generateToken (userDetails );
36
34
return ResponseEntity .ok (new JwtResponse (token ));
37
35
}
38
-
39
- private Authentication authenticate (String username , String password ) throws Exception {
40
- try {
41
- return authenticationManager .authenticate (new UsernamePasswordAuthenticationToken (username , password ));
42
- } catch (DisabledException e ) {
43
- throw new Exception ("USER_DISABLED" , e );
44
- } catch (BadCredentialsException e ) {
45
- throw new Exception ("INVALID_CREDENTIALS" , e );
46
- }
47
- }
48
36
}
0 commit comments