1
1
package com .univ .sohwakhaeng .auth .service ;
2
2
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 ;
6
5
import com .univ .sohwakhaeng .auth .jwt .TokenProvider ;
7
6
import com .univ .sohwakhaeng .user .Authority ;
8
7
import com .univ .sohwakhaeng .user .SocialType ;
9
8
import com .univ .sohwakhaeng .user .User ;
10
9
import com .univ .sohwakhaeng .user .repository .UserRepository ;
11
10
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 ;
16
11
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
17
12
import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
18
13
import org .springframework .security .core .Authentication ;
19
14
import org .springframework .security .crypto .password .PasswordEncoder ;
20
15
import org .springframework .stereotype .Service ;
21
16
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 ;
27
17
28
18
@ Service
29
19
@ RequiredArgsConstructor
@@ -34,68 +24,30 @@ public class OAuth2LoginService {
34
24
private final AuthenticationManagerBuilder authenticationManagerBuilder ;
35
25
private final TokenProvider tokenProvider ;
36
26
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
-
48
27
@ 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 );
54
30
}
55
31
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 );
80
39
}
81
40
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 ())
84
43
.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" ))
90
49
.authority (Authority .ROLE_USER )
91
- .profileImgUrl (oAuth2userInfo . getProfileImage ())
50
+ .profileImgUrl (kakaoLoginDto . profileUrl ())
92
51
.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 );
100
52
}
101
53
}
0 commit comments