forked from KNU-HAEDAL/Birthday_Funding_BE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KNU-HAEDAL#1 feat: OAuthController 및 서비스 제작
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/team/haedal/gifticionfunding/auth/api/OAuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
src/main/java/team/haedal/gifticionfunding/auth/dto/TokenDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/team/haedal/gifticionfunding/auth/service/OAuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
|
||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/team/haedal/gifticionfunding/auth/service/SecurityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/java/team/haedal/gifticionfunding/service/user/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |