diff --git a/src/main/java/com/diareat/diareat/auth/service/CustomUserDetailService.java b/src/main/java/com/diareat/diareat/auth/service/CustomUserDetailService.java new file mode 100644 index 0000000..622e09f --- /dev/null +++ b/src/main/java/com/diareat/diareat/auth/service/CustomUserDetailService.java @@ -0,0 +1,22 @@ +package com.diareat.diareat.auth.service; + +import com.diareat.diareat.user.repository.UserRepository; +import com.diareat.diareat.util.api.ResponseCode; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.stereotype.Service; + +@RequiredArgsConstructor +@Service +public class CustomUserDetailService implements UserDetailsService { + + private final UserRepository userRepository; + + @Override + public UserDetails loadUserByUsername(String keyCode) throws UsernameNotFoundException { + return userRepository.findByKeyCode(keyCode) + .orElseThrow(() -> new UsernameNotFoundException(ResponseCode.USER_NOT_FOUND.getMessage())); + } +} diff --git a/src/main/java/com/diareat/diareat/user/domain/User.java b/src/main/java/com/diareat/diareat/user/domain/User.java index 6852f48..98e16d7 100644 --- a/src/main/java/com/diareat/diareat/user/domain/User.java +++ b/src/main/java/com/diareat/diareat/user/domain/User.java @@ -3,18 +3,21 @@ import com.diareat.diareat.food.domain.FavoriteFood; import com.diareat.diareat.food.domain.Food; import com.fasterxml.jackson.annotation.JsonIgnore; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.authority.SimpleGrantedAuthority; import javax.persistence.*; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity -public class User { +public class User implements UserDetails { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id") @@ -23,6 +26,7 @@ public class User { private String name; // 닉네임 @JsonIgnore + @Column(length = 100, nullable = false, unique = true) private String keyCode; // 로그인 식별키 private String image; // 프로필 사진 경로 @@ -43,6 +47,50 @@ public class User { @OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) // 유저가 탈퇴하면 즐겨찾기 음식도 삭제 private List favoriteFoods = new ArrayList<>(); + // Jwt 전용 설정 (UserDetails 인터페이스 구현) + + @ElementCollection(fetch = FetchType.EAGER) //roles 컬렉션 + private List roles = new ArrayList<>(); + + @Override //사용자의 권한 목록 리턴 + public Collection getAuthorities() { + return this.roles.stream() + .map(SimpleGrantedAuthority::new) + .collect(Collectors.toList()); + } + + @Override + public String getUsername() { + return keyCode; + } + + @Override + public String getPassword() { + return null; + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } + + // Jwt 전용 설정 종료 + // 생성 메서드 public static User createUser(String name, String image, String keyCode, int height, int weight, int gender, int age, BaseNutrition baseNutrition) { User user = new User();