Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ out/
.vscode/

application-oauth.properties
application-aws.properties
application-aws.properties
application-secret.properties
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ repositories {

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
implementation 'com.google.code.gson:gson:2.8.7'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
// compileOnly 'org.springframework.session:spring-session-jdbc'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package HYLikeLion.gitppo.gitppoProject.config.auth;
package HYLikeLion.gitppo.gitppoProject.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package HYLikeLion.gitppo.gitppoProject.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@RequiredArgsConstructor
@Configuration
public class WebConfig implements WebMvcConfigurer {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*");
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package HYLikeLion.gitppo.gitppoProject.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -23,42 +24,36 @@
@RestController
@RequiredArgsConstructor
public class AuthApiController {

// private final String REDIRECT_URL = "http://localhost:30/00";
private final String TOKEN_REQUEST_URL = "https://github.com/login/oauth/access_token";
private final String PROFILE_REQUEST_URL = "https://api.github.com/user";
private final String REDIRECT_URL = "http://gitppo.github.io/Frontend/";
private final UserService userService;

@Value("${github.id}")
private String id;

@Value("${github.secret}")
private String secret;

@PostMapping("/auth")
private User getOAuthToken(@RequestParam String code) throws JsonProcessingException {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(TOKEN_REQUEST_URL,
HttpMethod.POST,
getCodeRequestHttpEntity(code),
String.class);

final String tokenRequestUrl = "https://github.com/login/oauth/access_token";
final String profileRequestUrl = "https://api.github.com/user";
ObjectMapper objectMapper = new ObjectMapper();
OAuthToken oAuthToken = objectMapper.readValue(response.getBody(), OAuthToken.class);
System.out.println(oAuthToken);

ResponseEntity<String> profileResponse = restTemplate.exchange(
PROFILE_REQUEST_URL,
HttpMethod.GET,
getProfileRequestEntity(oAuthToken),
String.class
);
ResponseEntity<String> response = getResponse(getCodeRequestHttpEntity(code), tokenRequestUrl, HttpMethod.POST);
OAuthToken oAuthToken = objectMapper.readValue(response.getBody(), OAuthToken.class);

ResponseEntity<String> profileResponse = getResponse(getProfileRequestEntity(oAuthToken), profileRequestUrl,
HttpMethod.GET);
JsonNode root = objectMapper.readTree(profileResponse.getBody());

return userService.saveOrUpdate(root);
}

private HttpEntity<MultiValueMap<String, String>> getCodeRequestHttpEntity(String code) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
// 주의!
params.add("client_id", "93ad6f9f68a2f8fbd473");
params.add("client_secret", "b548d698a91c1057736919d1fc12555f1443b24c");
String REDIRECT_URL = "https://gitppo.github.io/Frontend/";

params.add("client_id", id);
params.add("client_secret", secret);
params.add("code", code);
params.add("redirect_url", REDIRECT_URL);

Expand All @@ -67,11 +62,20 @@ private HttpEntity<MultiValueMap<String, String>> getCodeRequestHttpEntity(Strin
return new HttpEntity<>(params, headers);
}

private HttpEntity<MultiValueMap<String, String>> getProfileRequestEntity(
OAuthToken oAuthToken) {
private HttpEntity<MultiValueMap<String, String>> getProfileRequestEntity(OAuthToken oAuthToken) {
HttpHeaders infoRequestHeaders = new HttpHeaders();
infoRequestHeaders.add("Authorization", "token " + oAuthToken.getAccessToken());
return new HttpEntity<>(infoRequestHeaders);
}

private ResponseEntity<String> getResponse(HttpEntity<MultiValueMap<String, String>> requestEntity, String url,
HttpMethod httpMethod) {
RestTemplate restTemplate = new RestTemplate();
return restTemplate.exchange(
url,
httpMethod,
requestEntity,
String.class);
}

}
Loading