-
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.
Browse files
Browse the repository at this point in the history
로그인 확인 및 JWT 생성 테스트 완료
- Loading branch information
Showing
18 changed files
with
308 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,6 @@ dependencyManagement { | |
} | ||
} | ||
|
||
tasks.named('test') { | ||
enabled = false | ||
} | ||
test { | ||
useJUnitPlatform() | ||
} |
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: 9 additions & 0 deletions
9
src/main/java/univ/goormthon/kongju/global/config/JpaAuditingConfig.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,9 @@ | ||
package univ.goormthon.kongju.global.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
|
||
@EnableJpaAuditing | ||
@Configuration | ||
public class JpaAuditingConfig { | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/univ/goormthon/kongju/global/config/KeyConfig.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,16 @@ | ||
package univ.goormthon.kongju.global.config; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
@Configuration | ||
public class KeyConfig { | ||
|
||
@Bean | ||
public SecretKey secretKey() { | ||
return Jwts.SIG.HS256.key().build(); | ||
} | ||
} |
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
24 changes: 0 additions & 24 deletions
24
src/main/java/univ/goormthon/kongju/global/config/WebConfig.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/main/java/univ/goormthon/kongju/global/exception/UnsupportedProviderException.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,9 @@ | ||
package univ.goormthon.kongju.global.exception; | ||
|
||
import univ.goormthon.kongju.global.exception.dto.ErrorCode; | ||
|
||
public class UnsupportedProviderException extends KongjuException { | ||
public UnsupportedProviderException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
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
24 changes: 0 additions & 24 deletions
24
src/main/java/univ/goormthon/kongju/global/jwt/controller/JwtController.java
This file was deleted.
Oops, something went wrong.
2 changes: 2 additions & 0 deletions
2
src/main/java/univ/goormthon/kongju/global/jwt/dto/request/TokenRequest.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
33 changes: 33 additions & 0 deletions
33
src/main/java/univ/goormthon/kongju/global/oauth2/controller/OAuth2Controller.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,33 @@ | ||
package univ.goormthon.kongju.global.oauth2.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import univ.goormthon.kongju.domain.member.entity.Member; | ||
import univ.goormthon.kongju.domain.member.service.MemberService; | ||
import univ.goormthon.kongju.global.jwt.dto.request.TokenRequest; | ||
import univ.goormthon.kongju.global.jwt.service.JwtProvider; | ||
import univ.goormthon.kongju.global.oauth2.provider.OAuth2UserInfoProvider; | ||
import univ.goormthon.kongju.global.oauth2.provider.OAuth2UserInfoProviderFactory; | ||
|
||
import java.util.Map; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/auth") | ||
public class OAuth2Controller { | ||
|
||
private final JwtProvider jwtProvider; | ||
private final OAuth2UserInfoProviderFactory oAuth2UserInfoProviderFactory; | ||
private final MemberService memberService; | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<?> login(@RequestParam String provider, | ||
@RequestBody TokenRequest tokenRequest) { | ||
OAuth2UserInfoProvider providerInstance = oAuth2UserInfoProviderFactory.getProvider(provider); | ||
Map<String, Object> userInfo = providerInstance.getUserInfo(tokenRequest.accessToken()); | ||
|
||
Member member = memberService.findOrRegisterMember(userInfo); | ||
return ResponseEntity.ok(jwtProvider.issueJwtToken(member.getEmail())); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/univ/goormthon/kongju/global/oauth2/provider/KakaoUserInfoProvider.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,43 @@ | ||
package univ.goormthon.kongju.global.oauth2.provider; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
|
||
import java.util.Map; | ||
|
||
@Component | ||
public class KakaoUserInfoProvider implements OAuth2UserInfoProvider { | ||
|
||
private final WebClient webClient; | ||
|
||
public KakaoUserInfoProvider(WebClient.Builder webClientBuilder) { | ||
this.webClient = webClientBuilder.baseUrl("https://kapi.kakao.com").build(); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getUserInfo(String accessToken) { | ||
var response = webClient.get() | ||
.uri("/v2/user/me") | ||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) | ||
.retrieve() | ||
.bodyToMono(Map.class) | ||
.block(); | ||
|
||
// 카카오 사용자 정보 변환 | ||
Map<String, Object> kakaoAccount = (Map<String, Object>) response.get("kakao_account"); | ||
Map<String, Object> properties = (Map<String, Object>) response.get("properties"); | ||
|
||
return Map.of( | ||
"id", response.get("id"), | ||
"email", kakaoAccount.get("email"), | ||
"nickname", properties.get("nickname"), | ||
"profile_image", properties.get("profile_image") | ||
); | ||
} | ||
|
||
@Override | ||
public String getProviderName() { | ||
return "kakao"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/univ/goormthon/kongju/global/oauth2/provider/OAuth2UserInfoProvider.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,9 @@ | ||
package univ.goormthon.kongju.global.oauth2.provider; | ||
|
||
import java.util.Map; | ||
|
||
public interface OAuth2UserInfoProvider { | ||
|
||
Map<String, Object> getUserInfo(String accessToken); | ||
String getProviderName(); // kakao, google, naver, ... | ||
} |
23 changes: 23 additions & 0 deletions
23
...main/java/univ/goormthon/kongju/global/oauth2/provider/OAuth2UserInfoProviderFactory.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,23 @@ | ||
package univ.goormthon.kongju.global.oauth2.provider; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import univ.goormthon.kongju.global.exception.UnsupportedProviderException; | ||
import univ.goormthon.kongju.global.exception.dto.ErrorCode; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class OAuth2UserInfoProviderFactory { | ||
|
||
private final List<OAuth2UserInfoProvider> providers; | ||
|
||
public OAuth2UserInfoProvider getProvider(String providerName) { | ||
return providers.stream() | ||
.filter(provider -> provider.getProviderName().equalsIgnoreCase(providerName)) | ||
.findFirst() | ||
.orElseThrow(() -> new UnsupportedProviderException(ErrorCode.UNSUPPORTED_PROVIDER)); | ||
} | ||
} | ||
|
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
Oops, something went wrong.