-
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
[Feature] 회원 탈퇴 마킹 처리 및 카카오 로그인 구현
- Loading branch information
Showing
25 changed files
with
651 additions
and
28 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
10 changes: 10 additions & 0 deletions
10
src/main/java/meltingpot/server/auth/controller/dto/OAuthSignInRequestDto.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,10 @@ | ||
package meltingpot.server.auth.controller.dto; | ||
|
||
import meltingpot.server.domain.entity.enums.OAuthType; | ||
|
||
public record OAuthSignInRequestDto( | ||
OAuthType type, | ||
String code, | ||
String push_token | ||
) { | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/meltingpot/server/auth/controller/dto/OAuthSignupRequestDto.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,21 @@ | ||
package meltingpot.server.auth.controller.dto; | ||
|
||
import lombok.Builder; | ||
import meltingpot.server.domain.entity.enums.OAuthType; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
@Builder | ||
public record OAuthSignupRequestDto( | ||
OAuthType OauthType, | ||
String email, | ||
String name, | ||
String gender, | ||
LocalDate birth, | ||
String nationality, | ||
List<String> languages, | ||
List<ProfileImageRequestDto> profileImages, | ||
String pushToken | ||
) { | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/meltingpot/server/auth/oauth/OAuthUserDetails.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,58 @@ | ||
package meltingpot.server.auth.oauth; | ||
|
||
import meltingpot.server.domain.entity.Account; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class OAuthUserDetails implements UserDetails { | ||
|
||
private final Account account; | ||
|
||
public OAuthUserDetails(Account account){ | ||
this.account = account; | ||
} | ||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); | ||
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER")); | ||
return grantedAuthorities; | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return this.account.getPassword(); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return this.account.getName(); | ||
} | ||
|
||
//계정 만료 여부 | ||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
//계정 잠김 여부 | ||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
//계정 정보 변경 필요 여부 | ||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
//계정 활성화 여부 | ||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/meltingpot/server/auth/oauth/kakao/KaKaoTokenDto.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,10 @@ | ||
package meltingpot.server.auth.oauth.kakao; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record KaKaoTokenDto ( | ||
String accessToken, | ||
String refreshToken | ||
){ | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/meltingpot/server/auth/oauth/kakao/KakaoDto.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,13 @@ | ||
package meltingpot.server.auth.oauth.kakao; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
@Builder | ||
@Data | ||
public class KakaoDto { | ||
|
||
private long id; | ||
private String email; | ||
private String nickname; | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.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,110 @@ | ||
package meltingpot.server.auth.oauth.kakao; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.json.simple.parser.JSONParser; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
public class KakaoService { | ||
|
||
@Value("${spring.security.oauth2.client.registration.kakao.client-id}") | ||
private String KAKAO_CLIENT_ID; | ||
|
||
@Value("${spring.security.oauth2.client.registration.kakao.client-secret}") | ||
private String KAKAO_CLIENT_SECRET; | ||
|
||
@Value("${spring.security.oauth2.client.registration.kakao.redirect-uri}") | ||
private String redirect_uri; | ||
|
||
private final static String KAKAO_AUTH_URI = "https://kauth.kakao.com"; | ||
private final static String KAKAO_API_URI = "https://kapi.kakao.com"; | ||
|
||
public String getKakaoLogin(String redirect_uri) { // 프론트 구현부 | ||
return KAKAO_AUTH_URI + "/oauth/authorize" | ||
+ "?client_id=" + KAKAO_CLIENT_ID | ||
+ "&redirect_uri=" + redirect_uri | ||
+ "&response_type=code"; | ||
} | ||
|
||
public KaKaoTokenDto getKakaoInfo(String code) throws Exception { | ||
if (code == null) throw new Exception("Failed get authorization code"); | ||
|
||
String accessToken = ""; | ||
String refreshToken = ""; | ||
|
||
try { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Content-type", "application/x-www-form-urlencoded"); | ||
|
||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); | ||
params.add("grant_type" , "authorization_code"); | ||
params.add("client_id" , KAKAO_CLIENT_ID); | ||
params.add("client_secret", KAKAO_CLIENT_SECRET); | ||
params.add("code" , code); | ||
params.add("redirect_uri" , redirect_uri); | ||
|
||
RestTemplate restTemplate = new RestTemplate(); | ||
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, headers); | ||
|
||
ResponseEntity<String> response = restTemplate.exchange( | ||
KAKAO_AUTH_URI + "/oauth/token", | ||
HttpMethod.POST, | ||
httpEntity, | ||
String.class | ||
); | ||
|
||
JSONParser jsonParser = new JSONParser(); | ||
JSONObject jsonObj = (JSONObject) jsonParser.parse(response.getBody()); | ||
|
||
accessToken = (String) jsonObj.get("access_token"); | ||
refreshToken = (String) jsonObj.get("refresh_token"); | ||
} catch (Exception e) { | ||
throw new Exception("KAKAO API call failed"); | ||
} | ||
|
||
return KaKaoTokenDto.builder() | ||
.accessToken(accessToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
} | ||
|
||
public KakaoDto getUserInfoWithToken(String accessToken) throws Exception { | ||
//HttpHeader 생성 | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Authorization", "Bearer " + accessToken); | ||
headers.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); | ||
|
||
//HttpHeader 담기 | ||
RestTemplate rt = new RestTemplate(); | ||
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(headers); | ||
ResponseEntity<String> response = rt.exchange( | ||
KAKAO_API_URI + "/v2/user/me", | ||
HttpMethod.POST, | ||
httpEntity, | ||
String.class | ||
); | ||
|
||
//Response 데이터 파싱 | ||
JSONParser jsonParser = new JSONParser(); | ||
JSONObject jsonObj = (JSONObject) jsonParser.parse(response.getBody()); | ||
JSONObject account = (JSONObject) jsonObj.get("kakao_account"); | ||
JSONObject profile = (JSONObject) account.get("profile"); | ||
|
||
long id = (long) jsonObj.get("id"); | ||
String email = String.valueOf(account.get("email")); | ||
String nickname = String.valueOf(profile.get("nickname")); | ||
|
||
return KakaoDto.builder() | ||
.id(id) | ||
.email(email) | ||
.nickname(nickname).build(); | ||
} | ||
} |
Oops, something went wrong.