-
[AWS] Setting up Cloudfront signed cookies for multiple paths in Java, KotlinBack End/AWS 2023. 5. 1. 18:29
What we need to do
1. Find AWS SDK Library
2. Assgin openssl rsa public key to Cloudfront
3. Set SignedCookie to Response in Application Code
Example Kotlin Code
fun getBookContentResponse( req: HttpServletRequest, res: HttpServletResponse, bookContentId: String ): BookContentResponse { val expireCalendar = Calendar.getInstance() expireCalendar.add(Calendar.MINUTE, 60) val resourcePath = "$bookContentId/*" val privateKeyFile = File(privateKeyLocation) val cookies = CloudFrontCookieSigner .getCookiesForCustomPolicy( SignerUtils.Protocol.https, // e.g. "https" (not `https://`) contentCloudFrontFQDN, // e.g. "www.abc.com" (FQDN needed, not Root Domain) privateKeyFile, // private key for Cloudfront public key resourcePath, // e.g. "image/*" (not `/image/*`) cfPubKey, // CloudFront public key id (you can find it in AWS Console) expireCalendar.time, // Signed Cookie expiredAt null, // Signed Cookie activeFrom (if null, it starts as soon as it is issued) null // Signed Cookie Allowed IP (if null, there's no retriction for client IP) ) val url = SignerUtils.generateResourcePath(SignerUtils.Protocol.https, contentCloudFrontFQDN, resourcePath) res.addCookie(makeSignedCookie(cookies.getPolicy().key, cookies.getPolicy().value)) res.addCookie(makeSignedCookie(cookies.signature.key, cookies.signature.value)) res.addCookie(makeSignedCookie(cookies.keyPairId.key, cookies.keyPairId.value)) return BookContentResponse(baseUrl = url.substring(0, url.length - 2)) } fun makeSignedCookie(key: String, value: String): Cookie { val cookie = Cookie(key, value) cookie.domain = getRootDomain(contentCloudFrontFQDN) // you can customize cookie.path = "/" cookie.isHttpOnly = true cookie.secure = true return cookie }
728x90'Back End > AWS' 카테고리의 다른 글
[AWS] CloudFront SignedCookie 특정경로 하위 모두 적용하기 (Java, Kotlin) (1) 2023.05.01 [AWS] AWS WAF 적용 시, 주의해야 할 Default 설정들 (0) 2022.12.17 [AWS] CloudWatch Log Insight value counts (CloudWatch Log Insight 에서 groupby count 사용하기) (0) 2022.12.17 [AWS] EventBridge 에서 Lambda 를 Trigger 하기 (0) 2022.11.13 [AWS] CloudWatch 로그를 S3 로 보관하기 (Exporting CloudWatch logs to S3 Bucket) (0) 2022.11.07