Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ dependencies {
//actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'io.micrometer:micrometer-registry-prometheus'

implementation group: 'org.apache.httpcomponents.client5', name: 'httpclient5', version: '5.2'

}

def generated = 'src/main/generated'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ public void minusPoint(int usingPoint) {
public void plusPoint(int exchangePoint) {
point += exchangePoint;
}

public void changeProfileImgUrl(String newProfileImgUrl) {
this.profileImgUrl = newProfileImgUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import kcs.funding.fundingboost.domain.security.entity.KakaoOAuthToken;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpHost;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
Expand Down Expand Up @@ -55,22 +58,27 @@ public class KaKaoLoginService {
* ์นด์นด์˜ค ์„œ๋ฒ„์—์„œ ์ธ์ฆ ํ† ํฐ์„ ๋ฐ›์•„์˜ค๋Š” ๋ฉ”์†Œ๋“œ
*/
public String getAccessTokenFromKakao(String clientId, String code) throws IOException {
// ํ”„๋ก์‹œ ์„ค์ •
HttpHost proxy = new HttpHost("krmp-proxy.9rum.cc", "http", 3128);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
HttpClient httpClient = HttpClients.custom().setProxy(proxy).build();
requestFactory.setHttpClient(httpClient);
// KAKAO ์„œ๋ฒ„์— ์ธ์ฆ ํ† ํฐ ๋ฐœ๊ธ‰ ์š”์ฒญ
RestClient restClient = RestClient.builder()
.requestFactory(new JdkClientHttpRequestFactory())
.requestFactory(requestFactory)
.messageConverters(convert -> convert.add(new AllEncompassingFormHttpMessageConverter()))
.baseUrl("https://kauth.kakao.com/oauth/token")
.build();

//ํ† ํฐ ์š”์ฒญ์— ๋“ค์–ด๊ฐˆ body
// ํ† ํฐ ์š”์ฒญ์— ๋“ค์–ด๊ฐˆ body
MultiValueMap<String, String> objectMaping = new LinkedMultiValueMap<>();
objectMaping.add("grant_type", "authorization_code");
objectMaping.add("client_id", clientId);
objectMaping.add("redirect_uri", redirectUri);
objectMaping.add("code", code);
objectMaping.add("client_secret", clientSecret);

//์นด์นด์˜ค ์„œ๋ฒ„๋กœ post ํ† ํฐ์š”์ฒญ
// ์นด์นด์˜ค ์„œ๋ฒ„๋กœ post ํ† ํฐ์š”์ฒญ
ResponseEntity<KakaoOAuthToken> response = restClient.post()
.body(objectMaping)
.contentType(APPLICATION_FORM_URLENCODED)
Expand All @@ -80,21 +88,26 @@ public String getAccessTokenFromKakao(String clientId, String code) throws IOExc
if (response.getBody() == null) {
throw new OAuth2AuthenticationException("Invalid authorization code");
}
//ํ† ํฐ ๋ฐœ์ทŒ
// ํ† ํฐ ๋ฐœ์ทŒ
String accessToken = response.getBody().access_token();
String refreshToken = response.getBody().refresh_token();

return accessToken;

}


/**
* ํ† ํฐ์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ ํ›„ ์ธ์ฆ, access token ๋ฐ refresh token ๋ฐœํ–‰
*/
public JwtDto getJwtDto(String accessToken) throws IOException {
// ํ”„๋ก์‹œ ์„ค์ •
HttpHost proxy = new HttpHost("krmp-proxy.9rum.cc", "http", 3128);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
HttpClient httpClient = HttpClients.custom().setProxy(proxy).build();
requestFactory.setHttpClient(httpClient);
// kakao ์„œ๋ฒ„์— access token์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ
RestClient restClient = RestClient.builder()
.requestFactory(new JdkClientHttpRequestFactory())
.requestFactory(requestFactory)
.messageConverters(convert -> convert.add(new AllEncompassingFormHttpMessageConverter()))
.baseUrl("https://kapi.kakao.com/v2/user/me")
.build();
Expand Down Expand Up @@ -126,24 +139,25 @@ public JwtDto getJwtDto(String accessToken) throws IOException {
* ๊ฐœ์ธ์ •๋ณด ์ €์žฅ ๋ฐ ์นœ๊ตฌ๋ชฉ๋ก ์—…๋ฐ์ดํŠธ
*/
private CustomUserDetails processLoginAndRelationships(KakaoOAuth2User kakaoOAuth2User, String accessToken) {
String provider = "kakao";
String providerId = kakaoOAuth2User.getId();
String username = kakaoOAuth2User.getName();
String password = passwordEncoder.encode(provider + "_" + providerId);
String password = passwordEncoder.encode("string");
String email = kakaoOAuth2User.getEmail();
String profileImgUrl = kakaoOAuth2User.getImageUrl();
String uuid = kakaoOAuth2User.getAttribute("id").toString();
String kakao_id = "kakao_" + kakaoOAuth2User.getAttribute("id").toString();

Member findMember = memberRepository.findByEmail(email).orElse(null);
String friendsList = getFriendsListByKakao(accessToken);

CustomUserDetails customUserDetails;
if (findMember == null) {
Member createMember = Member.createMember(username, email, password, profileImgUrl, uuid);
Member createMember = Member.createMember(username, email, password, profileImgUrl, kakao_id);
customUserDetails = new CustomUserDetails(kakaoOAuth2User.getAttributes(), createMember);
memberRepository.save(createMember);
processFirstRelationships(friendsList, createMember);
} else {
if (findMember.getProfileImgUrl() != profileImgUrl) {
findMember.changeProfileImgUrl(profileImgUrl);
}
customUserDetails = new CustomUserDetails(kakaoOAuth2User.getAttributes(), findMember);
processRelationships(friendsList, findMember);
}
Expand Down Expand Up @@ -220,8 +234,14 @@ private void processFirstRelationships(String friendsList, Member member) {
* ์นด์นด์˜ค๋กœ๋ถ€ํ„ฐ ์นœ๊ตฌ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
*/
private static String getFriendsListByKakao(String accessToken) {
// ํ”„๋ก์‹œ ์„ค์ •
HttpHost proxy = new HttpHost("krmp-proxy.9rum.cc", "http", 3128);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
HttpClient httpClient = HttpClients.custom().setProxy(proxy).build();
requestFactory.setHttpClient(httpClient);

RestClient restClient = RestClient.builder()
.requestFactory(new JdkClientHttpRequestFactory())
.requestFactory(requestFactory)
.messageConverters(converter -> converter.add(new AllEncompassingFormHttpMessageConverter()))
.baseUrl("https://kapi.kakao.com/v1/api/talk/friends")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class ElasticsearchConfig {

@Bean
public RestClient restClient() {
org.apache.hc.core5.http.HttpHost httpHost = new org.apache.hc.core5.http.HttpHost(host, port);

return RestClient
.builder(new HttpHost(host, port, "http"))
.build();
Expand Down