<aside> 📎 ‣

</aside>

🎥 시연 영상


hotsix trello

이것도 했습니다!!

💬 구글 소셜 로그인


  1. 필드 형식에 맞춰서 api를 보내줍니다.

    private String getToken(String code) throws JsonProcessingException {
    
            Map<String, String> params = new HashMap<>();
    
            **params.put("code", code);
            params.put("client_id", GOOGLE_CLIENT_ID);
            params.put("client_secret", GOOGLE_CLIENT_SECRET);
            params.put("redirect_uri", "<http://localhost:8080/users/login/google/callback>");
            params.put("grant_type", grantType);**
    
            **ResponseEntity<String> responseEntity = restTemplate.postForEntity(GOOGLE_TOKEN_URL, params, String.class);**
    
            if (responseEntity.getStatusCode() == HttpStatus.OK) {
                // HTTP 응답 (JSON) -> 액세스 토큰 파싱
                JsonNode jsonNode = new ObjectMapper().readTree(responseEntity.getBody());
                log.info("jwt" + jsonNode.get("id_token").asText());
                return jsonNode.get("id_token").asText();
            }
            return null;
        }
    
  2. 응답 받은 값에서 Id_token을 찾아 반환해줍니다.

    1. Id_token은 JWT이며, Base64 형태로 인코딩된 JSON 객체입니다. 이 안에 로그인한 구글 사용자의 정보가 들어 있습니다.
  3. Id_token을 디코딩해서 JSON 객체 정보를 확인합니다.

    private GoogleUserInfoDto getGoogleoUserInfo(String infoToken) throws JsonProcessingException {
    
            String[] chunks = infoToken.split("\\\\.");
            Base64.Decoder decoder = Base64.getUrlDecoder();
            String payload = new String(decoder.decode(chunks[1]));
    
            log.info("decoded string: " + payload);
    
            JsonNode jsonNode = new ObjectMapper().readTree(payload);
            String id = jsonNode.get("sub").asText();
            String email = jsonNode.get("email").asText();
            String name = jsonNode.get("name").asText();
    
            return new GoogleUserInfoDto(id, name, email);
        }
    
  4. JSON 객체에서 sub, email, name을 찾아 DB에 저장해줍니다.

    private User registerGoogleUserIfNeeded(GoogleUserInfoDto googleUserInfo) {
            // DB 에 중복된 google Id 가 있는지 확인
            String googleId = googleUserInfo.getId();
            User googleUser = userRepository.findByGoogleId(googleId).orElse(null);
    
            if (googleUser == null) {
                // google 사용자 email 동일한 email 가진 회원이 있는지 확인
                String googleEmail = googleUserInfo.getEmail();
                User sameEmailUser = userRepository.findByEmail(googleEmail).orElse(null);
                if (sameEmailUser != null) {
                    googleUser = sameEmailUser;
                    // 기존 회원정보에 google Id 추가
                    googleUser = googleUser.googleIdUpdate(googleId);
                } else {
                    // 신규 회원가입
                    // password: random UUID
                    String password = UUID.randomUUID().toString();
                    String encodedPassword = passwordEncoder.encode(password);
    
                    // email: google email
                    String **email** = googleUserInfo.getEmail();
                    **googleUser = new User(googleUserInfo.getNickname(), encodedPassword, email, UserRoleEnum.USER, null, googleId);**
                }
    
                userRepository.save(googleUser);
            }
            return googleUser;
        }
    

🔔 알림 기능


  1. Spring Event를 사용하여 서비스 간의 강한 의존성을 줄이고자 하였습니다.

  2. 하지만, Spring Event는 기본적으로 동기 방식으로 동작합니다.

    1. 만약, 이벤트 혹은 이벤트 전에 실행되는 프로세스가 처리 시간이 오래 걸린다면, 이벤트를 처리하는데 오랜 시간이 걸리게 되는 문제가 발생합니다.
    2. 그래서 이벤트와 동시에 비동기 처리 방식을 채택했습니다.

    Untitled