|
4 | 4 | import io.swagger.v3.oas.annotations.tags.Tag; |
5 | 5 | import jakarta.servlet.http.HttpServletResponse; |
6 | 6 | import lombok.RequiredArgsConstructor; |
7 | | -import org.springframework.beans.factory.annotation.Value; |
8 | | -import org.springframework.http.*; |
| 7 | +import org.springframework.http.HttpHeaders; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.http.ResponseCookie; |
| 10 | +import org.springframework.http.ResponseEntity; |
9 | 11 | import org.springframework.web.bind.annotation.*; |
10 | | -import org.springframework.web.reactive.function.BodyInserters; |
11 | 12 | import org.springframework.web.reactive.function.client.WebClient; |
12 | | -import org.tuna.zoopzoop.backend.domain.auth.deprecated.KakaoTokenResponse; |
13 | | -import org.tuna.zoopzoop.backend.domain.auth.deprecated.KakaoUserInfoResponse; |
14 | 13 | import org.tuna.zoopzoop.backend.domain.auth.service.KakaoUserInfoService; |
15 | 14 | import org.tuna.zoopzoop.backend.domain.member.entity.Member; |
16 | 15 | import org.tuna.zoopzoop.backend.domain.member.service.MemberService; |
17 | 16 | import org.tuna.zoopzoop.backend.global.config.jwt.JwtProperties; |
18 | 17 | import org.tuna.zoopzoop.backend.global.rsData.RsData; |
19 | 18 | import org.tuna.zoopzoop.backend.global.security.jwt.JwtUtil; |
20 | 19 |
|
21 | | -import java.util.HashMap; |
22 | | -import java.util.Map; |
23 | | - |
24 | 20 | @RestController |
25 | 21 | @RequiredArgsConstructor |
26 | 22 | @RequestMapping("api/v1/auth") |
@@ -109,76 +105,4 @@ public ResponseEntity<RsData<Void>> refreshToken(@CookieValue(name = "refreshTok |
109 | 105 | null |
110 | 106 | )); |
111 | 107 | } |
112 | | - |
113 | | - @GetMapping("/test") |
114 | | - @Operation(summary = "소셜 로그인 테스트용 엔드 포인트") |
115 | | - public ResponseEntity<RsData<Map<String, String>>> test( |
116 | | - @RequestParam String code |
117 | | - ) { |
118 | | - Map<String, Object> attributes = getAttributesFromCode(code); |
119 | | - Member member = kakaoUserInfoService.processUser(attributes); |
120 | | - |
121 | | - String accessToken = jwtUtil.generateToken(member); |
122 | | - String refreshToken = jwtUtil.generateRefreshToken(member); |
123 | | - |
124 | | - Map<String, String> tokens = new HashMap<>(); |
125 | | - tokens.put("accessToken", accessToken); |
126 | | - tokens.put("refreshToken", refreshToken); |
127 | | - |
128 | | - return ResponseEntity |
129 | | - .status(HttpStatus.OK) |
130 | | - .body(new RsData<>( |
131 | | - "200", |
132 | | - "로그인 테스트 완료.", |
133 | | - tokens |
134 | | - )); |
135 | | - } |
136 | | - |
137 | | - @Value("${spring.security.oauth2.client.provider.kakao.token-uri}") |
138 | | - String TOKEN_URL; |
139 | | - |
140 | | - @Value("${spring.security.oauth2.client.provider.kakao.user-info-uri}") |
141 | | - String USER_INFO_URL; |
142 | | - |
143 | | - @Value("${spring.security.oauth2.client.registration.kakao.client-id}") |
144 | | - String CLIENT_ID; |
145 | | - |
146 | | - String REDIRECT_URI = "http://localhost:8080/oauth/kakao"; |
147 | | - |
148 | | - public Map<String, Object> getAttributesFromCode(String code) { |
149 | | - KakaoTokenResponse tokenResponse = webClient.post() |
150 | | - .uri(TOKEN_URL) |
151 | | - .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
152 | | - .body(BodyInserters.fromFormData("grant_type","authorization_code") |
153 | | - .with("client_id", CLIENT_ID) |
154 | | - .with("redirect_uri", REDIRECT_URI) |
155 | | - .with("code", code)) |
156 | | - .retrieve() |
157 | | - .bodyToMono(KakaoTokenResponse.class) |
158 | | - .block(); |
159 | | - |
160 | | - String accessToken = tokenResponse.access_token(); |
161 | | - |
162 | | - KakaoUserInfoResponse userInfo = webClient.get() |
163 | | - .uri(USER_INFO_URL) |
164 | | - .headers(headers -> headers.setBearerAuth(accessToken)) |
165 | | - .retrieve() |
166 | | - .bodyToMono(KakaoUserInfoResponse.class) |
167 | | - .block(); |
168 | | - |
169 | | - Map<String, Object> kakaoAccountMap = new HashMap<>(); |
170 | | - |
171 | | - Map<String, Object> profileMap = new HashMap<>(); |
172 | | - profileMap.put("nickname", userInfo.kakao_account().profile().nickname()); |
173 | | - profileMap.put("profile_image_url", userInfo.kakao_account().profile().profile_image_url()); |
174 | | - |
175 | | - kakaoAccountMap.put("profile", profileMap); |
176 | | - |
177 | | - // 4. attributes Map 생성 |
178 | | - Map<String, Object> attributes = new HashMap<>(); |
179 | | - attributes.put("id", userInfo.id()); |
180 | | - attributes.put("kakao_account", kakaoAccountMap); |
181 | | - |
182 | | - return attributes; |
183 | | - } |
184 | 108 | } |
0 commit comments