Skip to content

Commit

Permalink
KNU-HAEDAL#1 feat: OAuthController 및 서비스 제작
Browse files Browse the repository at this point in the history
  • Loading branch information
momnpa333 committed Apr 5, 2024
1 parent a943d80 commit 4241c23
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package team.haedal.gifticionfunding.auth.api;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team.haedal.gifticionfunding.auth.service.OAuthService;
import team.haedal.gifticionfunding.auth.service.SecurityService;
import team.haedal.gifticionfunding.service.user.UserService;

@RestController
@RequiredArgsConstructor
public class OAuthController {
private final OAuthService oAuthService;
private final SecurityService securityService;
private final UserService userService;

@GetMapping("/oauth2")
public ResponseEntity<Void> socialLogin(@RequestParam("code") String code){
String email= oAuthService.getEmail(code);
return ResponseEntity
.status(HttpStatus.OK)
.build();
}

}
15 changes: 15 additions & 0 deletions src/main/java/team/haedal/gifticionfunding/auth/dto/TokenDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package team.haedal.gifticionfunding.auth.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@Builder
@RequiredArgsConstructor
public class TokenDto {
private final String grantType;
private final String accessToken;
private final String refreshToken;
private final Long accessTokenExpiresIn;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package team.haedal.gifticionfunding.auth.service;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
@RequiredArgsConstructor
@PropertySource("classpath:application.yml")
public class OAuthService {
@Value("${kakao.client-id}")
private String CLIENT_ID;
@Value("${kakao.redirect-uri}")
private String REDIRECTION_URI;
@Value("${kakao.token-uri}")
private String TOKEN_URI;

@Value("${kakao.user-info-uri}")
private String USER_INFO_URI;

private final RestTemplate restTemplate = new RestTemplate();


public String getEmail(String code) {
String accessToken = getAccessToken(code);

HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + accessToken);
HttpEntity entity = new HttpEntity(headers);

String email = restTemplate.exchange(USER_INFO_URI,HttpMethod.GET, entity, JsonNode.class).getBody().get("email").asText();
return email;
}

public String getAccessToken(String code) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "authorization_code");
params.add("client_id", CLIENT_ID);
params.add("redirect_uri", REDIRECTION_URI);
params.add("code", code);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

HttpEntity entity=new HttpEntity(params, headers);

ResponseEntity<JsonNode> responseNode=restTemplate.exchange(TOKEN_URI, HttpMethod.POST,entity,JsonNode.class);
JsonNode accessTokenNode=responseNode.getBody();
return accessTokenNode.get("access_token").asText();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package team.haedal.gifticionfunding.auth.service;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SecurityService {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import team.haedal.gifticionfunding.entity.user.User;

public interface UserJpaRepository extends JpaRepository<User, Long> {
Boolean existsByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package team.haedal.gifticionfunding.service.user;

import org.springframework.stereotype.Service;

@Service
public class UserService {
}

0 comments on commit 4241c23

Please sign in to comment.