Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. package expert.healthcareanalytics.medicaid.controller;
  2.  
  3. import expert.healthcareanalytics.medicaid.configuration.security.jwt.JwtTokenProvider;
  4. import expert.healthcareanalytics.medicaid.domain.repository.UserRepository;
  5. import expert.healthcareanalytics.medicaid.domain.vo.AuthenticationRequest;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.http.ResponseEntity;
  8. import org.springframework.security.authentication.AuthenticationManager;
  9. import org.springframework.security.authentication.BadCredentialsException;
  10. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  11. import org.springframework.security.core.AuthenticationException;
  12. import org.springframework.security.core.GrantedAuthority;
  13. import org.springframework.security.core.annotation.AuthenticationPrincipal;
  14. import org.springframework.security.core.userdetails.UserDetails;
  15. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  16. import org.springframework.web.bind.annotation.RequestBody;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestMethod;
  19. import org.springframework.web.bind.annotation.RestController;
  20.  
  21. import java.util.HashMap;
  22. import java.util.Map;
  23.  
  24. import static java.util.stream.Collectors.toList;
  25. import static org.springframework.http.ResponseEntity.ok;
  26.  
  27. @RestController
  28. public class AuthController {
  29.  
  30.     @Autowired
  31.     AuthenticationManager authenticationManager;
  32.  
  33.     @Autowired
  34.     JwtTokenProvider jwtTokenProvider;
  35.  
  36.     @Autowired
  37.     UserRepository userRepository;
  38.  
  39.     @RequestMapping(method = RequestMethod.POST, value = "/signin")
  40.     public ResponseEntity signin(@RequestBody AuthenticationRequest data) {
  41.         try {
  42.             String username = data.getUsername();
  43.             String password = data.getPassword();
  44.             UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, password);
  45.             authenticationManager.authenticate(authentication);
  46.             String token = jwtTokenProvider.createToken(username, this.userRepository.findByUsername(username)
  47.                     .orElseThrow(() -> new UsernameNotFoundException("Username " + username + "not found")).getRole());
  48.             Map<Object, Object> model = new HashMap<>();
  49.             model.put("username", username);
  50.             model.put("token", token);
  51.             return ok(model);
  52.         } catch (AuthenticationException e) {
  53.             throw new BadCredentialsException("Invalid username/password supplied");
  54.         }
  55.     }
  56.  
  57.     @RequestMapping(method = RequestMethod.GET, value = "/me")
  58.     public ResponseEntity currentUser(@AuthenticationPrincipal UserDetails userDetails){
  59.         Map<Object, Object> model = new HashMap<>();
  60.         model.put("username", userDetails.getUsername());
  61.         model.put("roles", userDetails.getAuthorities()
  62.                 .stream()
  63.                 .map(GrantedAuthority::getAuthority)
  64.                 .collect(toList())
  65.         );
  66.         return ok(model);
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement