-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
330 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/GDG/whatssue/domain/user/entity/PrincipalDetails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//package GDG.whatssue.domain.user.entity; | ||
// | ||
//import GDG.whatssue.domain.member.entity.ClubMember; | ||
//import GDG.whatssue.domain.user.entity.User; | ||
//import GDG.whatssue.global.common.Role; | ||
//import jakarta.transaction.Transactional; | ||
//import lombok.Getter; | ||
//import org.springframework.security.core.GrantedAuthority; | ||
//import org.springframework.security.core.userdetails.UserDetails; | ||
// | ||
//import java.util.ArrayList; | ||
//import java.util.Collection; | ||
//import java.util.List; | ||
// | ||
///* | ||
//* 시큐리티가 /login 주소 요청이 오면 낚아채서 로그인을 진행시킨다. | ||
//* 로그인을 진행이 완료가 되면 시큐리티 session을 만들어준다. (Security ContextHolder) | ||
//* 오브젝트 => Authentication 타입 객체 | ||
//* Authentication 안에 User 정보가 있어야 됨. | ||
//* User 오브젝트 타입 => UserDetails 타입 객체 | ||
//* | ||
//* Security Session => Authentication => UserDetails(PrincipalDetails) | ||
// */ | ||
//@Getter | ||
//@Transactional | ||
//public class PrincipalDetails implements UserDetails { | ||
// | ||
// private User user; | ||
// | ||
// public PrincipalDetails(User user) { | ||
// this.user = user; | ||
// // 추가: 세션을 열고 연관된 엔티티를 즉시 로딩 | ||
// this.user.getClubMemberList().size(); | ||
// } | ||
// | ||
// @Override | ||
// public Collection<? extends GrantedAuthority> getAuthorities() { | ||
// List<ClubMember> clubMemberList = user.getClubMemberList(); | ||
// Collection<GrantedAuthority> authorities = new ArrayList<>(); | ||
// for (ClubMember clubMember : clubMemberList) { | ||
// authorities.add((GrantedAuthority) () -> { | ||
// Long clubId = clubMember.getClub().getId(); | ||
// Role role = clubMember.getRole(); | ||
// System.out.println("ROLE_" + clubId + role); | ||
// return "ROLE_" + clubId + role; | ||
// }); | ||
// } | ||
// return authorities; | ||
// } | ||
// | ||
// @Override | ||
// public String getPassword() { | ||
// return null; | ||
// } | ||
// | ||
// @Override | ||
// public String getUsername() { | ||
// return user.getUserName(); | ||
// } | ||
// | ||
// @Override | ||
// public boolean isAccountNonExpired() { | ||
// return true; | ||
// } | ||
// | ||
// @Override | ||
// public boolean isAccountNonLocked() { | ||
// return true; | ||
// } | ||
// | ||
// @Override | ||
// public boolean isCredentialsNonExpired() { | ||
// return true; | ||
// } | ||
// | ||
// @Override | ||
// public boolean isEnabled() { | ||
// // 우리 사이트 1년동안 회원이 로그인을 안하면 휴먼 계정으로 하기로 함. | ||
// // 현재시간 - 로긴시간 => 1년을 초과하면 return false; | ||
// return true; | ||
// } | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 65 additions & 65 deletions
130
src/main/java/GDG/whatssue/domain/user/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,65 @@ | ||
package GDG.whatssue.domain.user.service; | ||
import GDG.whatssue.domain.user.dto.UserDto; | ||
import GDG.whatssue.global.auth.PrincipalDetails; | ||
import GDG.whatssue.domain.user.entity.User; | ||
import GDG.whatssue.domain.user.repository.UserRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Slf4j | ||
@Transactional | ||
//시큐리티 설정에서 loginProcessingUrl("/login"); | ||
//login 요청이 오면 자동으로 UserDetailsService 타입으로 IoC되어있는 loadUserByUsername 함수가 실행 | ||
public class UserService implements UserDetailsService { | ||
|
||
private final UserRepository userRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
public void signUp(UserDto userDto) { | ||
User user = User.builder() | ||
.userNick(userDto.getUserNick()) | ||
.userPw(userDto.getUserPw()) | ||
.userEmail(userDto.getUserEmail()) | ||
.userName(userDto.getUserName()) | ||
.userPhone(userDto.getUserPhone()) | ||
.role("ROLE_USER") | ||
.build(); | ||
// 비밀번호 암호화 : 비밀번호 암호화가 안되어있으면 security로 로그인을 할 수 없음. | ||
user.setUserPw(passwordEncoder.encode(user.getUserPw())); | ||
userRepository.save(user); | ||
} | ||
@Override | ||
@Transactional | ||
public UserDetails loadUserByUsername(String userName) { | ||
User user = userRepository.findByUserNick(userName); | ||
|
||
if(userRepository.findByUserNick(userName) == null) | ||
return null; | ||
|
||
PrincipalDetails userDetails = new PrincipalDetails(user); | ||
System.out.println("로그인 완료"); | ||
return userDetails; | ||
|
||
// UserDetails가 return이 되면 시큐리티 session의 Authentication의 내부에 userDetail이 저장이 된다. | ||
// Session(내부 Authentication(내부 UserDetails)) | ||
} | ||
|
||
public UserDto getUserInfo(PrincipalDetails principalDetails) { | ||
User user = principalDetails.getUser(); | ||
return UserDto.builder() | ||
.userNick(user.getUserNick()) | ||
.userPw(user.getUserPw()) | ||
.userEmail(user.getUserEmail()) | ||
.userName(user.getUserName()) | ||
.userPhone(user.getUserPhone()) | ||
.build(); | ||
} | ||
|
||
|
||
} | ||
//package GDG.whatssue.domain.user.service; | ||
//import GDG.whatssue.domain.user.dto.UserDto; | ||
//import GDG.whatssue.domain.user.entity.PrincipalDetails; | ||
//import GDG.whatssue.domain.user.entity.User; | ||
//import GDG.whatssue.domain.user.repository.UserRepository; | ||
//import jakarta.transaction.Transactional; | ||
//import lombok.RequiredArgsConstructor; | ||
//import lombok.extern.slf4j.Slf4j; | ||
//import org.springframework.security.core.userdetails.UserDetailsService; | ||
//import org.springframework.security.core.userdetails.UserDetails; | ||
//import org.springframework.security.crypto.password.PasswordEncoder; | ||
//import org.springframework.stereotype.Service; | ||
// | ||
//@RequiredArgsConstructor | ||
//@Service | ||
//@Slf4j | ||
//@Transactional | ||
////시큐리티 설정에서 loginProcessingUrl("/login"); | ||
////login 요청이 오면 자동으로 UserDetailsService 타입으로 IoC되어있는 loadUserByUsername 함수가 실행 | ||
//public class UserService implements UserDetailsService { | ||
// | ||
// private final UserRepository userRepository; | ||
// private final PasswordEncoder passwordEncoder; | ||
// public void signUp(UserDto userDto) { | ||
// User user = User.builder() | ||
//// .userNick(userDto.getUserNick()) | ||
//// .userPw(userDto.getUserPw()) | ||
//// .userEmail(userDto.getUserEmail()) | ||
// .userName(userDto.getUserName()) | ||
//// .userPhone(userDto.getUserPhone()) | ||
// .role("ROLE_USER") | ||
// .build(); | ||
// // 비밀번호 암호화 : 비밀번호 암호화가 안되어있으면 security로 로그인을 할 수 없음. | ||
// user.setUserPw(passwordEncoder.encode(user.getUserPw())); | ||
// userRepository.save(user); | ||
// } | ||
// @Override | ||
// @Transactional | ||
// public UserDetails loadUserByUsername(String userName) { | ||
// User user = userRepository.findByUserNick(userName); | ||
// | ||
// if(userRepository.findByUserNick(userName) == null) | ||
// return null; | ||
// | ||
// PrincipalDetails userDetails = new PrincipalDetails(user); | ||
// System.out.println("로그인 완료"); | ||
// return userDetails; | ||
// | ||
// // UserDetails가 return이 되면 시큐리티 session의 Authentication의 내부에 userDetail이 저장이 된다. | ||
// // Session(내부 Authentication(내부 UserDetails)) | ||
// } | ||
// | ||
// public UserDto getUserInfo(PrincipalDetails principalDetails) { | ||
// User user = principalDetails.getUser(); | ||
// return UserDto.builder() | ||
// .userNick(user.getUserNick()) | ||
// .userPw(user.getUserPw()) | ||
// .userEmail(user.getUserEmail()) | ||
// .userName(user.getUserName()) | ||
// .userPhone(user.getUserPhone()) | ||
// .build(); | ||
// } | ||
// | ||
// | ||
//} |
Oops, something went wrong.