private void addConnection(
     Connection<?> connection, ConnectionFactory<?> connectionFactory, WebRequest request) {
   try {
     connectionRepository.addConnection(connection);
     postConnect(connectionFactory, connection, request);
   } catch (Exception e) {
     sessionStrategy.setAttribute(request, DUPLICATE_CONNECTION_ATTRIBUTE, e);
   }
 }
  @ApiOperation(
      value = "SNS 기반 로그인",
      produces = "application/json",
      response = EmptyJsonResponse.class)
  @RequestMapping(value = "/login/social/{providerId}", method = RequestMethod.POST)
  public EmptyJsonResponse loginSocialUser(
      @PathVariable String providerId,
      @Valid @RequestBody LoginSocialUserForm form,
      NativeWebRequest request) {

    CommonConst.ACCOUNT_TYPE convertProviderId =
        CommonConst.ACCOUNT_TYPE.valueOf(providerId.toUpperCase());

    AccessGrant accessGrant = new AccessGrant(form.getAccessToken());
    Connection<?> connection = null;

    switch (convertProviderId) {
      case FACEBOOK:
        connection = facebookConnectionFactory.createConnection(accessGrant);
        break;
      case DAUM:
        connection = daumConnectionFactory.createConnection(accessGrant);
        break;
    }

    assert connection != null;
    ConnectionKey connectionKey = connection.getKey();

    Set<String> userIds =
        usersConnectionRepository.findUserIdsConnectedTo(
            providerId,
            new HashSet<>(Collections.singletonList(connectionKey.getProviderUserId())));
    User existUser =
        userService.findOneByProviderIdAndProviderUserId(
            convertProviderId, connectionKey.getProviderUserId());

    // 로그인 처리.
    if (!userIds.isEmpty()) {
      userService.signInSocialUser(existUser);

      return EmptyJsonResponse.newInstance();
    }

    // SNS 신규 가입.
    ProviderSignInAttempt signInAttempt = new ProviderSignInAttempt(connection);
    sessionStrategy.setAttribute(request, ProviderSignInAttempt.SESSION_ATTRIBUTE, signInAttempt);

    throw new ServiceException(ServiceError.NOT_REGISTER_WITH_SNS);
  }
 @Override
 @RequestMapping(value = "/{providerId}", method = RequestMethod.GET, params = "oauth_token")
 public RedirectView oauth1Callback(@PathVariable String providerId, NativeWebRequest request) {
   connectSupport = new ConnectSupport(sessionStrategy);
   try {
     OAuth1ConnectionFactory<?> connectionFactory =
         (OAuth1ConnectionFactory<?>) connectionFactoryLocator.getConnectionFactory(providerId);
     Connection<?> connection = connectSupport.completeConnection(connectionFactory, request);
     Serializable userId = null;
     switch (providerId) {
       case "google":
         google = (Google) connection.getApi();
         userId = ApiBindingUtils.getId(google);
         break;
       case "facebook":
         facebook = (Facebook) connection.getApi();
         userId = ApiBindingUtils.getId(facebook);
         break;
       case "twitter":
         twitter = (Twitter) connection.getApi();
         userId = ApiBindingUtils.getId(twitter);
         break;
       default:
         break;
     }
     SecurityContextHolder.getContext()
         .setAuthentication(new SocialAuthenticationToken(connection, userId, null, null));
     addConnection(connection, connectionFactory, request);
   } catch (Exception e) {
     sessionStrategy.setAttribute(request, PROVIDER_ERROR_ATTRIBUTE, e);
     logger.warn(
         "Exception while handling OAuth1 callback ("
             + e.getMessage()
             + "). Redirecting to "
             + providerId
             + " connection status page.");
   }
   return connectionStatusRedirect(providerId, request);
 }