-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🚚 User 엔티티 패키지 이동 * 🚚 UserRepository 패키지 이동 * ♻️ 코드 스타일 변경 * 💄 서비스 인터페이스 생성
- Loading branch information
1 parent
2380acd
commit c5ab024
Showing
12 changed files
with
125 additions
and
112 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
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
9 changes: 6 additions & 3 deletions
9
...d/util/jwt/repository/UserRepository.java → ...r_back_end/repository/UserRepository.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,11 +1,14 @@ | ||
package org.example.gather_back_end.util.jwt.repository; | ||
package org.example.gather_back_end.repository; | ||
|
||
import org.example.gather_back_end.util.jwt.entity.User; | ||
import org.example.gather_back_end.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
User findByNickname(String nickname); | ||
|
||
User findByUsername(String username); | ||
|
||
|
||
} |
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
8 changes: 3 additions & 5 deletions
8
src/main/java/org/example/gather_back_end/util/jwt/dto/GoogleResponse.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
2 changes: 2 additions & 0 deletions
2
src/main/java/org/example/gather_back_end/util/jwt/dto/UserDto.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
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
90 changes: 2 additions & 88 deletions
90
src/main/java/org/example/gather_back_end/util/jwt/service/CustomOAuth2UserService.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,94 +1,8 @@ | ||
package org.example.gather_back_end.util.jwt.service; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.time.LocalDateTime; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.SecretKey; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.example.gather_back_end.util.jwt.dto.CustomOAuth2User; | ||
import org.example.gather_back_end.util.jwt.dto.GoogleResponse; | ||
import org.example.gather_back_end.util.jwt.dto.OAuth2Response; | ||
import org.example.gather_back_end.util.jwt.dto.UserDto; | ||
import org.example.gather_back_end.util.jwt.entity.User; | ||
import org.example.gather_back_end.util.jwt.repository.UserRepository; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class CustomOAuth2UserService extends DefaultOAuth2UserService { | ||
|
||
private final UserRepository userRepository; | ||
private final LocalDateTimeNumericEncryption localDateTimeNumericEncryption; | ||
|
||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
|
||
OAuth2User oAuth2User = super.loadUser(userRequest); | ||
log.info("[CustomOAuth2UserService 클래스][loadUser 메소드] : " + oAuth2User); | ||
|
||
String registrationId = userRequest.getClientRegistration().getRegistrationId(); | ||
OAuth2Response oAuth2Response; | ||
if (registrationId.equals("google")) { | ||
oAuth2Response = new GoogleResponse(oAuth2User.getAttributes()); | ||
} else { | ||
return null; | ||
} | ||
|
||
String username = oAuth2Response.getProvider() + " " + oAuth2Response.getProviderId(); | ||
|
||
StringBuilder numericEncryptedDateTime; | ||
|
||
while(true) { | ||
SecretKey secretKey; | ||
try { | ||
secretKey = KeyGenerator.getInstance(localDateTimeNumericEncryption.getALGORITHM()).generateKey(); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
LocalDateTime code = LocalDateTime.now(); | ||
try { | ||
numericEncryptedDateTime = new StringBuilder(localDateTimeNumericEncryption.encryptToSixDigits(code, secretKey)); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if(userRepository.findByNickname("USER" + numericEncryptedDateTime)==null) { | ||
break; | ||
} | ||
} | ||
|
||
String nickname = "USER"+numericEncryptedDateTime; | ||
|
||
User existData = userRepository.findByUsername(username); | ||
|
||
if (existData == null) { | ||
|
||
userRepository.save(User.createAllUserInfo(username,oAuth2Response.getName(),oAuth2Response.getEmail(),"ROLE_USER",nickname)); | ||
|
||
UserDto userDto = new UserDto(); | ||
userDto.setNickname(nickname); | ||
userDto.setName(oAuth2Response.getName()); | ||
userDto.setRole("ROLE_USER"); | ||
|
||
return new CustomOAuth2User(userDto); | ||
} else { | ||
|
||
existData.updateUserInfo(oAuth2Response.getName(), oAuth2Response.getEmail()); | ||
|
||
userRepository.save(existData); | ||
|
||
UserDto userDto = new UserDto(); | ||
userDto.setNickname(existData.getNickname()); | ||
userDto.setName(oAuth2Response.getName()); | ||
userDto.setRole(existData.getRole()); | ||
|
||
return new CustomOAuth2User(userDto); | ||
} | ||
} | ||
public interface CustomOAuth2UserService { | ||
OAuth2User loadUser(OAuth2UserRequest userRequest); | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/java/org/example/gather_back_end/util/jwt/service/CustomOAuth2UserServiceImpl.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,101 @@ | ||
package org.example.gather_back_end.util.jwt.service; | ||
|
||
import java.security.NoSuchAlgorithmException; | ||
import java.time.LocalDateTime; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.SecretKey; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.example.gather_back_end.domain.User; | ||
import org.example.gather_back_end.repository.UserRepository; | ||
import org.example.gather_back_end.util.jwt.dto.CustomOAuth2User; | ||
import org.example.gather_back_end.util.jwt.dto.GoogleResponse; | ||
import org.example.gather_back_end.util.jwt.dto.OAuth2Response; | ||
import org.example.gather_back_end.util.jwt.dto.UserDto; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; | ||
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; | ||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class CustomOAuth2UserServiceImpl extends DefaultOAuth2UserService implements CustomOAuth2UserService { | ||
|
||
private final UserRepository userRepository; | ||
private final LocalDateTimeNumericEncryption localDateTimeNumericEncryption; | ||
|
||
@Override | ||
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException { | ||
|
||
OAuth2User oAuth2User = super.loadUser(userRequest); | ||
log.info("[CustomOAuth2UserService 클래스][loadUser 메소드] : " + oAuth2User); | ||
|
||
String registrationId = userRequest.getClientRegistration().getRegistrationId(); | ||
OAuth2Response oAuth2Response; | ||
|
||
if (registrationId.equals("google")) { oAuth2Response = new GoogleResponse(oAuth2User.getAttributes()); } | ||
else { return null; } | ||
|
||
String username = oAuth2Response.getProvider() + " " + oAuth2Response.getProviderId(); | ||
|
||
StringBuilder numericEncryptedDateTime; | ||
|
||
while(true) { | ||
SecretKey secretKey; | ||
try { | ||
secretKey = KeyGenerator.getInstance(localDateTimeNumericEncryption.getALGORITHM()).generateKey(); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
} | ||
LocalDateTime code = LocalDateTime.now(); | ||
try { | ||
numericEncryptedDateTime = new StringBuilder(localDateTimeNumericEncryption.encryptToSixDigits(code, secretKey)); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (userRepository.findByNickname("USER" + numericEncryptedDateTime) == null) { | ||
break; | ||
} | ||
} | ||
|
||
String nickname = "USER" + numericEncryptedDateTime; | ||
|
||
User existData = userRepository.findByUsername(username); | ||
|
||
if (existData == null) { | ||
|
||
userRepository.save(User.createAllUserInfo( | ||
username, | ||
oAuth2Response.getName(), | ||
oAuth2Response.getEmail(), | ||
"ROLE_USER", | ||
nickname) | ||
); | ||
|
||
UserDto userDto = UserDto.builder() | ||
.nickname(nickname) | ||
.name(oAuth2Response.getName()) | ||
.role("ROLE_USER") | ||
.build(); | ||
|
||
return new CustomOAuth2User(userDto); | ||
|
||
} else { | ||
|
||
existData.updateUserInfo(oAuth2Response.getName(), oAuth2Response.getEmail()); | ||
|
||
userRepository.save(existData); | ||
|
||
UserDto userDto = UserDto.builder() | ||
.nickname(existData.getNickname()) | ||
.name(existData.getName()) | ||
.role(existData.getRole()) | ||
.build(); | ||
|
||
return new CustomOAuth2User(userDto); | ||
} | ||
} | ||
} |
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