dto파일에 CustomUserDTO.java
// 의문점 : getter와 setter를 쓰는 이유는 클래스를 만들때 데이터를 보호시키고
// 어떤 오브젝트 혹은 변수의 값을 가져오거나, 설정해주는 역할을 하는 메소드를 말한다.
// extends는 부모를 상속할때 쓰는 문법
@Getter
@Setter
@ToString
public class CustomUserDTO extends User {
String username; // 변수들
String password;
String phone;
int age;
Collection<GrantedAuthority> authorities;
// 기본적인 생성자에서 새로운 변수를 추가한 생성자 구현
public CustomUserDTO(String username, String password,
Collection<GrantedAuthority> authorities,
String phone, int age) {
super(username, password, authorities); // 부모쪽에 있는걸 상속시키고 (기본적인것은 수행 해야 함)
this.authorities = authorities; // 변수들 리턴
this.username = username;
this.password = password;
this.phone = phone;
this.age = age;
}
// 기본 생성자(아이디, 암호, 권한)
public CustomUserDTO(String username, String password, Collection<? extends GrantedAuthority> authorities) {
super(username, password, authorities);
}
}
service 파일 안에 CustomDetailsService.java
@Service
@Slf4j
public class CustomDetailsService implements UserDetailsService{
final String format = "SECURITY => {}";
@Autowired
MemberMapper mMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
log.info(format, username);
// 아이디가 전송되면 UserDetails 타입으로 변환해서 리턴
MemberDTO member = mMapper.selectMemberOne(username);
if(member != null){
// 권한 여러개 처리를 위해 스프링을 하지 않았음.
String[] strRole = { member.getRole() };
Collection<GrantedAuthority> role = AuthorityUtils.createAuthorityList(strRole);
// 아이디, 암호, 권한을 컬렉션 타입
return new CustomUserDTO(
member.getUserid(),
member.getUserpw(),
role,
member.getPhone(),
member.getAge());
}
else { // 오류로 보이지 않게 로그인 실패 처리
String[] strRole = { "_" };
Collection<GrantedAuthority> role = AuthorityUtils.createAuthorityList(strRole);
// 아이디, 암호, 권한을 컬렉션 타입
return new User(username, "", role);
}
}
}