diff --git a/web3-credential-server/build/resources/main/application.properties b/web3-credential-server/build/resources/main/application.properties index 9fd291c..fd411bc 100644 --- a/web3-credential-server/build/resources/main/application.properties +++ b/web3-credential-server/build/resources/main/application.properties @@ -54,14 +54,13 @@ spring.mvc.cors.allowed-origin-patterns=http://localhost:8080 spring.mvc.cors.allow-credentials=true # AWS S3 Service bucket -cloud.aws.s3.bucket= -cloud.aws.region.static=ap-northeast-2 -cloud.aws.credentials.accessKey= -cloud.aws.credentials.secretKey= -cloud.aws.stack.auto=false - +cloud.aws.s3-bucket-name= # AWS S3 Bucket URL -cloud.aws.s3.bucket.url= +cloud.aws.s3-bucket-url= +cloud.aws.region-static=ap-northeast-2 +cloud.aws.credentials-accessKey= +cloud.aws.credentials-secretKey= +cloud.aws.stack.auto=false diff --git a/web3-credential-server/build/tmp/compileJava/previous-compilation-data.bin b/web3-credential-server/build/tmp/compileJava/previous-compilation-data.bin index 21ae870..3d0270c 100644 Binary files a/web3-credential-server/build/tmp/compileJava/previous-compilation-data.bin and b/web3-credential-server/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/web3-credential-server/src/main/java/web3/Application.java b/web3-credential-server/src/main/java/web3/Application.java index 9d8b67b..29a58f5 100644 --- a/web3-credential-server/src/main/java/web3/Application.java +++ b/web3-credential-server/src/main/java/web3/Application.java @@ -5,7 +5,7 @@ import org.springframework.boot.context.properties.ConfigurationPropertiesScan; @SpringBootApplication -@ConfigurationPropertiesScan("web3.properties.kakao") +@ConfigurationPropertiesScan("web3.properties") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/web3-credential-server/src/main/java/web3/auth/util/JwtTokenUtil.java b/web3-credential-server/src/main/java/web3/auth/util/JwtTokenUtil.java index ab031fa..97e8601 100644 --- a/web3-credential-server/src/main/java/web3/auth/util/JwtTokenUtil.java +++ b/web3-credential-server/src/main/java/web3/auth/util/JwtTokenUtil.java @@ -5,17 +5,20 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.security.Keys; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import web3.properties.JwtProperties; +import web3.properties.KakaoProperties; import java.util.Date; @Component +@RequiredArgsConstructor public class JwtTokenUtil { - @Value("${jwt.key}") - private String secretKey; + private final JwtProperties jwtProperties; @Value("${jwt.access-token-validity}") private long accessTokenValidityMilliseconds; @@ -39,7 +42,7 @@ public boolean validateToken(String token) { try { Jwts.parserBuilder() - .setSigningKey(secretKey.getBytes()) + .setSigningKey(jwtProperties.key().getBytes()) .build() .parseClaimsJws(token); @@ -56,7 +59,7 @@ public boolean validateToken(String token) { public Claims getClaimsFromToken(String token) { return Jwts.parserBuilder() - .setSigningKey(secretKey.getBytes()) + .setSigningKey(jwtProperties.key().getBytes()) .build() .parseClaimsJws(token) .getBody(); @@ -70,7 +73,7 @@ private String generateToken(String email, long validityMilliseconds) { .setSubject(email) .setIssuedAt(now) .setExpiration(validity) - .signWith(Keys.hmacShaKeyFor(secretKey.getBytes()), SignatureAlgorithm.HS256) + .signWith(Keys.hmacShaKeyFor(jwtProperties.key().getBytes()), SignatureAlgorithm.HS256) .compact(); } } diff --git a/web3-credential-server/src/main/java/web3/controller/kakao/AdminKakaoController.java b/web3-credential-server/src/main/java/web3/controller/kakao/AdminKakaoController.java index ffdc16c..d20bb33 100644 --- a/web3-credential-server/src/main/java/web3/controller/kakao/AdminKakaoController.java +++ b/web3-credential-server/src/main/java/web3/controller/kakao/AdminKakaoController.java @@ -4,7 +4,7 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; -import web3.properties.kakao.KakaoProperties; +import web3.properties.KakaoProperties; @Controller @RequestMapping("/admin/kakao") diff --git a/web3-credential-server/src/main/java/web3/controller/kakao/KakaoController.java b/web3-credential-server/src/main/java/web3/controller/kakao/KakaoController.java index affe592..d01a152 100644 --- a/web3-credential-server/src/main/java/web3/controller/kakao/KakaoController.java +++ b/web3-credential-server/src/main/java/web3/controller/kakao/KakaoController.java @@ -8,9 +8,9 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.util.UriComponentsBuilder; -import web3.properties.kakao.KakaoProperties; +import web3.properties.KakaoProperties; import web3.domain.user.User; -import web3.common.dto.user.UserInfoDto; +import web3.service.dto.user.UserInfoDto; import web3.service.kakao.KakaoService; import web3.validation.LoginMember; diff --git a/web3-credential-server/src/main/java/web3/properties/JwtProperties.java b/web3-credential-server/src/main/java/web3/properties/JwtProperties.java new file mode 100644 index 0000000..fb3700a --- /dev/null +++ b/web3-credential-server/src/main/java/web3/properties/JwtProperties.java @@ -0,0 +1,9 @@ +package web3.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "jwt") +public record JwtProperties ( + String key +){ +} diff --git a/web3-credential-server/src/main/java/web3/properties/kakao/KakaoProperties.java b/web3-credential-server/src/main/java/web3/properties/KakaoProperties.java similarity index 91% rename from web3-credential-server/src/main/java/web3/properties/kakao/KakaoProperties.java rename to web3-credential-server/src/main/java/web3/properties/KakaoProperties.java index 474cec7..b26f891 100644 --- a/web3-credential-server/src/main/java/web3/properties/kakao/KakaoProperties.java +++ b/web3-credential-server/src/main/java/web3/properties/KakaoProperties.java @@ -1,4 +1,4 @@ -package web3.properties.kakao; +package web3.properties; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/web3-credential-server/src/main/java/web3/properties/S3Properties.java b/web3-credential-server/src/main/java/web3/properties/S3Properties.java new file mode 100644 index 0000000..9f8767a --- /dev/null +++ b/web3-credential-server/src/main/java/web3/properties/S3Properties.java @@ -0,0 +1,40 @@ +package web3.properties; + +import lombok.Getter; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import jakarta.annotation.PostConstruct; +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; + +@Configuration +@ConfigurationProperties(prefix = "cloud.aws") +@Getter +@Setter +@Slf4j +public class S3Properties { + + private String s3BucketName; + private String credentialsAccessKey; + private String credentialsSecretKey; + private String s3BucketUrl; + private String regionStatic; + private S3Client s3Client; + + @PostConstruct + public void init() { + if (regionStatic == null || regionStatic.trim().isEmpty()) { + throw new IllegalStateException("Region must not be null or empty"); + } + this.s3Client = S3Client.builder() + .region(Region.of(regionStatic)) + .credentialsProvider(StaticCredentialsProvider.create( + AwsBasicCredentials.create(credentialsAccessKey, credentialsSecretKey))) + .build(); + } + +} diff --git a/web3-credential-server/src/main/java/web3/s3Storage/config/S3Config.java b/web3-credential-server/src/main/java/web3/s3Storage/config/S3Config.java index 96969cc..b37017a 100644 --- a/web3-credential-server/src/main/java/web3/s3Storage/config/S3Config.java +++ b/web3-credential-server/src/main/java/web3/s3Storage/config/S3Config.java @@ -1,21 +1,23 @@ package web3.s3Storage.config; +import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; -import org.springframework.beans.factory.annotation.Value; - +import web3.properties.S3Properties; +/* @Configuration +@RequiredArgsConstructor public class S3Config { - - @Value("${cloud.aws.region.static}") - private String region; - + private final S3Properties s3Properties; @Bean public S3Client s3Client() { return S3Client.builder() - .region(Region.of(region)) + .region(Region.of(s3Properties.getRegionStatic())) .build(); } -} \ No newline at end of file + +} + + */ \ No newline at end of file diff --git a/web3-credential-server/src/main/java/web3/s3Storage/controller/S3StorageController.java b/web3-credential-server/src/main/java/web3/s3Storage/controller/S3StorageController.java index 9e0400a..5c05f5a 100644 --- a/web3-credential-server/src/main/java/web3/s3Storage/controller/S3StorageController.java +++ b/web3-credential-server/src/main/java/web3/s3Storage/controller/S3StorageController.java @@ -100,13 +100,4 @@ public ResponseEntity getPdfKey(@RequestParam String pdfUrl, return ResponseEntity.ok().body(metadata); } - - - /*//스케줄러 용 - @GetMapping("/all/photourl") - public ResponseEntity> getAllImageUrls() { - List imageUrls = s3StorageService.getAllImageUrls(); - return ResponseEntity.ok(imageUrls); - }*/ - } \ No newline at end of file diff --git a/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java b/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java index a34ed33..b17fc20 100644 --- a/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java +++ b/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java @@ -1,21 +1,18 @@ package web3.s3Storage.service; import jakarta.annotation.PreDestroy; +import lombok.extern.slf4j.Slf4j; import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; -import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.core.ResponseInputStream; import software.amazon.awssdk.core.sync.RequestBody; -import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.*; import web3.domain.wallet.Wallet; +import web3.properties.S3Properties; import web3.repository.wallet.WalletRepository; import java.io.*; @@ -23,28 +20,17 @@ import java.util.Map; import java.util.UUID; +@Slf4j @Service public class S3StorageService { + private final S3Properties s3Properties; private final S3Client s3Client; - private final String bucketName; - private final String bucketUrl; - private final WalletRepository walletRepository; - @Autowired - public S3StorageService(@Value("${cloud.aws.credentials.accessKey}") String accessKey, - @Value("${cloud.aws.credentials.secretKey}") String secretKey, - @Value("${cloud.aws.s3.bucket}") String bucketName, - @Value("${cloud.aws.s3.bucket.url}") String bucketUrl, - WalletRepository walletRepository) { - this.s3Client = S3Client.builder() - .region(Region.AP_NORTHEAST_2) // 원하는 리전을 지정해야 합니다. - .credentialsProvider(StaticCredentialsProvider.create( - AwsBasicCredentials.create(accessKey, secretKey))) - .build(); - this.bucketName = bucketName; - this.bucketUrl = bucketUrl; + public S3StorageService(S3Properties s3Properties, WalletRepository walletRepository) { + this.s3Properties = s3Properties; + this.s3Client = s3Properties.getS3Client(); this.walletRepository = walletRepository; } @@ -56,10 +42,12 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String //첫 등록일때 => 생성해줘야함 if (wallet.getPdfUrl() == null){ - System.out.println("file = " + file); + log.info("file = {}", file); fileName = (file.getSize() > 0) ? getFileName(file, wallet): getEmptyFilename(wallet); - System.out.println("fileName = " + fileName); + + log.info("fileName = {}", fileName); + // PDF 파일 확장자 검증 //validatePdfFile(fileName); result = (file.getSize() > 0) ? file.getBytes():createEmptyPdf(); @@ -69,15 +57,15 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String //이미 있을시 -> pdf 병합 String destination = wallet.getPdfUrl(); fileName = extractKeyFromUrl(destination); - System.out.println("destination = " + destination); - System.out.println("fileName = " + fileName); + log.info("destination = {}", destination); + log.info("fileName = {}", fileName); byte[] first = getPdf(destination).readAllBytes();//원래 파일 byte[] second = (file.getSize() > 0) ? file.getBytes() : createEmptyPdf(); //뒤에 들어온 파일 nowPage = getPdfPageCount(first)+1; result = mergePdfs(first, second); - System.out.println("merge Success"); + log.info("merge Success"); metadata= getPdfMetadata(fileName); @@ -86,26 +74,26 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String String page = "page-" + nowPage; // 키 설정 String value = pdfInfo + ":" + pdfKey; metadata.put(page,value); // 키-값 쌍으로 추가 - System.out.println("metadata = " + metadata); - + log.info("metadata = {}", metadata); try { // S3에 PDF 파일 업로드 PutObjectRequest putRequest = PutObjectRequest.builder() - .bucket(bucketName) + .bucket(s3Properties.getS3BucketName()) .key(fileName) .contentType(file.getContentType()) .metadata(metadata) .build(); - // PutObject 요청 수행 s3Client.putObject(putRequest, RequestBody.fromBytes(result)); } catch (S3Exception e) { throw new IOException("Failed to upload pdf to S3: " + e.getMessage()); } + wallet.updatePdfUrl(getpdfUrl(fileName)); walletRepository.saveAndFlush(wallet); + return getpdfUrl(fileName); } @@ -174,7 +162,7 @@ public String replacePdfPage(Wallet wallet, int pageNumberToRemove, MultipartFil // 최종 PDF를 S3에 업로드 try { PutObjectRequest putRequest = PutObjectRequest.builder() - .bucket(bucketName) + .bucket(s3Properties.getS3BucketName()) .key(fileName) .contentType(newPdfFile.getContentType()) .metadata(metadata) @@ -277,7 +265,7 @@ public HashMap getPdfMetadata( String pdfUrl) { try { HeadObjectResponse response = s3Client.headObject( HeadObjectRequest.builder() - .bucket(bucketName) + .bucket(s3Properties.getS3BucketName()) .key(fileName) .build()); @@ -300,7 +288,7 @@ public String getMetadataForPage(String pdfUrl, int pageNumber) { String fileName = extractKeyFromUrl(pdfUrl); GetObjectRequest getRequest = GetObjectRequest.builder() - .bucket(bucketName) + .bucket(s3Properties.getS3BucketName()) .key(fileName) .build(); @@ -324,7 +312,7 @@ public String getPdfKeyForPage(String pdfUrl, int pageNumber) { String fileName = extractKeyFromUrl(pdfUrl); GetObjectRequest getRequest = GetObjectRequest.builder() - .bucket(bucketName) + .bucket(s3Properties.getS3BucketName()) .key(fileName) .build(); @@ -365,7 +353,7 @@ public void deletePdf(String urlToDelete) { System.out.println("key = " + key); try { s3Client.deleteObject(DeleteObjectRequest.builder() - .bucket(bucketName) + .bucket(s3Properties.getS3BucketName()) .key(key) .build()); } catch (S3Exception e) { @@ -376,7 +364,7 @@ public void deletePdf(String urlToDelete) { public ResponseInputStream getPdf(String pdfUrl) { String key = extractKeyFromUrl(pdfUrl); GetObjectRequest getObjectRequest = GetObjectRequest.builder() - .bucket(bucketName) + .bucket(s3Properties.getS3BucketName()) .key(key) .build(); return s3Client.getObject(getObjectRequest); @@ -399,7 +387,7 @@ private String extractKeyFromUrl(String url) { } private String getpdfUrl(String fileName) { - return bucketUrl + "/" + fileName; + return s3Properties.getS3BucketUrl() + "/" + fileName; } @PreDestroy diff --git a/web3-credential-server/src/main/java/web3/common/dto/jwt/JwtResponse.java b/web3-credential-server/src/main/java/web3/service/dto/jwt/JwtResponse.java similarity index 95% rename from web3-credential-server/src/main/java/web3/common/dto/jwt/JwtResponse.java rename to web3-credential-server/src/main/java/web3/service/dto/jwt/JwtResponse.java index eef1123..ac2f66f 100644 --- a/web3-credential-server/src/main/java/web3/common/dto/jwt/JwtResponse.java +++ b/web3-credential-server/src/main/java/web3/service/dto/jwt/JwtResponse.java @@ -1,4 +1,4 @@ -package web3.common.dto.jwt; +package web3.service.dto.jwt; public class JwtResponse { private String accessToken; diff --git a/web3-credential-server/src/main/java/web3/common/dto/user/UserInfoDto.java b/web3-credential-server/src/main/java/web3/service/dto/user/UserInfoDto.java similarity index 97% rename from web3-credential-server/src/main/java/web3/common/dto/user/UserInfoDto.java rename to web3-credential-server/src/main/java/web3/service/dto/user/UserInfoDto.java index 140500d..cc3d640 100644 --- a/web3-credential-server/src/main/java/web3/common/dto/user/UserInfoDto.java +++ b/web3-credential-server/src/main/java/web3/service/dto/user/UserInfoDto.java @@ -1,4 +1,4 @@ -package web3.common.dto.user; +package web3.service.dto.user; public class UserInfoDto { private long id; diff --git a/web3-credential-server/src/main/java/web3/service/kakao/KakaoService.java b/web3-credential-server/src/main/java/web3/service/kakao/KakaoService.java index ee1b3a1..588ed7a 100644 --- a/web3-credential-server/src/main/java/web3/service/kakao/KakaoService.java +++ b/web3-credential-server/src/main/java/web3/service/kakao/KakaoService.java @@ -12,9 +12,9 @@ import org.springframework.web.client.RestClient; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestClientResponseException; -import web3.properties.kakao.KakaoProperties; +import web3.properties.KakaoProperties; import web3.domain.user.User; -import web3.common.dto.user.UserInfoDto; +import web3.service.dto.user.UserInfoDto; import web3.domain.wallet.Wallet; import web3.repository.wallet.WalletRepository; import web3.service.user.UserService; diff --git a/web3-credential-server/src/main/resources/application.properties b/web3-credential-server/src/main/resources/application.properties index 9fd291c..fd411bc 100644 --- a/web3-credential-server/src/main/resources/application.properties +++ b/web3-credential-server/src/main/resources/application.properties @@ -54,14 +54,13 @@ spring.mvc.cors.allowed-origin-patterns=http://localhost:8080 spring.mvc.cors.allow-credentials=true # AWS S3 Service bucket -cloud.aws.s3.bucket= -cloud.aws.region.static=ap-northeast-2 -cloud.aws.credentials.accessKey= -cloud.aws.credentials.secretKey= -cloud.aws.stack.auto=false - +cloud.aws.s3-bucket-name= # AWS S3 Bucket URL -cloud.aws.s3.bucket.url= +cloud.aws.s3-bucket-url= +cloud.aws.region-static=ap-northeast-2 +cloud.aws.credentials-accessKey= +cloud.aws.credentials-secretKey= +cloud.aws.stack.auto=false diff --git a/web3-credential-server/src/test/java/web3/kakao/RestClientTest.java b/web3-credential-server/src/test/java/web3/kakao/RestClientTest.java index a08f1c3..a64c3cb 100644 --- a/web3-credential-server/src/test/java/web3/kakao/RestClientTest.java +++ b/web3-credential-server/src/test/java/web3/kakao/RestClientTest.java @@ -15,7 +15,7 @@ import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.client.RestTemplate; -import web3.properties.kakao.KakaoProperties; +import web3.properties.KakaoProperties; import java.util.HashMap;