260803_01_ENH: 동적 qr코드 렌더링 notihub로부터 이관
This commit is contained in:
@@ -31,13 +31,17 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation "com.kri.common:starter:beta.1.1.32"
|
implementation "com.kri.common:starter:beta.1.4.0"
|
||||||
|
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-security'
|
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||||
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.11"
|
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.11"
|
||||||
|
|
||||||
|
// ZXing (QR code generation)
|
||||||
|
implementation 'com.google.zxing:core:3.5.4'
|
||||||
|
implementation 'com.google.zxing:javase:3.5.4'
|
||||||
|
|
||||||
//JPA
|
//JPA
|
||||||
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
|
implementation "org.springframework.boot:spring-boot-starter-data-jpa"
|
||||||
//Entity Json <-> Class 파싱용
|
//Entity Json <-> Class 파싱용
|
||||||
|
|||||||
@@ -3,10 +3,11 @@ package com.kri.shorturl.common.consts;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public final class ShortUrlConstat {
|
public final class ShortUrlConst {
|
||||||
|
|
||||||
public static final Integer UUID_LENGTH = 8;
|
public static final Integer UUID_LENGTH = 8;
|
||||||
public static final Integer MAX_UUID_GENERATION_ATTEMPTS = 100;
|
public static final Integer MAX_UUID_GENERATION_ATTEMPTS = 100;
|
||||||
|
public static final int QR_CODE_MARGIN = 1;
|
||||||
|
|
||||||
public static final String BASE_URL = "https://s.kric.com";
|
public static final String BASE_URL = "https://s.kric.com";
|
||||||
|
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.kri.shorturl.controller;
|
||||||
|
|
||||||
|
import com.kri.shorturl.service.QRCodeService;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import java.time.Duration;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.CacheControl;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v1/qr")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Tag(name = "QRCode", description = "QR Code API")
|
||||||
|
public class QRCodeController {
|
||||||
|
|
||||||
|
private final QRCodeService qrCodeService;
|
||||||
|
|
||||||
|
@GetMapping(value = "/{encodedUrl}", produces = MediaType.IMAGE_PNG_VALUE)
|
||||||
|
public ResponseEntity<byte[]> getQrCode(@PathVariable String encodedUrl) {
|
||||||
|
byte[] png = qrCodeService.generate(encodedUrl);
|
||||||
|
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.contentType(MediaType.IMAGE_PNG)
|
||||||
|
.cacheControl(CacheControl.maxAge(Duration.ofDays(1)).cachePublic())
|
||||||
|
.body(png);
|
||||||
|
}
|
||||||
|
}
|
||||||
116
src/main/java/com/kri/shorturl/service/QRCodeService.java
Normal file
116
src/main/java/com/kri/shorturl/service/QRCodeService.java
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package com.kri.shorturl.service;
|
||||||
|
|
||||||
|
import static com.kri.shorturl.common.consts.ShortUrlConst.QR_CODE_MARGIN;
|
||||||
|
|
||||||
|
import com.google.zxing.BarcodeFormat;
|
||||||
|
import com.google.zxing.EncodeHintType;
|
||||||
|
import com.google.zxing.WriterException;
|
||||||
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
||||||
|
import com.google.zxing.common.BitMatrix;
|
||||||
|
import com.google.zxing.qrcode.QRCodeWriter;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.CharacterCodingException;
|
||||||
|
import java.nio.charset.CodingErrorAction;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class QRCodeService {
|
||||||
|
|
||||||
|
private static final int QR_IMAGE_SIZE = 240;
|
||||||
|
private static final Map<EncodeHintType, Object> QR_ENCODE_HINTS = Map.of(
|
||||||
|
EncodeHintType.MARGIN, QR_CODE_MARGIN
|
||||||
|
);
|
||||||
|
|
||||||
|
private final int maxUrlBytes;
|
||||||
|
|
||||||
|
public QRCodeService(@Value("${kri.qr.max-url-bytes:2048}") int maxUrlBytes) {
|
||||||
|
if (maxUrlBytes <= 0) {
|
||||||
|
throw new IllegalArgumentException("kri.qr.max-url-bytes must be greater than zero.");
|
||||||
|
}
|
||||||
|
this.maxUrlBytes = maxUrlBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] generate(String encodedUrl) {
|
||||||
|
String targetUrl = decodeUrl(encodedUrl);
|
||||||
|
|
||||||
|
try {
|
||||||
|
BitMatrix matrix = new QRCodeWriter()
|
||||||
|
.encode(targetUrl, BarcodeFormat.QR_CODE, QR_IMAGE_SIZE, QR_IMAGE_SIZE, QR_ENCODE_HINTS);
|
||||||
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png", output);
|
||||||
|
return output.toByteArray();
|
||||||
|
} catch (WriterException | IOException e) {
|
||||||
|
throw new IllegalStateException("Failed to render QR code.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String decodeUrl(String encodedUrl) {
|
||||||
|
if (encodedUrl == null || encodedUrl.isBlank()) {
|
||||||
|
throw badRequest("Encoded URL must not be empty.");
|
||||||
|
}
|
||||||
|
|
||||||
|
int maxEncodedLength = (maxUrlBytes * 4 + 2) / 3;
|
||||||
|
if (encodedUrl.length() > maxEncodedLength) {
|
||||||
|
throw badRequest("Encoded URL is too long.");
|
||||||
|
}
|
||||||
|
|
||||||
|
final byte[] decoded;
|
||||||
|
try {
|
||||||
|
decoded = Base64.getUrlDecoder().decode(encodedUrl);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
throw badRequest("Invalid Base64URL value.");
|
||||||
|
}
|
||||||
|
if (decoded.length > maxUrlBytes) {
|
||||||
|
throw badRequest("Decoded URL is too long.");
|
||||||
|
}
|
||||||
|
|
||||||
|
final String targetUrl;
|
||||||
|
try {
|
||||||
|
targetUrl = StandardCharsets.UTF_8.newDecoder()
|
||||||
|
.onMalformedInput(CodingErrorAction.REPORT)
|
||||||
|
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
||||||
|
.decode(ByteBuffer.wrap(decoded))
|
||||||
|
.toString();
|
||||||
|
} catch (CharacterCodingException e) {
|
||||||
|
throw badRequest("Decoded URL is not valid UTF-8.");
|
||||||
|
}
|
||||||
|
|
||||||
|
validateUrl(targetUrl);
|
||||||
|
return targetUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validateUrl(String targetUrl) {
|
||||||
|
if (targetUrl == null || targetUrl.isBlank()) {
|
||||||
|
throw badRequest("URL must not be empty.");
|
||||||
|
}
|
||||||
|
if (targetUrl.getBytes(StandardCharsets.UTF_8).length > maxUrlBytes) {
|
||||||
|
throw badRequest("URL is too long.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
URI uri = new URI(targetUrl);
|
||||||
|
String scheme = uri.getScheme();
|
||||||
|
if (!("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme))
|
||||||
|
|| uri.getHost() == null || uri.getHost().isBlank()) {
|
||||||
|
throw badRequest("Only absolute HTTP or HTTPS URLs are allowed.");
|
||||||
|
}
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
throw badRequest("Invalid URL.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseStatusException badRequest(String reason) {
|
||||||
|
return new ResponseStatusException(HttpStatus.BAD_REQUEST, reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.kri.shorturl.service;
|
package com.kri.shorturl.service;
|
||||||
|
|
||||||
import static com.kri.shorturl.common.consts.ShortUrlConstat.BASE_URL;
|
import static com.kri.shorturl.common.consts.ShortUrlConst.BASE_URL;
|
||||||
|
|
||||||
import com.kri.common.feature.excel.ExcelMapper;
|
import com.kri.common.feature.excel.ExcelMapper;
|
||||||
import com.kri.common.feature.web.BusinessException;
|
import com.kri.common.feature.web.BusinessException;
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ kri:
|
|||||||
- /ping
|
- /ping
|
||||||
- /internal/apikey/**
|
- /internal/apikey/**
|
||||||
- /api/v1/short-url/redirect/**
|
- /api/v1/short-url/redirect/**
|
||||||
|
- /api/v1/qr/**
|
||||||
|
|
||||||
swagger:
|
swagger:
|
||||||
enabled: true
|
enabled: true
|
||||||
@@ -62,6 +63,9 @@ kri:
|
|||||||
short-url:
|
short-url:
|
||||||
salt: KricShortUrlSecret2026
|
salt: KricShortUrlSecret2026
|
||||||
|
|
||||||
|
qr:
|
||||||
|
max-url-bytes: 2048
|
||||||
|
|
||||||
|
|
||||||
web:
|
web:
|
||||||
enabled: true # 공통 Web 구성 사용 여부 (기본 true)
|
enabled: true # 공통 Web 구성 사용 여부 (기본 true)
|
||||||
|
|||||||
Reference in New Issue
Block a user