Skip to content

Commit

Permalink
Merge pull request #22 from lotteon2/dev-auth-refactoring
Browse files Browse the repository at this point in the history
add: add the cors at config
  • Loading branch information
indl1670 authored Dec 14, 2023
2 parents 8a2448f + dcb81cf commit 096bd36
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 123 deletions.
Binary file added src/main/java/com/bit/lot/flower/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand Down Expand Up @@ -39,6 +40,14 @@ public void addInterceptors(InterceptorRegistry registry) {
).addPathPatterns("/**/logout");
}

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowCredentials(true);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

public class JsonBinderUtil {

public static HttpServletResponse setResponseWithJson(HttpServletResponse response, int status,
public static HttpServletResponse
setResponseWithJson(HttpServletResponse response, int status,
Object type) throws IOException {

response.setContentType("application/json");
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/bit/lot/flower/auth/oauth/OauthController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.bit.lot.flower.auth.oauth;

import com.bit.lot.flower.auth.common.valueobject.AuthenticationProvider;
import com.bit.lot.flower.auth.oauth.facade.OauthLoginAccessTokenRequestFacade;
import com.bit.lot.flower.auth.oauth.facade.OauthUserMeInfoRequestFacade;
import com.bit.lot.flower.auth.social.dto.command.SocialLoginRequestCommand;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RequiredArgsConstructor
@RestController
public class OauthController {

private final OauthLoginAccessTokenRequestFacade oauthLoginRequestFacade;
private final OauthUserMeInfoRequestFacade userMeInfoRequestFacade;

@GetMapping("/login/oauth2/{provider}")
public ResponseEntity<SocialLoginRequestCommand> requestSocialInfo(@RequestParam String code,
@PathVariable AuthenticationProvider provider) throws JsonProcessingException {
String authorizationCode = oauthLoginRequestFacade.request(provider, code);
return ResponseEntity.ok(userMeInfoRequestFacade.getUserInfo(authorizationCode, provider));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bit.lot.flower.auth.oauth.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -8,11 +9,6 @@
@AllArgsConstructor
@NoArgsConstructor
public class LoginResponseDto {
private String tokenType;
@JsonProperty("access_token")
private String accessToken;
private String idToken;
private String refreshToken;
private int expiresIn;
private int refreshTokenExpiresIn;
private String scope;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bit.lot.flower.auth.oauth.facade;

import com.bit.lot.flower.auth.common.valueobject.AuthenticationProvider;
import com.bit.lot.flower.auth.oauth.util.access.GetKakaoAccessKeyHttpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class OauthLoginAccessTokenRequestFacade {

private final GetKakaoAccessKeyHttpUtil getKakaoAccessKeyHttpUtil;

public String request(AuthenticationProvider provider, String code) {
if (provider.equals(AuthenticationProvider.kakao)) {
return getKakaoAccessKeyHttpUtil.getAccessToken(code);
} else {
throw new IllegalArgumentException("존재 하지 않는 인증 제공k자입니다.");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.bit.lot.flower.auth.oauth.facade;

import com.bit.lot.flower.auth.common.valueobject.AuthId;
import com.bit.lot.flower.auth.common.valueobject.AuthenticationProvider;
import com.bit.lot.flower.auth.oauth.dto.response.LoginResponseDto;
import com.bit.lot.flower.auth.oauth.http.util.RequestUserMeRestTemplateUtil;
import com.bit.lot.flower.auth.social.dto.command.SocialLoginRequestCommand;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class OauthUserMeInfoRequestFacade {

private final RequestUserMeRestTemplateUtil requestUserMeRestTemplateUtil;

@Value("${spring.security.oauth2.client.provider.kakao.user-info-uri}")
private String kakaoUserMeURL;

public SocialLoginRequestCommand getUserInfo(String code, AuthenticationProvider provider)
throws JsonProcessingException {
if (provider.equals(AuthenticationProvider.kakao)) {
return getKakaoUserData(requestUserMeRestTemplateUtil.getUserInfo(code, kakaoUserMeURL));
}
throw new IllegalArgumentException("존재 하지 않는 인증 제공자입니다.");
}

private SocialLoginRequestCommand getKakaoUserData(String data) throws JsonProcessingException {

ObjectMapper mapper = new ObjectMapper();
HashMap<String, Object> resultMap = mapper.readValue(data, HashMap.class);

HashMap<String, Object> properties = (HashMap<String, Object>) resultMap.get("properties");
HashMap<String, Object> kakaoAccount = (HashMap<String, Object>) resultMap.get("kakao_account");

Long id = Long.valueOf(String.valueOf(resultMap.get("id")));
String nickname = (String) properties.get("nickname");
String email = (String) kakaoAccount.get("email");
String phoneNumber = (String) kakaoAccount.get("phone_number");


return new SocialLoginRequestCommand(new AuthId(id),email,phoneNumber,nickname);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.bit.lot.flower.auth.oauth.http.util;

import com.bit.lot.flower.auth.oauth.dto.response.LoginResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@RequiredArgsConstructor
@Component
public class RequestRestTemplateAccessTokenUtil {

private final RestTemplate restTemplate;

public String request(String code, String clientId, String redirectURI, String requestURL) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-type", "application/x-www-form-urlencoded; charset=utf-8");

MultiValueMap<String, String> body = new LinkedMultiValueMap<>();

body.add("grant_type", "authorization_code");
body.add("client_id", clientId);
body.add("redirect_uri", redirectURI);
body.add("code", code);

LoginResponseDto loginResponseDto = restTemplate.postForObject(
requestURL,
new HttpEntity<>(body, headers),
LoginResponseDto.class);

return loginResponseDto.getAccessToken();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.bit.lot.flower.auth.oauth.http.util;


import com.bit.lot.flower.auth.oauth.dto.response.LoginResponseDto;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@RequiredArgsConstructor
@Component
public class RequestUserMeRestTemplateUtil {

private final RestTemplate restTemplate;

public String getUserInfo(String accessCode,String userMeURL) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
headers.add("Authorization", "Bearer " + accessCode);

ResponseEntity<String> response =
restTemplate.exchange(userMeURL,
HttpMethod.GET,
new HttpEntity<>(null, headers),
String.class);

return response.getBody();
}




}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.bit.lot.flower.auth.oauth.util.access;

import com.bit.lot.flower.auth.oauth.http.util.RequestRestTemplateAccessTokenUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@RequiredArgsConstructor
@Component
public class GetKakaoAccessKeyHttpUtil {

private final RequestRestTemplateAccessTokenUtil requestRestTemplateAccessTokenUtil;

private final String requestURI = "https://kauth.kakao.com/oauth/token";
@Value("${spring.security.oauth2.client.registration.kakao.client-id}")
private String clientId;
private String redirectURI = "http://localhost:3000/login/oauth/";

public String getAccessToken(String code) {
log.info("redirectURL: " + redirectURI);
return requestRestTemplateAccessTokenUtil.request(code, clientId, redirectURI, requestURI);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.bit.lot.flower.auth.social.dto.command;

import com.bit.lot.flower.auth.common.valueobject.AuthId;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -11,7 +13,9 @@
@AllArgsConstructor
@NoArgsConstructor
@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
public class SocialLoginRequestCommand {

@NotNull
private AuthId socialId;
@NotNull
Expand Down
Loading

0 comments on commit 096bd36

Please sign in to comment.