AuthController.java
2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.srh.api.controller;
import com.srh.api.dto.auth.AuthDto;
import com.srh.api.dto.auth.LoginForm;
import com.srh.api.error.exception.InvalidUserInTokenException;
import com.srh.api.service.JWTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import static com.srh.api.security.SecurityConstants.*;
@RestController
@RequestMapping("/auth")
public class AuthController {
@Autowired
private AuthenticationManager authManager;
@Autowired
private JWTService jwtService;
@PostMapping
public ResponseEntity<AuthDto> auth(@RequestBody @Valid LoginForm form) {
UsernamePasswordAuthenticationToken loginUser = form.convert();
String token = generatedTokenForAuthenticatedUser(loginUser);
return ResponseEntity.ok()
.headers(generateAuthorizationHeader(token))
.body(new AuthDto(token, "Bearer"));
}
public String generatedTokenForAuthenticatedUser(UsernamePasswordAuthenticationToken userToken) {
try {
Authentication authentication = authManager.authenticate(userToken);
return jwtService.buildToken(authentication);
} catch (AuthenticationException e) {
throw new InvalidUserInTokenException(e);
}
}
private HttpHeaders generateAuthorizationHeader(String token) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HEADER, TOKEN_PREFIX + token);
return responseHeaders;
}
}