Skip to content

Commit

Permalink
#17 [Update] Refresh token 발급받기 성공
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna-Jin committed Jul 11, 2022
1 parent 159fd0b commit 8a1ce8d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
82 changes: 41 additions & 41 deletions src/main/java/com/mpnp/baechelin/api/service/PublicApiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,47 @@ public class PublicApiService {
*/
private final StoreRepository storeRepository;
private final LocationService locationService;
private final HttpConfig httpConfig;
ObjectMapper objectMapper = new ObjectMapper();

public PublicApiResponseDto processApiToDBWithWebclientMono(PublicApiRequestDto publicApiRequestDto) throws UnsupportedEncodingException {
WebClient client = WebClient.builder()
.baseUrl("http://openapi.seoul.go.kr:8088")
// .defaultCookie("cookieKey", "cookieValue")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.defaultUriVariables(Collections.singletonMap("url", "http://openapi.seoul.go.kr:8088"))
.clientConnector(new ReactorClientHttpConnector(httpConfig.httpClient())) // 위의 타임아웃 적용
.build();

String key = URLEncoder.encode(publicApiRequestDto.getKey(), "UTF-8"); /*인증키 (sample사용시에는 호출시 제한됩니다.)*/
String type = URLEncoder.encode(publicApiRequestDto.getType(), "UTF-8"); /*요청파일타입 (xml,xmlf,xls,json) */
String service = URLEncoder.encode(publicApiRequestDto.getService(), "UTF-8"); /*서비스명 (대소문자 구분 필수입니다.)*/
String start = URLEncoder.encode(String.valueOf(publicApiRequestDto.getStartIndex()), "UTF-8"); /*요청시작위치 (sample인증키 사용시 5이내 숫자)*/
String end = URLEncoder.encode(String.valueOf(publicApiRequestDto.getEndIndex()), "UTF-8"); /*요청종료위치(sample인증키 사용시 5이상 숫자 선택 안 됨)*/

PublicApiResponseDto result = client.get().uri(
uriBuilder -> uriBuilder.pathSegment(key, type, service, start, end).path("/")
.build())
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.onStatus(HttpStatus::is4xxClientError, response -> {
throw new IllegalAccessError("400");
})
.onStatus(HttpStatus::is5xxServerError, response -> {
throw new IllegalAccessError("500");
})
.bodyToMono(PublicApiResponseDto.class).flux()
.toStream()
.findFirst()
.orElse(null);
if (result == null) {
return null;
}
setInfos(result);
saveDTO(result.getTouristFoodInfo().getRow());
return result;

}
// private final HttpConfig httpConfig;
// ObjectMapper objectMapper = new ObjectMapper();
//
// public PublicApiResponseDto processApiToDBWithWebclientMono(PublicApiRequestDto publicApiRequestDto) throws UnsupportedEncodingException {
// WebClient client = WebClient.builder()
// .baseUrl("http://openapi.seoul.go.kr:8088")
//// .defaultCookie("cookieKey", "cookieValue")
// .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
// .defaultUriVariables(Collections.singletonMap("url", "http://openapi.seoul.go.kr:8088"))
// .clientConnector(new ReactorClientHttpConnector(httpConfig.httpClient())) // 위의 타임아웃 적용
// .build();
//
// String key = URLEncoder.encode(publicApiRequestDto.getKey(), "UTF-8"); /*인증키 (sample사용시에는 호출시 제한됩니다.)*/
// String type = URLEncoder.encode(publicApiRequestDto.getType(), "UTF-8"); /*요청파일타입 (xml,xmlf,xls,json) */
// String service = URLEncoder.encode(publicApiRequestDto.getService(), "UTF-8"); /*서비스명 (대소문자 구분 필수입니다.)*/
// String start = URLEncoder.encode(String.valueOf(publicApiRequestDto.getStartIndex()), "UTF-8"); /*요청시작위치 (sample인증키 사용시 5이내 숫자)*/
// String end = URLEncoder.encode(String.valueOf(publicApiRequestDto.getEndIndex()), "UTF-8"); /*요청종료위치(sample인증키 사용시 5이상 숫자 선택 안 됨)*/
//
// PublicApiResponseDto result = client.get().uri(
// uriBuilder -> uriBuilder.pathSegment(key, type, service, start, end).path("/")
// .build())
// .accept(MediaType.APPLICATION_JSON)
// .retrieve()
// .onStatus(HttpStatus::is4xxClientError, response -> {
// throw new IllegalAccessError("400");
// })
// .onStatus(HttpStatus::is5xxServerError, response -> {
// throw new IllegalAccessError("500");
// })
// .bodyToMono(PublicApiResponseDto.class).flux()
// .toStream()
// .findFirst()
// .orElse(null);
// if (result == null) {
// return null;
// }
// setInfos(result);
// saveDTO(result.getTouristFoodInfo().getRow());
// return result;
//
// }

public PublicApiResponseDto processApiToDBWithRestTemplate(PublicApiRequestDto publicApiRequestDto) throws UnsupportedEncodingException, JsonProcessingException {
HttpHeaders headers = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ protected String determineTargetUrl(HttpServletRequest request, HttpServletRespo
// refresh token이 이미 존재한다면 토큰 업데이트
if (userRefreshToken != null) {
userRefreshToken.setRefreshToken(refreshToken.getToken());
userRefreshTokenRepository.saveAndFlush(userRefreshToken);
} else {
// refresh token이 없다면 DB에 저장
userRefreshToken = new UserRefreshToken(userInfo.getId(), refreshToken.getToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public AuthResponse refreshToken (HttpServletRequest request, HttpServletRespons
// access token 확인
String accessToken = HeaderUtil.getAccessToken(request);
AuthToken authToken = tokenProvider.convertAuthToken(accessToken);
if (!authToken.validate()) {
return AuthResponse.invalidAccessToken();
}
// if (!authToken.validate()) {
// return AuthResponse.invalidAccessToken();
// }

// expired access token 인지 확인
Claims claims = authToken.getExpiredTokenClaims();
Expand All @@ -63,7 +63,7 @@ public AuthResponse refreshToken (HttpServletRequest request, HttpServletRespons
.orElse((null));
AuthToken authRefreshToken = tokenProvider.convertAuthToken(refreshToken);

if (authRefreshToken.validate()) {
if (!authRefreshToken.validate()) {
return AuthResponse.invalidRefreshToken();
}

Expand Down

0 comments on commit 8a1ce8d

Please sign in to comment.