Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ out/
.vscode/

### oauth config ###
src/main/resources/application-oauth.yml
src/test/resources/application-oauth.yml

### environment ###
Expand Down
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'

//Apache HttpClient ๋ฒ„์ „ ๋ฌธ์ œ ํ•ด๊ฒฐ
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 @@ -22,12 +22,12 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "giftHub_item")
@Table(name = "gift_hub_item")
public class GiftHubItem extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "giftHub_item_id")
@Column(name = "gift_hub_item_id")
private Long giftHubItemId;

@ColumnDefault("1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import kcs.funding.fundingboost.domain.entity.common.BaseTimeEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -17,7 +16,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "item")
public class Item extends BaseTimeEntity {
public class Item {
@Id
@Column(name = "item_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestFactory;
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 +56,30 @@ public class KaKaoLoginService {
* ์นด์นด์˜ค ์„œ๋ฒ„์—์„œ ์ธ์ฆ ํ† ํฐ์„ ๋ฐ›์•„์˜ค๋Š” ๋ฉ”์†Œ๋“œ
*/
public String getAccessTokenFromKakao(String clientId, String code) throws IOException {
// ํ”„๋ก์‹œ ์„ค์ •
org.apache.hc.core5.http.HttpHost proxy = new org.apache.hc.core5.http.HttpHost(
"http", "krmp-proxy.9rum.cc", 3128);
org.apache.hc.client5.http.impl.classic.HttpClientBuilder clientBuilder = org.apache.hc.client5.http.impl.classic.HttpClientBuilder.create();
clientBuilder.setProxy(proxy);
org.apache.hc.client5.http.classic.HttpClient httpClient = clientBuilder.build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(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 +89,28 @@ 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 {
// ํ”„๋ก์‹œ ์„ค์ •
org.apache.hc.core5.http.HttpHost proxy = new org.apache.hc.core5.http.HttpHost(
"http", "krmp-proxy.9rum.cc", 3128);
org.apache.hc.client5.http.impl.classic.HttpClientBuilder clientBuilder = org.apache.hc.client5.http.impl.classic.HttpClientBuilder.create();
clientBuilder.setProxy(proxy);
org.apache.hc.client5.http.classic.HttpClient httpClient = clientBuilder.build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(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,20 +142,21 @@ public JwtDto getJwtDto(String accessToken) throws IOException {
* ๊ฐœ์ธ์ •๋ณด ์ €์žฅ ๋ฐ ์นœ๊ตฌ๋ชฉ๋ก ์—…๋ฐ์ดํŠธ
*/
private CustomUserDetails processLoginAndRelationships(KakaoOAuth2User kakaoOAuth2User, String accessToken) {
String provider = "kakao";
String providerId = kakaoOAuth2User.getId();
// String provider = "kakao";
// String providerId = kakaoOAuth2User.getId();
// String password = passwordEncoder.encode(providerId + "_" + providerId);
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);
Expand Down Expand Up @@ -220,8 +237,16 @@ private void processFirstRelationships(String friendsList, Member member) {
* ์นด์นด์˜ค๋กœ๋ถ€ํ„ฐ ์นœ๊ตฌ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
*/
private static String getFriendsListByKakao(String accessToken) {
// ํ”„๋ก์‹œ ์„ค์ •
org.apache.hc.core5.http.HttpHost proxy = new org.apache.hc.core5.http.HttpHost(
"http", "krmp-proxy.9rum.cc", 3128);
org.apache.hc.client5.http.impl.classic.HttpClientBuilder clientBuilder = org.apache.hc.client5.http.impl.classic.HttpClientBuilder.create();
clientBuilder.setProxy(proxy);
org.apache.hc.client5.http.classic.HttpClient httpClient = clientBuilder.build();
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(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
Loading