Skip to content

Commit

Permalink
✨ Feat: Spring Security를 위한 User 객체에 UserDetails 인터페이스 상속 (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Oct 16, 2023
1 parent 40a9089 commit 229ac32
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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()));
}
}
56 changes: 52 additions & 4 deletions src/main/java/com/diareat/diareat/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -23,6 +26,7 @@ public class User {
private String name; // 닉네임

@JsonIgnore
@Column(length = 100, nullable = false, unique = true)
private String keyCode; // 로그인 식별키

private String image; // 프로필 사진 경로
Expand All @@ -43,6 +47,50 @@ public class User {
@OneToMany(mappedBy = "user", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) // 유저가 탈퇴하면 즐겨찾기 음식도 삭제
private List<FavoriteFood> favoriteFoods = new ArrayList<>();

// Jwt 전용 설정 (UserDetails 인터페이스 구현)

@ElementCollection(fetch = FetchType.EAGER) //roles 컬렉션
private List<String> roles = new ArrayList<>();

@Override //사용자의 권한 목록 리턴
public Collection<? extends GrantedAuthority> 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();
Expand Down

0 comments on commit 229ac32

Please sign in to comment.