forked from NeilAlishev/SpringCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.java
More file actions
79 lines (65 loc) · 3.09 KB
/
AuthController.java
File metadata and controls
79 lines (65 loc) · 3.09 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package ru.alishev.springcourse.FirstSecurityApp.controllers;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import ru.alishev.springcourse.FirstSecurityApp.dto.AuthenticationDTO;
import ru.alishev.springcourse.FirstSecurityApp.dto.PersonDTO;
import ru.alishev.springcourse.FirstSecurityApp.models.Person;
import ru.alishev.springcourse.FirstSecurityApp.security.JWTUtil;
import ru.alishev.springcourse.FirstSecurityApp.services.RegistrationService;
import ru.alishev.springcourse.FirstSecurityApp.util.PersonValidator;
import javax.validation.Valid;
import java.util.Map;
/**
* @author Neil Alishev
*/
@RestController
@RequestMapping("/auth")
public class AuthController {
private final RegistrationService registrationService;
private final PersonValidator personValidator;
private final JWTUtil jwtUtil;
private final ModelMapper modelMapper;
private final AuthenticationManager authenticationManager;
@Autowired
public AuthController(RegistrationService registrationService, PersonValidator personValidator,
JWTUtil jwtUtil, ModelMapper modelMapper, AuthenticationManager authenticationManager) {
this.registrationService = registrationService;
this.personValidator = personValidator;
this.jwtUtil = jwtUtil;
this.modelMapper = modelMapper;
this.authenticationManager = authenticationManager;
}
@PostMapping("/registration")
public Map<String, String> performRegistration(@RequestBody @Valid PersonDTO personDTO,
BindingResult bindingResult) {
Person person = convertToPerson(personDTO);
personValidator.validate(person, bindingResult);
if (bindingResult.hasErrors()) {
return Map.of("message", "Ошибка!");
}
registrationService.register(person);
String token = jwtUtil.generateToken(person.getUsername());
return Map.of("jwt-token", token);
}
@PostMapping("/login")
public Map<String, String> performLogin(@RequestBody AuthenticationDTO authenticationDTO) {
UsernamePasswordAuthenticationToken authInputToken =
new UsernamePasswordAuthenticationToken(authenticationDTO.getUsername(),
authenticationDTO.getPassword());
try {
authenticationManager.authenticate(authInputToken);
} catch (BadCredentialsException e) {
return Map.of("message", "Incorrect credentials!");
}
String token = jwtUtil.generateToken(authenticationDTO.getUsername());
return Map.of("jwt-token", token);
}
public Person convertToPerson(PersonDTO personDTO) {
return this.modelMapper.map(personDTO, Person.class);
}
}