-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from lotteon2/dev-auth-refactoring
add: add the cors at config
- Loading branch information
Showing
16 changed files
with
228 additions
and
123 deletions.
There are no files selected for viewing
Binary file not shown.
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/bit/lot/flower/auth/oauth/OauthController.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,30 @@ | ||
package com.bit.lot.flower.auth.oauth; | ||
|
||
import com.bit.lot.flower.auth.common.valueobject.AuthenticationProvider; | ||
import com.bit.lot.flower.auth.oauth.facade.OauthLoginAccessTokenRequestFacade; | ||
import com.bit.lot.flower.auth.oauth.facade.OauthUserMeInfoRequestFacade; | ||
import com.bit.lot.flower.auth.social.dto.command.SocialLoginRequestCommand; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class OauthController { | ||
|
||
private final OauthLoginAccessTokenRequestFacade oauthLoginRequestFacade; | ||
private final OauthUserMeInfoRequestFacade userMeInfoRequestFacade; | ||
|
||
@GetMapping("/login/oauth2/{provider}") | ||
public ResponseEntity<SocialLoginRequestCommand> requestSocialInfo(@RequestParam String code, | ||
@PathVariable AuthenticationProvider provider) throws JsonProcessingException { | ||
String authorizationCode = oauthLoginRequestFacade.request(provider, code); | ||
return ResponseEntity.ok(userMeInfoRequestFacade.getUserInfo(authorizationCode, 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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/bit/lot/flower/auth/oauth/facade/OauthLoginAccessTokenRequestFacade.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 com.bit.lot.flower.auth.oauth.facade; | ||
|
||
import com.bit.lot.flower.auth.common.valueobject.AuthenticationProvider; | ||
import com.bit.lot.flower.auth.oauth.util.access.GetKakaoAccessKeyHttpUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.parameters.P; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class OauthLoginAccessTokenRequestFacade { | ||
|
||
private final GetKakaoAccessKeyHttpUtil getKakaoAccessKeyHttpUtil; | ||
|
||
public String request(AuthenticationProvider provider, String code) { | ||
if (provider.equals(AuthenticationProvider.kakao)) { | ||
return getKakaoAccessKeyHttpUtil.getAccessToken(code); | ||
} else { | ||
throw new IllegalArgumentException("존재 하지 않는 인증 제공k자입니다."); | ||
} | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/bit/lot/flower/auth/oauth/facade/OauthUserMeInfoRequestFacade.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,50 @@ | ||
package com.bit.lot.flower.auth.oauth.facade; | ||
|
||
import com.bit.lot.flower.auth.common.valueobject.AuthId; | ||
import com.bit.lot.flower.auth.common.valueobject.AuthenticationProvider; | ||
import com.bit.lot.flower.auth.oauth.dto.response.LoginResponseDto; | ||
import com.bit.lot.flower.auth.oauth.http.util.RequestUserMeRestTemplateUtil; | ||
import com.bit.lot.flower.auth.social.dto.command.SocialLoginRequestCommand; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.HashMap; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class OauthUserMeInfoRequestFacade { | ||
|
||
private final RequestUserMeRestTemplateUtil requestUserMeRestTemplateUtil; | ||
|
||
@Value("${spring.security.oauth2.client.provider.kakao.user-info-uri}") | ||
private String kakaoUserMeURL; | ||
|
||
public SocialLoginRequestCommand getUserInfo(String code, AuthenticationProvider provider) | ||
throws JsonProcessingException { | ||
if (provider.equals(AuthenticationProvider.kakao)) { | ||
return getKakaoUserData(requestUserMeRestTemplateUtil.getUserInfo(code, kakaoUserMeURL)); | ||
} | ||
throw new IllegalArgumentException("존재 하지 않는 인증 제공자입니다."); | ||
} | ||
|
||
private SocialLoginRequestCommand getKakaoUserData(String data) throws JsonProcessingException { | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
HashMap<String, Object> resultMap = mapper.readValue(data, HashMap.class); | ||
|
||
HashMap<String, Object> properties = (HashMap<String, Object>) resultMap.get("properties"); | ||
HashMap<String, Object> kakaoAccount = (HashMap<String, Object>) resultMap.get("kakao_account"); | ||
|
||
Long id = Long.valueOf(String.valueOf(resultMap.get("id"))); | ||
String nickname = (String) properties.get("nickname"); | ||
String email = (String) kakaoAccount.get("email"); | ||
String phoneNumber = (String) kakaoAccount.get("phone_number"); | ||
|
||
|
||
return new SocialLoginRequestCommand(new AuthId(id),email,phoneNumber,nickname); | ||
|
||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...main/java/com/bit/lot/flower/auth/oauth/http/util/RequestRestTemplateAccessTokenUtil.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,37 @@ | ||
package com.bit.lot.flower.auth.oauth.http.util; | ||
|
||
import com.bit.lot.flower.auth.oauth.dto.response.LoginResponseDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class RequestRestTemplateAccessTokenUtil { | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
public String request(String code, String clientId, String redirectURI, String requestURL) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Content-type", "application/x-www-form-urlencoded; charset=utf-8"); | ||
|
||
MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); | ||
|
||
body.add("grant_type", "authorization_code"); | ||
body.add("client_id", clientId); | ||
body.add("redirect_uri", redirectURI); | ||
body.add("code", code); | ||
|
||
LoginResponseDto loginResponseDto = restTemplate.postForObject( | ||
requestURL, | ||
new HttpEntity<>(body, headers), | ||
LoginResponseDto.class); | ||
|
||
return loginResponseDto.getAccessToken(); | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/bit/lot/flower/auth/oauth/http/util/RequestUserMeRestTemplateUtil.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,37 @@ | ||
package com.bit.lot.flower.auth.oauth.http.util; | ||
|
||
|
||
import com.bit.lot.flower.auth.oauth.dto.response.LoginResponseDto; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class RequestUserMeRestTemplateUtil { | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
public String getUserInfo(String accessCode,String userMeURL) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Content-type", "application/x-www-form-urlencoded; charset=utf-8"); | ||
headers.add("Authorization", "Bearer " + accessCode); | ||
|
||
ResponseEntity<String> response = | ||
restTemplate.exchange(userMeURL, | ||
HttpMethod.GET, | ||
new HttpEntity<>(null, headers), | ||
String.class); | ||
|
||
return response.getBody(); | ||
} | ||
|
||
|
||
|
||
|
||
} |
39 changes: 0 additions & 39 deletions
39
src/main/java/com/bit/lot/flower/auth/oauth/util/EncryptionUtil.java
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
src/main/java/com/bit/lot/flower/auth/oauth/util/UserInfoCipherHelper.java
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/main/java/com/bit/lot/flower/auth/oauth/util/access/GetKakaoAccessKeyHttpUtil.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,26 @@ | ||
package com.bit.lot.flower.auth.oauth.util.access; | ||
|
||
import com.bit.lot.flower.auth.oauth.http.util.RequestRestTemplateAccessTokenUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class GetKakaoAccessKeyHttpUtil { | ||
|
||
private final RequestRestTemplateAccessTokenUtil requestRestTemplateAccessTokenUtil; | ||
|
||
private final String requestURI = "https://kauth.kakao.com/oauth/token"; | ||
@Value("${spring.security.oauth2.client.registration.kakao.client-id}") | ||
private String clientId; | ||
private String redirectURI = "http://localhost:3000/login/oauth/"; | ||
|
||
public String getAccessToken(String code) { | ||
log.info("redirectURL: " + redirectURI); | ||
return requestRestTemplateAccessTokenUtil.request(code, clientId, redirectURI, requestURI); | ||
} | ||
|
||
} |
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.