Skip to content

Commit

Permalink
Merge pull request #218 from WE-ARE-RACCOONS/RAC-336
Browse files Browse the repository at this point in the history
RAC-336 fix : ๋น„์ฆˆ๋ฟŒ๋ฆฌ์˜ค ํ† ํฐ ๋ฐœ๊ธ‰ ๋กœ์ง ์ˆ˜์ •
  • Loading branch information
ywj9811 authored Apr 9, 2024
2 parents a712f63 + 9acf837 commit a49c721
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package com.postgraduate.global.bizppurio.usecase;

import com.postgraduate.global.bizppurio.dto.res.BizppurioTokenResponse;
import com.postgraduate.global.config.redis.RedisRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Base64Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Arrays;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Optional;

import static java.util.Base64.getEncoder;
import static java.time.Duration.between;
import static java.time.LocalDateTime.now;
import static java.time.LocalDateTime.parse;
import static java.time.format.DateTimeFormatter.ofPattern;
import static org.springframework.http.MediaType.APPLICATION_JSON;

@Component
@RequiredArgsConstructor
@Slf4j
public class BizppurioAuth {
private final WebClient webClient;
private final RedisRepository redisRepository;

@Value("${bizppurio.token}")
private String bizzppurioToken;
Expand All @@ -23,13 +34,28 @@ public class BizppurioAuth {
@Value("${bizppurio.pw}")
private String bizzpurioPw;

public BizppurioTokenResponse getAuth() {
public String getAuth() {
String auth = bizzpurioId + ":" + bizzpurioPw;
byte[] encode = getEncoder().encode(auth.getBytes());
String encode = Base64Util.encode(auth);
Optional<String> accessToken = redisRepository.getValues(encode);
if (accessToken.isPresent())
return accessToken.get();

BizppurioTokenResponse tokenResponse = getToken(encode);
DateTimeFormatter formatter = ofPattern("yyyyMMddHHmmss");
LocalDateTime expiredAt = parse(tokenResponse.expired(), formatter).minusMinutes(10);
Duration exipiredDuration = between(now(), expiredAt);
redisRepository.setValues(encode, tokenResponse.accesstoken(), exipiredDuration);
log.info("๋น„์ฆˆ๋ฟŒ๋ฆฌ์˜ค ํ† ํฐ {}์— ๋งŒ๋ฃŒ", expiredAt);
return tokenResponse.accesstoken();
}

private BizppurioTokenResponse getToken(String encode) {
log.info("๋น„์ฆˆ๋ฟŒ๋ฆฌ์˜ค ํ† ํฐ ์žฌ๋ฐœ๊ธ‰ ์ง„ํ–‰");
return webClient.post()
.uri(bizzppurioToken)
.headers(h -> h.setContentType(APPLICATION_JSON))
.headers(h -> h.setBasicAuth(Arrays.toString(encode)))
.headers(h -> h.setBasicAuth(encode))
.retrieve()
.bodyToMono(BizppurioTokenResponse.class)
.block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.user.domain.entity.User;
import com.postgraduate.global.bizppurio.dto.req.CommonRequest;
import com.postgraduate.global.bizppurio.dto.res.BizppurioTokenResponse;
import com.postgraduate.global.bizppurio.dto.res.MessageResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -47,11 +46,11 @@ public void mentoringFinish(User user) {

private void sendMessage(CommonRequest commonRequest) {
try {
BizppurioTokenResponse tokenResponse = bizppurioAuth.getAuth();
String accessToken = bizppurioAuth.getAuth();
webClient.post()
.uri(messageUrl)
.headers(h -> h.setContentType(APPLICATION_JSON))
.headers(h -> h.setBearerAuth(tokenResponse.accesstoken()))
.headers(h -> h.setBearerAuth(accessToken))
.bodyValue(commonRequest)
.retrieve()
.bodyToMono(MessageResponse.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.user.domain.entity.User;
import com.postgraduate.global.bizppurio.dto.req.CommonRequest;
import com.postgraduate.global.bizppurio.dto.res.BizppurioTokenResponse;
import com.postgraduate.global.bizppurio.dto.res.MessageResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -58,11 +57,11 @@ public void mentoringFinish(User user) {

private void sendMessage(CommonRequest commonRequest) {
try {
BizppurioTokenResponse tokenResponse = bizppurioAuth.getAuth();
String accessToken = bizppurioAuth.getAuth();
webClient.post()
.uri(messageUrl)
.headers(h -> h.setContentType(APPLICATION_JSON))
.headers(h -> h.setBearerAuth(tokenResponse.accesstoken()))
.headers(h -> h.setBearerAuth(accessToken))
.bodyValue(commonRequest)
.retrieve()
.bodyToMono(MessageResponse.class)
Expand Down

0 comments on commit a49c721

Please sign in to comment.