Created
May 1, 2018 20:57
-
-
Save lusabo/f481a05bf649e761e879908c93ff8a11 to your computer and use it in GitHub Desktop.
AuthController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eco.security; | |
// Imports | |
@RestController | |
@RequestMapping("/login") | |
@CrossOrigin(origins = "*") | |
public class AuthController { | |
@Autowired | |
private AuthenticationManager authenticationManager; | |
@Autowired | |
private JwtTokenUtils jwtTokenUtils; | |
@Autowired | |
private UserDetailsService userDetailsService; | |
@PostMapping | |
public ResponseEntity<TokenDTO> gerarTokenJwt(@Valid @RequestBody CredentialsDTO credentials, BindingResult result) | |
throws AuthenticationException { | |
TokenDTO response = new TokenDTO(); | |
Authentication authentication = authenticationManager | |
.authenticate(new UsernamePasswordAuthenticationToken(credentials.username, credentials.password)); | |
SecurityContextHolder.getContext().setAuthentication(authentication); | |
UserDetails userDetails = userDetailsService.loadUserByUsername(credentials.username); | |
String token = jwtTokenUtils.getToken(userDetails); | |
response.token = token; | |
return ResponseEntity.ok(response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment