Skip to content

Commit 7a56848

Browse files
authored
Merge branch 'main' into develop
2 parents 0c45d2d + 5bd8168 commit 7a56848

File tree

8 files changed

+40
-194
lines changed

8 files changed

+40
-194
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.univ.sohwakhaeng.auth.api;
22

33

4+
import com.univ.sohwakhaeng.auth.api.dto.KakaoLoginDto;
5+
import com.univ.sohwakhaeng.auth.api.dto.TokenDto;
6+
import com.univ.sohwakhaeng.auth.service.OAuth2LoginService;
47
import com.univ.sohwakhaeng.global.common.dto.BaseResponse;
58
import com.univ.sohwakhaeng.global.common.exception.SuccessCode;
6-
import jakarta.servlet.http.HttpServletResponse;
9+
import java.io.IOException;
710
import lombok.RequiredArgsConstructor;
811
import lombok.extern.slf4j.Slf4j;
9-
import com.univ.sohwakhaeng.auth.api.dto.TokenDto;
10-
import com.univ.sohwakhaeng.auth.service.OAuth2LoginService;
11-
import org.springframework.web.bind.annotation.*;
12-
13-
import java.io.IOException;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RestController;
1415

1516
@RestController
1617
@Slf4j
@@ -19,11 +20,8 @@ public class AuthController {
1920

2021
private final OAuth2LoginService oAuth2LoginService;
2122

22-
@GetMapping("/kakao")
23-
public BaseResponse<Void> auth(@RequestParam(value = "code") String code, HttpServletResponse response) throws IOException {
24-
String token = oAuth2LoginService.proccessOAuth2Login(code).accessToken();
25-
String redirectUrl = "http://localhost:5173/main?token=" + token;
26-
response.sendRedirect(redirectUrl);
27-
return BaseResponse.success(SuccessCode.USER_LOGIN_SUCCESS);
23+
@PostMapping("/public/login")
24+
public BaseResponse<TokenDto> auth(@RequestBody KakaoLoginDto kakaoLoginDto) throws IOException {;
25+
return BaseResponse.success(SuccessCode.USER_LOGIN_SUCCESS, oAuth2LoginService.proccessOAuth2Login(kakaoLoginDto));
2826
}
2927
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.univ.sohwakhaeng.auth.api.dto;
2+
3+
public record KakaoLoginDto(
4+
String providerId,
5+
String nickname,
6+
String profileUrl
7+
) {}

src/main/java/com/univ/sohwakhaeng/auth/api/dto/KakaoTokenDto.java

-24
This file was deleted.

src/main/java/com/univ/sohwakhaeng/auth/domain/KakaoUserInfo.java

-56
This file was deleted.

src/main/java/com/univ/sohwakhaeng/auth/domain/OAuth2UserInfo.java

-11
This file was deleted.

src/main/java/com/univ/sohwakhaeng/auth/domain/PrincipalDetails.java

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
11
package com.univ.sohwakhaeng.auth.domain;
22

33
import com.univ.sohwakhaeng.user.User;
4-
import org.springframework.security.core.GrantedAuthority;
5-
import org.springframework.security.core.userdetails.UserDetails;
6-
import org.springframework.security.oauth2.core.user.OAuth2User;
7-
84
import java.util.ArrayList;
95
import java.util.Collection;
10-
import java.util.Map;
6+
import org.springframework.security.core.GrantedAuthority;
7+
import org.springframework.security.core.userdetails.UserDetails;
118

12-
public class PrincipalDetails implements UserDetails, OAuth2User {
9+
public class PrincipalDetails implements UserDetails {
1310

1411
private final User user;
15-
private OAuth2UserInfo oAuth2UserInfo;
1612

1713
public PrincipalDetails(User user) {
1814
this.user = user;
1915
}
2016

21-
public PrincipalDetails(User user, OAuth2UserInfo oAuth2UserInfo) {
22-
this.user = user;
23-
this.oAuth2UserInfo = oAuth2UserInfo;
24-
}
25-
26-
@Override
27-
public Map<String, Object> getAttributes() {
28-
return oAuth2UserInfo.getAttributes();
29-
}
30-
3117
@Override
3218
public Collection<? extends GrantedAuthority> getAuthorities() {
3319
Collection<GrantedAuthority> collection = new ArrayList<>();
@@ -66,11 +52,6 @@ public boolean isEnabled() {
6652
return UserDetails.super.isEnabled();
6753
}
6854

69-
@Override
70-
public String getName() {
71-
return user.getUsername();
72-
}
73-
7455
public User getUser() {
7556
return this.user;
7657
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
package com.univ.sohwakhaeng.auth.service;
22

3-
import com.univ.sohwakhaeng.auth.api.dto.KakaoTokenDto;
4-
import com.univ.sohwakhaeng.auth.domain.KakaoUserInfo;
5-
import com.univ.sohwakhaeng.auth.domain.OAuth2UserInfo;
3+
import com.univ.sohwakhaeng.auth.api.dto.KakaoLoginDto;
4+
import com.univ.sohwakhaeng.auth.api.dto.TokenDto;
65
import com.univ.sohwakhaeng.auth.jwt.TokenProvider;
76
import com.univ.sohwakhaeng.user.Authority;
87
import com.univ.sohwakhaeng.user.SocialType;
98
import com.univ.sohwakhaeng.user.User;
109
import com.univ.sohwakhaeng.user.repository.UserRepository;
1110
import lombok.RequiredArgsConstructor;
12-
import com.univ.sohwakhaeng.auth.api.dto.TokenDto;
13-
import org.springframework.beans.factory.annotation.Value;
14-
import org.springframework.http.MediaType;
15-
import org.springframework.http.ResponseEntity;
1611
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1712
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
1813
import org.springframework.security.core.Authentication;
1914
import org.springframework.security.crypto.password.PasswordEncoder;
2015
import org.springframework.stereotype.Service;
2116
import org.springframework.transaction.annotation.Transactional;
22-
import org.springframework.util.LinkedMultiValueMap;
23-
import org.springframework.util.MultiValueMap;
24-
import org.springframework.web.client.RestClient;
25-
26-
import java.util.Map;
2717

2818
@Service
2919
@RequiredArgsConstructor
@@ -34,68 +24,30 @@ public class OAuth2LoginService {
3424
private final AuthenticationManagerBuilder authenticationManagerBuilder;
3525
private final TokenProvider tokenProvider;
3626

37-
@Value("${KAKAO_CLIENT_ID}")
38-
private String kakaoClientId;
39-
40-
@Value("${KAKAO_CLIENT_SECRET}")
41-
private String kakaoClientSecret;
42-
43-
@Value("${REDIRECT_URL_KAKAO}")
44-
private String redirectUrlKakao;
45-
46-
private static final String AUTHORIZATION_CODE = "authorization_code";
47-
4827
@Transactional
49-
public TokenDto proccessOAuth2Login(String code) {
50-
KakaoTokenDto kakaoTokenDto = getToken(code, kakaoClientId, kakaoClientSecret, redirectUrlKakao,
51-
"https://kauth.kakao.com/oauth/token", KakaoTokenDto.class);
52-
Map<String, Object> attributes = getUserInfo(kakaoTokenDto.accessToken(), "https://kapi.kakao.com/v2/user/me");
53-
return authenticateUser(new KakaoUserInfo(attributes));
28+
public TokenDto proccessOAuth2Login(KakaoLoginDto kakaoLoginDto) {
29+
return authenticateUser(kakaoLoginDto);
5430
}
5531

56-
private <T> T getToken(String code, String clientId, String clientSecret, String redirectUri, String tokenUri,
57-
Class<T> responseType) {
58-
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
59-
params.add("grant_type", AUTHORIZATION_CODE);
60-
params.add("client_id", clientId);
61-
params.add("redirect_uri", redirectUri);
62-
params.add("code", code);
63-
params.add("client_secret", clientSecret);
64-
ResponseEntity<T> response = RestClient.create().post()
65-
.uri(tokenUri)
66-
.accept(MediaType.APPLICATION_JSON)
67-
.body(params)
68-
.retrieve()
69-
.toEntity(responseType);
70-
return response.getBody();
71-
}
72-
73-
private Map<String, Object> getUserInfo(String accessToken, String userInfoUri) {
74-
ResponseEntity<Map> response = RestClient.create().get()
75-
.uri(userInfoUri)
76-
.header("Authorization", "Bearer " + accessToken)
77-
.retrieve()
78-
.toEntity(Map.class);
79-
return response.getBody();
32+
private TokenDto authenticateUser(KakaoLoginDto kakaoLoginDto) {
33+
User user = getUserDomain(kakaoLoginDto);
34+
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(
35+
new UsernamePasswordAuthenticationToken(user.getUsername(),
36+
kakaoLoginDto.providerId() + kakaoLoginDto.nickname())
37+
);
38+
return tokenProvider.generateTokenDto(authentication);
8039
}
8140

82-
private TokenDto authenticateUser(OAuth2UserInfo oAuth2userInfo) {
83-
User user = userRepository.findByUsername(oAuth2userInfo.getProviderId())
41+
private User getUserDomain(KakaoLoginDto kakaoLoginDto) {
42+
return userRepository.findByUsername(kakaoLoginDto.providerId())
8443
.orElseGet(() -> userRepository.save(User.builder() // <- DB에 사용자의 정보가 없다면 자동 회원가입
85-
.username(oAuth2userInfo.getProviderId())
86-
.name(oAuth2userInfo.getName())
87-
.nickname(oAuth2userInfo.getName())
88-
.password(passwordEncoder.encode(oAuth2userInfo.getProviderId() + oAuth2userInfo.getName()))
89-
.socialType(SocialType.of(oAuth2userInfo.getProvider()))
44+
.username(kakaoLoginDto.providerId())
45+
.name(kakaoLoginDto.nickname())
46+
.nickname(kakaoLoginDto.nickname())
47+
.password(passwordEncoder.encode(kakaoLoginDto.providerId() + kakaoLoginDto.nickname()))
48+
.socialType(SocialType.of("kakao"))
9049
.authority(Authority.ROLE_USER)
91-
.profileImgUrl(oAuth2userInfo.getProfileImage())
50+
.profileImgUrl(kakaoLoginDto.profileUrl())
9251
.build()));
93-
94-
Authentication authentication = authenticationManagerBuilder.getObject().authenticate(
95-
new UsernamePasswordAuthenticationToken(user.getUsername(),
96-
oAuth2userInfo.getProviderId() + oAuth2userInfo.getName())
97-
);
98-
99-
return tokenProvider.generateTokenDto(authentication);
10052
}
10153
}

src/main/resources/application-dev.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ cloud:
5454
static: ${AWS_REGION}
5555
stack:
5656
auto: false
57-
5857
iamport:
5958
apikey: ${IAMPORT_API_KEY}
60-
secretkey: ${IAMPORT_SECRET_KEY}
59+
secretkey: ${IAMPORT_SECRET_KEY}

0 commit comments

Comments
 (0)