Skip to content

Commit

Permalink
Merge pull request #112 from Developer-Wikis/feat/githubLogin
Browse files Browse the repository at this point in the history
Feat/GitHub login
  • Loading branch information
jhdl0157 authored Dec 1, 2022
2 parents 714468a + c8eb02e commit 8782b78
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,40 @@ public class OauthController {

@GetMapping()
public String Code(@RequestParam(value = "code")String code) throws JsonProcessingException {
//순서
// 1.코드와 리다이렉트 주소가 온다.
// 2.받은 정보로 토큰을 발급 받는다.
// 3. 토큰으로 사용자 정보를 요청한다.
System.out.println("코드 값임당 "+code);
String GOOGLE_TOKEN_REQUEST_URL="https://github.com/login/oauth/access_token";
RestTemplate restTemplate=new RestTemplate();
Map<String, Object> params = new HashMap<>();
// create headers
HttpHeaders headers1 = new HttpHeaders();
// set `content-type` header
headers1.setContentType(MediaType.APPLICATION_JSON);
// set `accept` header
headers1.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
params.put("code", code);
params.put("client_id", GITHUB_CLIENT_ID);
params.put("client_secret", GITHUB_CLIENT_SECRET);
params.put("redirect_uri", "http://localhost:8080/api/v1/oauth");
params.put("scope","user");
ResponseEntity<String> responseEntity;
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(params, headers1);
try {
responseEntity=restTemplate.postForEntity(GOOGLE_TOKEN_REQUEST_URL,params,String.class);
responseEntity=restTemplate.postForEntity(GOOGLE_TOKEN_REQUEST_URL,entity,String.class);
System.out.println("엑세스 토큰 임당 "+responseEntity.getBody());
}catch (RestClientException e){
e.printStackTrace();
throw new BadRequestException(String.format("인가코드로 구글의 AccessToken을 발급하지 못했습니다. code : %s, redirectUrl : %s, 오류 내용 : %s",code,"redirectUrl",e.getMessage()));
}
String[] resList=responseEntity.getBody().split("&");
String[] token=resList[0].split("=");
System.out.println("토큰의 값은 : "+token[1]);
GitHubOauthToken tokenTest=getAccessToken(responseEntity);
System.out.println("토큰 테스트 값 :"+tokenTest.getAccess_token());
String GOOGLE_USERINFO_REQUEST_URL="https://api.github.com/user";
//header에 accessToken을 담는다.
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization","Bearer "+token[1]);
headers.add("Authorization","Bearer "+tokenTest.getAccess_token());

//HttpEntity를 하나 생성해 헤더를 담아서 restTemplate으로 구글과 통신하게 된다.
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ResponseEntity<Page<CommentDto>> getCommentList(@AuthenticationPrincipal
return ResponseEntity.ok(list);
}

@GetMapping("/profile/default/{userId}")
@PutMapping("/profile/default/{userId}")
public ResponseEntity<Boolean> changeDefaultProfile(@AuthenticationPrincipal User currentUser,
@PathVariable(name = "userId")Long userId){
if(!currentUser.getId().equals(userId)) throw new BadRequestException("Not Match Userid");
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ spring:
hibernate.format_sql: true
hibernate:
default_batch_fetch_size: 1000
servlet:
multipart:
maxFileSize: 5MB
maxRequestSize: 10MB
datasource:
driver-class-name: org.mariadb.jdbc.Driver
url: ENC(aDY7D1okxkccvPE9kZk6XZmpxK07cD4poaCeU3DLsJOfD1rCsB5FzvDEN8Y1bQePmSCGgAvL8OM=)
Expand Down

0 comments on commit 8782b78

Please sign in to comment.