-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAuthController.java
More file actions
30 lines (25 loc) · 1.07 KB
/
Copy pathAuthController.java
File metadata and controls
30 lines (25 loc) · 1.07 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
package application.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
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 application.model.Usuario;
import application.service.TokenService;
@RestController
@RequestMapping("/auth")
public class AuthController {
@Autowired
private AuthenticationManager authManager;
@Autowired
private TokenService tokenService;
@PostMapping
public String login(@RequestBody Usuario usuario) {
UsernamePasswordAuthenticationToken tk = new UsernamePasswordAuthenticationToken(
usuario.getNomeDeUsuario(), usuario.getSenha());
authManager.authenticate(tk);
return tokenService.generateToken(usuario);
}
}