์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- ๋ฐฑ์ค
- ์ฝ๋ฉํ ์คํธ
- ์๋ฃ๊ตฌ์กฐ
- boj11653
- ๋ฆฌ์กํธ
- ๋จ์ํ ์คํธ
- ์ปด๊ณต
- ๋ฐฑ์คํ์ด
- ํ๋ก๊ทธ๋๋ฐ
- ํ์ด์ฌ
- ๋ชจ๋ฐ์ผ์ฑํ๋ก๊ทธ๋๋ฐ
- ๋ฐฑ์๋
- ๊ทธ๋ฆฌ๋์๊ณ ๋ฆฌ์ฆ
- ChatGPT
- ๋ฐฑ์ค1436
- ๊ฐ๋ฐ์
- ์ฐ์ ์์ํ
- ์ดํญ๊ณ์
- ๊ทธ๋ฆฌ๋
- ์๊ณ ๋ฆฌ์ฆ
- SSE
- ์ปดํจํฐ๊ณตํ
- ์น๊ฐ๋ฐ
- ํ๋ก ํธ์ค๋
- ๋ฆฌ์กํธ๋ค์ดํฐ๋ธ
- ์คํ๋ง
- spring
- ์ฝ๋ฉ
- ์ปด๊ณต์
- ์น๊ฐ๋ฐ๊ธฐ๋ก
- Today
- Total
๐ป๐ญ๐ง๐
[์น ๊ฐ๋ฐ ํ๋ก์ ํธ] 2. ๊ตฌ๊ธ ์์ ๋ก๊ทธ์ธ ๊ตฌํ ๋ณธ๋ฌธ
[์น ๊ฐ๋ฐ ํ๋ก์ ํธ] 2. ๊ตฌ๊ธ ์์ ๋ก๊ทธ์ธ ๊ตฌํ
adorableco 2024. 1. 5. 23:11๊ตฌ๊ธ ๋ก๊ทธ์ธ ๊ตฌํ์ ์๋์ ๋ธ๋ก๊ทธ๋ฅผ ๊ต์ฅํ ๋ง์ด ์ฐธ๊ณ ํ์ต๋๋ค!!! ๋๋ฌด๋๋ฌด๋๋ฌด๋๋ฌด ๋์์ด ๋ง์ด ๋์ด์ ๊ฐ์ฌํฉ๋๋ค...
https://antdev.tistory.com/71
[Google Login API] ์์ ๋ก๊ทธ์ธ ์์ฒญ Redirect ์ฒ๋ฆฌ - 2 (Spring Boot ๋ ํผ๋ฐ์ค๋ฅผ ๋ณด๋ฉด์ ๊ตฌํํด๋ณด๋ ๊ตฌ๊ธ ์
Spring Boot ํ๊ฒฝ์์ ๊ตฌ๊ธ ์์ ๋ก๊ทธ์ธ API๋ฅผ REST ๋ฐฉ์์ผ๋ก ๊ตฌํํ๊ธฐ ์ด์ ๊ธ 2020/10/18 - [OAuth/Google Login API] - [Google Login API] ์์ ๋ก๊ทธ์ธ ์์ฒญ Redirect ์ฒ๋ฆฌ (Spring Boot ๋ ํผ๋ฐ์ค๋ฅผ ๋ณด๋ฉด์ ๊ตฌํํด๋ณด๋ ๊ตฌ
antdev.tistory.com
๋๋ฉ์ธ ๊ณ์ธต ๊ตฌ์กฐ ๋ํ๋ด๋ ค๊ณ ๋๋์ด tree๋ ์ค์นํ์๋ค! ์ด๋ฒ ์์
๋ก๊ทธ์ธ์ ๊ตฌํํ ๋ ์ํฅ์ ๋ฐ์ ๋๋ฉ์ธ์ ๋ค์๊ณผ ๊ฐ๋ค.
โโโ com
โโโ example
โโโ fit_friends
โโโ FitFriendsApplication.java
โโโ config
โ โโโ auth
โ โโโ GoogleOAuth.java
โ โโโ SecurityConfig.java
โ โโโ SocialOAuth.java
โ โโโ dto
โ โโโ GoogleRequestAccessTokenDto.java
โ โโโ OAuthAttributes.java
โ โโโ SessionUser.java
โโโ controller
โ โโโ LoginController.java
โ โโโ PostApiController.java
SocialOAuth.java
public interface SocialOAuth {
String getOAuthRedirectUrl();
String requestAccessToken(String code);
}
getOAuthRedirectUrl()
- ๊ตฌ๊ธ ๋ก๊ทธ์ธ ์ฐฝ์ผ๋ก ์ด๋ํ๋ url์ ๊ตฌ์ฑํ๋ ๋ฉ์๋
requestAccessToken(String code)
- ๊ตฌ๊ธ ๋ก๊ทธ์ธ ์ฐฝ์์ ๋ก๊ทธ์ธ ํ ๋ฐ๋ code๋ฅผ ์ด์ฉํด accessToken์ ๋ง๋ค ์ ์๋๋กํ๋ url์ ๊ตฌ์ฑํ๋ ๋ฉ์๋
GoogleOAuth.java
SocialOAuth
์ ๊ตฌํ์ฒด
package com.example.fit_friends.config.auth;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
@Component
@RequiredArgsConstructor
public class GoogleOAuth implements SocialOAuth{
@Value("${google.client.id}")
private String clientId;
@Value("${google.redirect.uri}")
private String redirectUri;
@Value("${google.base.uri}")
private String googleBaseUri;
@Value("${google.client.secret}")
private String clientSecret;
@Value("${google.token_uri}")
private String tokenUri;
@Override
public String getOAuthRedirectUrl() {
Map<String,Object> params = new HashMap<>();
params.put("client_id",clientId);
params.put("response_type","code");
params.put("redirect_uri",redirectUri);
params.put("scope","profile");
String paramsString = params.entrySet().stream()
.map(x->x.getKey()+"="+x.getValue())
.collect(Collectors.joining("&"));
return googleBaseUri+"?"+paramsString;
}
@Override
public String requestAccessToken(String code) {
RestTemplate restTemplate = new RestTemplate();
Map<String,Object> params = new HashMap<>();
params.put("code",code);
params.put("client_id",clientId);
params.put("grant_type","authorization_code");
params.put("response_type","code");
params.put("redirect_uri",redirectUri);
params.put("scope","profile");
params.put("client_secret",clientSecret);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(tokenUri, params, String.class);
if(responseEntity.getStatusCode()== HttpStatus.OK){
return responseEntity.getBody();
}
return "๊ตฌ๊ธ ๋ก๊ทธ์ธ ์์ฒญ ์ฒ๋ฆฌ ์คํจ";
}
}
@Value()
๋ก ๊ฐ์ ธ์จ application.properties
์ ๋ณ์๊ฐ์ด null
๋ก ๋ฌ๋ค๋ฉด,
- ํด๋น ํด๋์ค๋ฅผ new ๋ก ๊ฐ์ ธ์จ๊ฑด ์๋์ง ์ฒดํฌ
@Value()
๊ฐ properties์ ๊ฐ์ ๊ฐ์ ธ์ค๋๊ฑด ํด๋์ค๊ฐ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก๋๋ ์๊ฐ์ธ๋ฐ, ๋ค๋ฅธ ํด๋์ค์์ new ์ฐ์ฐ์๋ฅผ ํตํด ์ด ํด๋์ค๋ฅผ ์์ฑํด๋ฒ๋ฆฌ๋ฉด ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋ก๋์ง ์๊ธฐ ๋๋ฌธ์@Value()
๋ฅผ ํตํด ๋ณ์์ ๊ฐ์ด ์ ์ฅ๋์ง๋ ์๋ ๊ฒ์ด๋ค. โก๏ธ ์ด๋ฐ ์ค์๋ฅผ ํ ๋๋ฉด ๋ถ๋ช ์คํ๋ง์ผ๋ก ๊ฐ๋ฐํ๊ณ ์๋๋ฐ ์คํ๋ง์ ๊ธฐ๋ฅ์ ์ ๋๋ก ์ฐ์ง ์๊ณ ์๋ค๋๊ฒ ๋๊ปด์ง๋ค..
getOAuthRedirectUrl()
์์ ํ์ํ request params์ 4๊ฐ์ง์ด๋ค.
client_id
,response_type
,redirect_uri
,scope
- Map<> ์ ์ด์ฉํด params๋ฅผ ๊ตฌ์ฑํ๋ค. ์ค์ํ ์ ๋ณด๋
application.properties
๋ฑ์ ๋ฐ๋ก ๋นผ๋๊ณ ๊บผ๋ด ์ฐ๊ธฐ!!
String paramsString = params.entrySet().stream()
.map(x->x.getKey()+"="+x.getValue())
.collect(Collectors.joining("&"));
- ๋๋ค์์ผ๋ก ๊ตฌ์ฑํ๋ ์ฝ๋๋ ์งง์์ง๊ณ ์ข๋ค.....!
Map.entrySet()
:Map์ Key-Value ์์ ๋ชจ์Stream()
: ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ฐ์ฐ์ ์ง์ํ๋๋ก ์ ์ฅ๋์ด ์๋ ์์๋ค์ ์ถ์ถํ์ฌ ๋ฐ๋ณต์ ์ธ ์ฒ๋ฆฌ๋ฅผ ๊ฐ๋ฅํ๊ฒ ํ๋ ๊ธฐ๋ฅ
requestAccessToken(String code)
์์ ํ์ํ request params์ 6๊ฐ์ง์ด๋ค.
code
,client_id
,grant_type
,response_type
,redirect_uri
,scope
,client_secret
redirect_uri
๋ ๋ชจ๋ ๊ตฌ๊ธ ํด๋ผ์ฐ๋์์ ๋ฏธ๋ฆฌ ์ ๋ ฅํด์ค uri๊ณผ ์ผ์นํด์ผ ํ๋ค.
restTemplate.postForEntity(String url, Object request, Class responseType)
- POST ์์ฒญ์ ๋ณด๋ด๊ณ ๊ฒฐ๊ณผ๋ก ResponseEntity๋ก ๋ฐํ
url
: ์์ฒญ์ ๋ณด๋ผ ์๋ฒ์ urlrequest
: POST ์์ฒญ ์ ํจ๊ป ๋ณด๋ผ ๊ฐ์ฒด- ์ฌ๊ธฐ์์ ๋๋ accessToken์ ์ป๊ธฐ ์ํด ํ์ํ params์ ๋ด์๋ค.
responseType
: ์๋ฒ์ ์๋ต์ ์ด๋ค ํํ๋ก ๋ฐ์์ง๋ฅผ ์ง์ ํ๋ Class ๊ฐ์ฒด- POST ์์ฒญ ์์ ํค๋๊ฐ๋ ์ถ๊ฐํด์ผํ๋ค๋ฉด?
HttpEntity
๋ฅผ ์์ฑํ์ฌ ์๋ ๋ณด๋ด์ผํ๋ ๊ฐ์ฒด์,HttpHeaders
๊ฐ์ฒด๋ฅผ ํจ๊ป ๋ด๋๋ค.
โ ์ด์ธ์๋ ์๋ฒ์ ์๋ต์ ๊ฐ์ฒด๋ก ๋ฐํํ๋PostforObject
๋ GET ์์ฒญ์ ์ํํ๋getForEntity
๋ฑ ๋ค์ํ๊ฒ ์กด์ฌํ๋ค.
LoginController.java
public class LoginController{
@Autowired
private final SocialOAuth socialOAuth;
private final HttpServletResponse response;
@GetMapping("/api/login")
public void loginGoogle() throws Exception{
response.sendRedirect(socialOAuth.getOAuthRedirectUrl());
}
@GetMapping("/login/oauth2/code/google")
public String requestToken(@RequestParam(name="code") String code) throws Exception{
return socialOAuth.requestAccessToken(code);
}
- ์ฌ์ค ์ฒ์์ ๊ฐ์ฅ ํค๋งธ๋ ๋ถ๋ถ์ด๋ค.. ๋ก๊ทธ์ธ ์์ฒญ์ ํ๊ณ ๋ฆฌ๋๋ ํธ๋๋ฉด ๊ทธ๊ฑธ ๋ค์
GetMapping
ํ๊ณ ๊ทธ ๋ค์์ ์ด๋ป๊ฒ ํด์ผํ์ง..? ๋ผ๋ ์๊ฐ์์ ๋งํ์ ์ง์ง ํ์ฐธ์ ํค๋งธ๋ค. ๊ณ์ controller์ ํ ๋ฉ์๋ ๋ด์์ code ๋ฐ์๊ฑธ ๋ค์ accessToken ์ ๋ฐ๊ธฐ ์ํด ์ฃผ์๋ฅผ ๋ฆฌ๋๋ ํธํ๊ณ .. ๋ณต์กํ๊ฒ ์๊ฐํ๋๋ฐ ๊ทธ๋ฅ ๋ฉ์๋๋ฅผ ๊ฐ๊ฐ ๋ค ๋๋๋ฉด ์๊ฐํ๊ธฐ๋ ํจ์ฌ ์ฝ๋ค!
๋ค์ ํ ์ผ
- ๊ตฌ๊ธ ๋ก๊ทธ์ธ์ ๊ธฐ๋ฐ์ผ๋ก ํ๋๋ก User ๋๋ฉ์ธ ์์
- User ๋๋ฉ์ธ๊ณผ ๊ด๊ณ๊ฐ ์๋ ์ฌ๋ฌ ๋๋ฉ์ธ ๋๋ฌ ์์ (...)
- ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ์์ ํด์ผ ํ์ง ์์๊น.