Skip to content

Instantly share code, notes, and snippets.

@lusabo
Created May 1, 2018 20:57
Show Gist options
  • Save lusabo/f481a05bf649e761e879908c93ff8a11 to your computer and use it in GitHub Desktop.
Save lusabo/f481a05bf649e761e879908c93ff8a11 to your computer and use it in GitHub Desktop.
AuthController
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