๐Ÿ”ฅ #18 ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์„œ๋น„์Šค ๊ตฌํ˜„ 2 - ์ž์›์„œ๋ฒ„ ์ ‘๊ทผ๊ณผ ๋ธ”๋กœ๊ทธ์„œ๋ฒ„์™€ ํ†ตํ•ฉํ•˜๊ธฐ

myeonjiยท2022๋…„ 2์›” 24์ผ
0

์ด์ „ ๊ฒŒ์‹œ๋ฌผ์—์„œ ์นด์นด์˜ค ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์•ก์„ธ์Šค ํ† ํฐ์„ ๋ฐ›์•˜๋‹ค. ์ฆ‰, ์ž์›์„œ๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ์–ป์€ ๊ฒƒ์ด๋‹ค.

์•ก์„ธ์Šค ํ† ํฐ์ด ์œ„์ฒ˜๋Ÿผ ์‘๋‹ต๋˜์—ˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ๋ธŒ๋ผ์šฐ์ €์— ๋ฟŒ๋ ค์ ธ ์žˆ๋Š” json ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ํž˜๋“ค๊ธฐ ๋•Œ๋ฌธ์— model์„ ํ•˜๋‚˜ ๋งŒ๋“ค์—ˆ๋‹ค.

< OAuthToken >

import lombok.Data;

@Data
public class OAuthToken {
    private String access_token;
    private String token_type;
    private String refresh_token;
    private int expires_in;
    private String scope;
    private int refresh_token_expires_in;
}

์•ก์„ธ์Šค ํ† ํฐ์œผ๋กœ ์นด์นด์˜ค ์ž์› ์„œ๋ฒ„์— ์š”์ฒญํ•˜๊ธฐ!
< UserController >

	.
	.
	.
    RestTemplate rt2 = new RestTemplate();

    // HttpHeader ๊ฐ์ฒด ์ƒ์„ฑ
    HttpHeaders headers2 = new HttpHeaders();
    headers2.add("Authorization", "Bearer " + oauthToken.getAccess_token());
    headers2.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); // key=value ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š” ๋ถ€๋ถ„

    // HttpHeader๋ฅผ ๊ฐ์ฒด์— ๋‹ด๊ธฐ -> ๋งŒ๋“  ์ด์œ  : ์•„๋ž˜์˜ exchange ํ•จ์ˆ˜์— HttpEntity๋ฅผ ๋„ฃ์–ด์•ผ ํ•ด์„œ..
    HttpEntity<MultiValueMap<String, String>> kakaoProfileRequest2 =
            new HttpEntity<>(headers2); // headers ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Entity

    // ์นด์นด์˜ค์—๊ฒŒ Http ์š”์ฒญํ•˜๊ธฐ (POST ๋ฐฉ์‹) -> response2๋ผ๋Š” ๋ณ€์ˆ˜์— ์‘๋‹ต์„ ๋ฐ›์Œ
    ResponseEntity<String> response2 = rt2.exchange(
            "https://kapi.kakao.com/v2/user/me",
            HttpMethod.POST,
            kakaoProfileRequest2,
            String.class
    );
    
   
    .
    .
    .
    return "์•ก์„ธ์Šค ํ† ํฐ์œผ๋กœ ํšŒ์›์ •๋ณด ์š”์ฒญ : ํšŒ์›์ •๋ณด ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต : " + response2.getBody();
        

jsonschema2pojo์—์„œ ์œ„์˜ json ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์‘๋‹ต ๋ฐ›๊ธฐ!

	.
	.
	.
    // kakaoProfile์— ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋‹ด๊ธฐ
    // json ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋ฐ”์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค.
    // ๊ฐ์ฒด(ํ˜„์žฌ๋Š” OAuthToken)์— ๋‹ด์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ : Gson, Json Simple, ObjectMapper
    ObjectMapper objectMapper2 = new ObjectMapper();

    KakaoProfile kakaoProfile = null;
    try {
        kakaoProfile = objectMapper2.readValue(response2.getBody(), KakaoProfile.class);
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

    // User ์˜ค๋ธŒ์ ํŠธ : id, username, password, email, role, createDate
    System.out.println("์นด์นด์˜ค ์•„์ด๋””(๋ฒˆํ˜ธ) : " + kakaoProfile.getId());
    System.out.println("์นด์นด์˜ค ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());

//    return "์นด์นด์˜ค ์ธ์ฆ ์™„๋ฃŒ : ์ฝ”๋“œ๊ฐ’ : " + code;
//    return "์นด์นด์˜ค ํ† ํฐ ์š”์ฒญ ์™„๋ฃŒ : ํ† ํฐ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต : " + response.getBody();
     return "์•ก์„ธ์Šค ํ† ํฐ์œผ๋กœ ํšŒ์›์ •๋ณด ์š”์ฒญ : ํšŒ์›์ •๋ณด ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต : " + response2.getBody();

์„ฑ๊ณต !!


์ด์ œ ์นด์นด์˜ค๋กœ ๋กœ๊ทธ์ธ ํ•œ ์œ ์ € ์ •๋ณด๋ฅผ ๋‚˜์˜ ํ”„๋กœ์ ํŠธ User ๊ฐ์ฒด๋กœ ์ •๋ณด๋ฅผ ๊ตฌ์„ฑํ•ด์•ผ ํ•œ๋‹ค. (ํ†ตํ•ฉ)

.
.
.
// User ์˜ค๋ธŒ์ ํŠธ : (id), username, password, email, (role), (createDate)
System.out.println("์นด์นด์˜ค ์•„์ด๋””(๋ฒˆํ˜ธ) : " + kakaoProfile.getId());
System.out.println("์นด์นด์˜ค ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());

System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ์œ ์ €๋„ค์ž„ : " + kakaoProfile.getKakao_account().getEmail() + "_" + kakaoProfile.getId());
System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());
UUID garbagePassword = UUID.randomUUID();
System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ํŒจ์Šค์›Œ๋“œ : " + garbagePassword);
.
.
.

.
.
.
// User ์˜ค๋ธŒ์ ํŠธ : (id), username, password, email, (role), (createDate)
System.out.println("์นด์นด์˜ค ์•„์ด๋””(๋ฒˆํ˜ธ) : " + kakaoProfile.getId());
System.out.println("์นด์นด์˜ค ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());

System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ์œ ์ €๋„ค์ž„ : " + kakaoProfile.getKakao_account().getEmail() + "_" + kakaoProfile.getId());
System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());
UUID garbagePassword = UUID.randomUUID();
System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ํŒจ์Šค์›Œ๋“œ : " + garbagePassword);

User user = User.builder()
        .username(kakaoProfile.getKakao_account().getEmail() + "_" + kakaoProfile.getId())
        .password(garbagePassword.toString())
        .email(kakaoProfile.getKakao_account().getEmail())
        .build();

userService.ํšŒ์›๊ฐ€์ž…(user);

return "ํšŒ์›๊ฐ€์ž… ์™„๋ฃŒ";
.
.
.

์—ฌ๊ธฐ์„œ ์ถ”๊ฐ€๋กœ,
userService.ํšŒ์›๊ฐ€์ž…(user) ์ „์— ์ด๋ฏธ ๊ฐ€์ž…์ด ๋˜์—ˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ์ฒดํฌ ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ด๋‹ค.


๊ฐ€์ž…/๋น„๊ฐ€์ž… ์ฒดํฌ

.
.
.
// User ์˜ค๋ธŒ์ ํŠธ : (id), username, password, email, (role), (createDate)
System.out.println("์นด์นด์˜ค ์•„์ด๋””(๋ฒˆํ˜ธ) : " + kakaoProfile.getId());
System.out.println("์นด์นด์˜ค ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());

System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ์œ ์ €๋„ค์ž„ : " + kakaoProfile.getKakao_account().getEmail() + "_" + kakaoProfile.getId());
System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());
UUID garbagePassword = UUID.randomUUID();
System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ํŒจ์Šค์›Œ๋“œ : " + garbagePassword);

User user = User.builder()
        .username(kakaoProfile.getKakao_account().getEmail() + "_" + kakaoProfile.getId())
        .password(garbagePassword.toString())
        .email(kakaoProfile.getKakao_account().getEmail())
        .build();

.
.
.
// ๊ฐ€์ž…์ž ํ˜น์€ ๋น„๊ฐ€์ž…์ž ๋ถ„๊ธฐ (์ด๋ฏธ ํšŒ์›์ธ์ง€ ์•„๋‹Œ์ง€ ์ฒดํฌ)
User originUser = userService.ํšŒ์›์ฐพ๊ธฐ(kakaoUser.getUsername());

// ๋น„๊ฐ€์ž…์ž(null)์ด๋ฉด, ํšŒ์›๊ฐ€์ž… ํ›„ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ
if (originUser.getUsername() == null) {
        System.out.println("๊ธฐ์กด ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค.");
        userService.ํšŒ์›๊ฐ€์ž…(kakaoUser);
        
// ๊ฐ€์ž…์ž์ด๋ฉด ํšŒ์›๊ฐ€์ž… ์—†์ด ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(kakaoUser.getUsername(), kakaoUser.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);

return "redirect:/";

์œ„์ฒ˜๋Ÿผ ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋Š”๋ฐ.. ๊ณ„์†ํ•ด์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค.

๊ทธ ์ด์œ ๋Š”,
๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ํŒจ์Šค์›Œ๋“œ๋ฅผ UUID๋กœ ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ธฐ์กด ํšŒ์›์ด ์•„๋‹ˆ๋ผ๋ฉด ์ƒ๊ด€์ด ์—†๊ฒ ์ง€๋งŒ ๊ธฐ์กด ํšŒ์›์ด๋ผ๋ฉด.. UUID๋กœ ๋งŒ๋“ค๋ฉด ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์–ด๋–ค ํŠน์ • ๊ฐ’์„ ๋งŒ๋“ค์–ด๋‚ด๊ฒŒ ๋˜๋Š”๋ฐ ์ด๊ฒŒ ๋งค๋ฒˆ ๋กœ๊ทธ์ธ ๋งˆ๋‹ค ๋ฐ”๋€Œ์–ด ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.
๊ฐ’์ด ๋งค๋ฒˆ ๋‹ฌ๋ผ์ง€๋‹ˆ๊นŒ ๋‹น์—ฐํžˆ ๋กœ๊ทธ์ธ์ด ์•ˆ๋œ๋‹ค!!

๋”ฐ๋ผ์„œ ์นด์นด์˜ค๋กœ ๋กœ๊ทธ์ธํ•˜๋Š” ๋ชจ๋“  ๋ธ”๋กœ๊ทธ ์„œ๋ฒ„ ํŒจ์Šค์›Œ๋“œ๋ฅผ ํ†ต์ผํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.
< application.yml >

yj:
  key: yj0118

key ์•ž์— ์ŠคํŽ˜์ด์Šค ๋‘ ์นธ! key:์™€ yj0118 ์•ž์— ์ŠคํŽ˜์ด์Šค ํ•œ ์นธ!!

โญ(์‹ค์ œ ํ”„๋กœ์ ํŠธ์˜€๋‹ค๋ฉด ์ด ๊ฐ’์€ ์ •๋ง ์ค‘์š”ํ•œ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ๋…ธ์ถœ๋˜๋ฉด ์•ˆ๋œ๋‹ค. ์ด key๊ฐ€ ๋…ธ์ถœ๋˜๋ฉด ๋ชจ๋“  OAuth ๋กœ๊ทธ์ธ์ด ๋…ธ์ถœ๋˜๊ฒŒ ๋œ๋‹ค.)

์ด์ œ, ์ด key๋ฅผ UserController์—๋‹ค๊ฐ€ ์ฃผ์ž…ํ•œ๋‹ค.

< UserController >์— ์ฃผ์ž…

@Value("${yj.key}")
private String yjKey;

< UserService >

	@Transactional(readOnly = true) // select๋งŒ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— readonly
    public User ํšŒ์›์ฐพ๊ธฐ(String username) {
        User user = userRepository.findByUsername(username).orElseGet(()->{ // .orElseGet : ํšŒ์›์„ ์ฐพ์•˜๋Š”๋ฐ ์—†์œผ๋ฉด, ๋นˆ ๊ฐ์ฒด ๋ฆฌํ„ด
            return new User();
        });
        return user;
    }

< UserController > ์ตœ์ข…

.
.
.
	@GetMapping("/auth/kakao/callback")
    public String kakaoCallback(String code) { // @ResponseBody : Data๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ์ปจํŠธ๋กค๋Ÿฌ ํ•จ์ˆ˜

        // ์นด์นด์˜ค API ์„œ๋ฒ„์—๊ฒŒ POST ๋ฐฉ์‹์œผ๋กœ key=value ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญ
        // ์š”์ฒญ ๋ฐฉ๋ฒ• -> ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ  : HttpsURLConnection, Retrofit2(์ฃผ๋กœ ์•ˆ๋“œ๋กœ์ด๋“œ), OkHttp, RestTemplate
        RestTemplate rt = new RestTemplate();

        // HttpHeader ๊ฐ์ฒด ์ƒ์„ฑ
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); // key=value ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š” ๋ถ€๋ถ„

        // HttpBody ๊ฐ์ฒด ์ƒ์„ฑ
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("grant_type", "authorization_code");
        params.add("client_id", "d5526e3b2a4169a3d9f2b7a6f9a12cdf");
        params.add("redirect_uri", "http://localhost:8000/auth/kakao/callback");
        params.add("code", code);

        // HttpHeader์™€ HttpBody๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ์ฒด์— ๋‹ด๊ธฐ -> ๋งŒ๋“  ์ด์œ  : ์•„๋ž˜์˜ exchange ํ•จ์ˆ˜์— HttpEntity๋ฅผ ๋„ฃ์–ด์•ผ ํ•ด์„œ..
        HttpEntity<MultiValueMap<String, String>> kakaoTokenRequest =
                new HttpEntity<>(params, headers); // body ๋ฐ์ดํ„ฐ์™€ headers ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Entity

        // ์นด์นด์˜ค์—๊ฒŒ Http ์š”์ฒญํ•˜๊ธฐ (POST ๋ฐฉ์‹) -> response๋ผ๋Š” ๋ณ€์ˆ˜์— ์‘๋‹ต์„ ๋ฐ›์Œ
        ResponseEntity<String> response = rt.exchange(
                "https://kauth.kakao.com/oauth/token",
                HttpMethod.POST,
                kakaoTokenRequest,
                String.class
        );

        // oauthToken์— ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋‹ด๊ธฐ
        // json ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋ฐ”์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค.
        // ๊ฐ์ฒด(ํ˜„์žฌ๋Š” OAuthToken)์— ๋‹ด์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ : Gson, Json Simple, ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper();

        OAuthToken oauthToken = null;
        try {
            oauthToken = objectMapper.readValue(response.getBody(), OAuthToken.class);
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        System.out.println("์นด์นด์˜ค ์—‘์„ธ์Šค ํ† ํฐ : " + oauthToken.getAccess_token());

        // ------------------------------------------------------------------------------------

        RestTemplate rt2 = new RestTemplate();

        // HttpHeader ๊ฐ์ฒด ์ƒ์„ฑ
        HttpHeaders headers2 = new HttpHeaders();
        headers2.add("Authorization", "Bearer " + oauthToken.getAccess_token());
        headers2.add("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); // key=value ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š” ๋ถ€๋ถ„

        // HttpHeader๋ฅผ ๊ฐ์ฒด์— ๋‹ด๊ธฐ -> ๋งŒ๋“  ์ด์œ  : ์•„๋ž˜์˜ exchange ํ•จ์ˆ˜์— HttpEntity๋ฅผ ๋„ฃ์–ด์•ผ ํ•ด์„œ..
        HttpEntity<MultiValueMap<String, String>> kakaoProfileRequest2 =
                new HttpEntity<>(headers2); // headers ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Entity

        // ์นด์นด์˜ค์—๊ฒŒ Http ์š”์ฒญํ•˜๊ธฐ (POST ๋ฐฉ์‹) -> response2๋ผ๋Š” ๋ณ€์ˆ˜์— ์‘๋‹ต์„ ๋ฐ›์Œ
        ResponseEntity<String> response2 = rt2.exchange(
                "https://kapi.kakao.com/v2/user/me",
                HttpMethod.POST,
                kakaoProfileRequest2,
                String.class
        );

        // kakaoProfile์— ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋‹ด๊ธฐ
        // json ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋ฐ”์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค.
        // ๊ฐ์ฒด(ํ˜„์žฌ๋Š” OAuthToken)์— ๋‹ด์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ : Gson, Json Simple, ObjectMapper
        ObjectMapper objectMapper2 = new ObjectMapper();

        KakaoProfile kakaoProfile = null;
        try {
            kakaoProfile = objectMapper2.readValue(response2.getBody(), KakaoProfile.class);
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        // User ์˜ค๋ธŒ์ ํŠธ : (id), username, password, email, (role), (createDate)
        System.out.println("์นด์นด์˜ค ์•„์ด๋””(๋ฒˆํ˜ธ) : " + kakaoProfile.getId());
        System.out.println("์นด์นด์˜ค ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());

        System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ์œ ์ €๋„ค์ž„ : " + kakaoProfile.getKakao_account().getEmail() + "_" + kakaoProfile.getId());
        System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ์ด๋ฉ”์ผ : " + kakaoProfile.getKakao_account().getEmail());
        UUID garbagePassword = UUID.randomUUID();
        // UUID๋ž€ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์–ด๋–ค ํŠน์ • ๊ฐ’์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
        System.out.println("๋ธ”๋กœ๊ทธ์„œ๋ฒ„ ํŒจ์Šค์›Œ๋“œ : " + yjKey);

        User kakaoUser = User.builder()
                .username(kakaoProfile.getKakao_account().getEmail() + "_" + kakaoProfile.getId())
                .password(yjKey)
                .email(kakaoProfile.getKakao_account().getEmail())
                .build();

        // ๊ฐ€์ž…์ž ํ˜น์€ ๋น„๊ฐ€์ž…์ž ๋ถ„๊ธฐ (์ด๋ฏธ ํšŒ์›์ธ์ง€ ์•„๋‹Œ์ง€ ์ฒดํฌ)
        User originUser = userService.ํšŒ์›์ฐพ๊ธฐ(kakaoUser.getUsername());

        // ๋น„๊ฐ€์ž…์ž(null)์ด๋ฉด, ํšŒ์›๊ฐ€์ž… ํ›„ ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ
        if (originUser.getUsername() == null) {
            System.out.println("๊ธฐ์กด ํšŒ์›์ด ์•„๋‹ˆ๊ธฐ์— ์ž๋™์œผ๋กœ ํšŒ์›๊ฐ€์ž…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.");
            userService.ํšŒ์›๊ฐ€์ž…(kakaoUser);
        }

        System.out.println("์ž๋™ ๋กœ๊ทธ์ธ์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.");

        // ๊ฐ€์ž…์ž์ด๋ฉด ํšŒ์›๊ฐ€์ž… ์—†์ด ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ
        Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(kakaoUser.getUsername(), yjKey));
        SecurityContextHolder.getContext().setAuthentication(authentication);

//        return "์นด์นด์˜ค ์ธ์ฆ ์™„๋ฃŒ : ์ฝ”๋“œ๊ฐ’ : " + code;
//        return "์นด์นด์˜ค ํ† ํฐ ์š”์ฒญ ์™„๋ฃŒ : ํ† ํฐ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต : " + response.getBody();
//        return "์•ก์„ธ์Šค ํ† ํฐ์œผ๋กœ ํšŒ์›์ •๋ณด ์š”์ฒญ : ํšŒ์›์ •๋ณด ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต : " + response2.getBody();
//        return "ํšŒ์›๊ฐ€์ž… ์™„๋ฃŒ";
        return "redirect:/";
    }

์ฒซ ์นด์นด์˜ค ๋กœ๊ทธ์ธ์—์„œ๋Š” ํšŒ์›๊ฐ€์ž…๊นŒ์ง€ ์ด๋ฃจ์–ด์ ธ DB์— ์ž˜ ๋“ค์–ด๊ฐ€์ง€๊ณ ,
๊ทธ ์ดํ›„์—๋Š” ์ž๋™์œผ๋กœ ๋กœ๊ทธ์ธ์ด ๋œ๋‹ค!

์—ฌ๊ธฐ์„œ password๋Š” yjkey๊ฐ€ ์•”ํ˜ธํ™” ๋˜์–ด ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.


OAuth ๋กœ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ํšŒ์›์ •๋ณด ์ˆ˜์ •์„ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด,
์ฆ‰ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ˆ˜์ •์„ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด
๊ธฐ์กด์— yjkey(yj0118)๋กœ ๋„ฃ์–ด๋†จ๋˜ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ๋˜์–ด ๋‹ค์‹œ OAuth ๋กœ๊ทธ์ธ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์งˆ ๊ฒƒ์ด๋‹ค.

โ— ์นด์นด์˜ค๋กœ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๋Š” ํšŒ์›์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋„๋ก ๋งŒ๋“ค๊ธฐ!!

-> User ๊ฐ์ฒด์— oauth ํ•„๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ, ์ด ํšŒ์›์ด ์นด์นด์˜ค๋กœ ๋กœ๊ทธ์ธํ•œ ํšŒ์›์ธ์ง€ ์ผ๋ฐ˜ ๋กœ๊ทธ์ธ์œผ๋กœ ๋กœ๊ทธ์ธ์„ ํ•œ ํšŒ์›์ธ์ง€ ๊ตฌ๋ถ„ํ•ด์•ผ ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์นด์นด์˜ค๋กœ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” oauth ๋ณ€์ˆ˜์— "kakao" ๋ผ๋Š” ๊ฐ’์„ ์ค€๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ด๋ ‡๊ฒŒ ๊ตฌ๋ถ„์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค!

ํšŒ์›์ˆ˜์ •ํผ์—์„œ, oauth ๊ฐ’์ด ๋น„์–ด์žˆ์„ ๋•Œ๋งŒ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
์ฆ‰, oauth ๊ฐ’์ด ๋น„์–ด์žˆ๋‹ค๋Š”๊ฑด ์นด์นด์˜ค ๋กœ๊ทธ์ธ์ด ์•„๋‹Œ ์ผ๋ฐ˜ ์‚ฌ์šฉ์ž๋ผ๋Š” ๊ฒƒ!!

< UserService >

UserService์—์„œ๋„ url ๋กœ ๊ณต๊ฒฉ ๋‹นํ•  ๊ฒƒ์„ ๋Œ€๋น„ํ•ด ๋ง‰์•„์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค!!

๊ฒฐ๊ณผ

- ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž

์–ด๋–ค ์ •๋ณด๋„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.

- ์ผ๋ฐ˜ ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž

password์™€ email ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค!

profile
๐Ÿ“š

0๊ฐœ์˜ ๋Œ“๊ธ€