UserAuthenticationProvider.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.its.traf.webapp.security;
  2. import com.its.traf.webapp.service.UserService;
  3. import com.its.traf.webapp.vo.voUser;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.security.authentication.AuthenticationProvider;
  6. import org.springframework.security.authentication.BadCredentialsException;
  7. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  8. import org.springframework.security.core.Authentication;
  9. import org.springframework.security.core.AuthenticationException;
  10. import org.springframework.stereotype.Component;
  11. @Slf4j
  12. @Component
  13. public class UserAuthenticationProvider implements AuthenticationProvider {
  14. private final UserService userService;
  15. public UserAuthenticationProvider(UserService userService) {
  16. this.userService = userService;
  17. }
  18. @Override
  19. public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  20. String id = authentication.getName();
  21. String password = (String) authentication.getCredentials();
  22. voUser user = this.userService.loadUserByUsername(id);
  23. //voUser user = new voUser();
  24. if (user == null) {
  25. throw new BadCredentialsException("Login Error !!");
  26. }
  27. //인증안하고 바로 접속
  28. //List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
  29. //roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
  30. //user.setAuthorities(roles);
  31. UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(id, password, user.getAuthorities());
  32. result.setDetails(user);
  33. return result;
  34. }
  35. @Override
  36. public boolean supports(Class authentication) {
  37. return authentication.equals(UsernamePasswordAuthenticationToken.class);
  38. }
  39. }