Saturday, April 27, 2024
HomeJavaAmazon CloudFront Signed URLs and Cookies at the moment are supported in...

Amazon CloudFront Signed URLs and Cookies at the moment are supported in AWS SDK for Java 2.x


We’re happy to announce the overall availability of Amazon CloudFront signed URLs and signed cookies within the AWS SDK for Java 2.x. Now you can securely serve personal content material by means of CloudFront, requiring that your customers entry your content material through the use of particular CloudFront signed URLs or signed cookies. To configure your CloudFront distribution to make use of this characteristic, you should specify a trusted key group (beneficial) or AWS account as a trusted signer. For extra info on establishing and configuring a CloudFront distribution, please see the Developer Information.

The brand new SDK 2.x CloudFrontUtilities part supplies the highly-requested signing options that many purchasers miss from the AWS SDK for Java 1.x. The brand new APIs within the SDK 2.x APIs have been redesigned with one level of entry for a streamlined utilization expertise.

Motivation

You’ll be able to management entry to your content material with both signed URLs or signed cookies. Signed URLs can help you create a brand new URL that has non permanent entry to one among your protected CloudFront assets. Signed cookies can help you create a brief set of credentials that can be utilized by your prospects to immediately entry one or many protected CloudFront assets.

Utilizing CloudFront Signing

1) Add a dependency for CloudFront

Step one to getting began is so as to add the dependency for CloudFront in your undertaking.

<dependency>
  <groupId>software program.amazon.awssdk</groupId>
  <artifactId>cloudfront</artifactId>
  <model>2.18.26</model> <!-- Replace to make use of the newest model. -->
</dependency>

We advocate utilizing the most up-to-date model of the SDK.

2) Instantiate the CloudFrontUtilities Class

To instantiate the utility class, you simply must name create().

import software program.amazon.awssdk.providers.cloudfront.CloudFrontUtilities;

CloudFrontUtilities cloudFrontUtilities = CloudFrontUtilities.create();

3) Create an CloudFrontSignerRequest

Subsequent, you create a CloudFrontSignerRequest utilizing a builder. A request can be utilized for each URLs and cookies.

Instance #1: Signed Request with Canned Coverage

For a URL/cookie with a canned coverage, create a CannedSignerRequest, specifying the useful resource URL, personal key, public key ID, and expiration date.

import software program.amazon.awssdk.providers.cloudfront.mannequin.CannedSignerRequest;
import java.safety.PrivateKey;
import java.time.On the spot;
import java.time.temporal.ChronoUnit;


On the spot expirationDate = On the spot.now().plus(7, ChronoUnit.DAYS);
String resourceUrl = "https://d1npcfkc2mojrf.cloudfront.internet/s3ObjectKey";
String keyPairId = "myKeyPairId";
PrivateKey privateKey = myPrivateKey; // Both PrivateKey or Path will be handed in
CannedSignerRequest cannedRequest = CannedSignerRequest.builder()
                                                       .resourceUrl(resourceUrl)
                                                       .privateKey(privateKey)
                                                       .keyPairId(keyPairId)
                                                       .expirationDate(expirationDate)
                                                       .construct();

Instance #2: Signed Request with Customized Coverage

For a URL/cookie with a customized coverage, create a CustomSignerRequest, the place you possibly can moreover specify the energetic date and/or the IP vary. On this instance, we specify each the energetic date and IP vary, although you possibly can select to specify solely one among them.

import software program.amazon.awssdk.providers.cloudfront.mannequin.CustomSignerRequest;
import java.nio.file.Path;
import java.time.On the spot;
import java.time.temporal.ChronoUnit;


On the spot expirationDate = On the spot.now().plus(7, ChronoUnit.DAYS);
String resourceUrl = "https://d111111abcdef8.cloudfront.internet/s3ObjectKey";
String keyPairId = "myKeyPairId";
On the spot activeDate = On the spot.now().plus(2, ChronoUnit.DAYS);
String ipRange = "192.168.0.1/24";
Path keyFile = myKeyFile; // Both PrivateKey or Path will be handed in

CustomSignerRequest customRequest = CustomSignerRequest.builder()
                                                       .resourceUrl(resourceUrl)
                                                       .privateKey(keyFile)
                                                       .keyPairId(keyPairId)
                                                       .expirationDate(expirationDate)
                                                       .activeDate(activeDate) //non-obligatory
                                                       .ipRange(ipRange) //non-obligatory
                                                       .construct();

4) Create a Signed URL/Signed Cookie

After you generate the request, create the signed URL or signed cookie, from which you’ll optionally generate an SdkHttpRequest to be executed by an HTTP consumer.

Instance #1: Signed URL with Canned Coverage

To create a SignedUrl with a canned coverage, you simply must name getSignedUrlWithCannedPolicy(), passing within the CannedSignerRequest occasion. With the SignedUrl object, you possibly can then name url() to return a signed URL String.

import software program.amazon.awssdk.providers.cloudfront.mannequin.CannedSignerRequest;
import software program.amazon.awssdk.providers.cloudfront.url.SignedUrl;


SignedUrl signedUrl = cloudFrontUtilities.getSignedUrlWithCannedPolicy(cannedRequest);
// Returns a signed URL String you could present to customers which can permit them to entry your content material
String url = signedUrl.url()

Instance #2: Signed Cookie with Customized Coverage

To create a CookiesForCustomPolicy, you simply must name getCookiesForCustomPolicy(), passing within the CustomSignerRequest occasion. With the CookiesForCustomPolicy object, you possibly can name the suitable strategies to return the coverage, signature, and key-pair ID cookie header values, which you’ll then ship to the viewer to grant entry to your personal content material.

import software program.amazon.awssdk.providers.cloudfront.cookie.CookiesForCustomPolicy;
import software program.amazon.awssdk.providers.cloudfront.mannequin.CustomSignerRequest;


CookiesForCustomPolicy cookies = cloudFrontUtilities.getCookiesForCustomPolicy(customRequest);
// Generates Set-Cookie header values to ship to the viewer to permit entry
String signatureHeaderValue = cookies.signatureHeaderValue();
String keyPairIdHeaderValue = cookies.keyPairIdHeaderValue();
String policyHeaderValue = cookies.policyHeaderValue();

Cleanup

The CloudFront signed URLs/cookies you generated can be legitimate till the desired expiration dates, after which customers will be unable to entry your personal content material. In case you want to revoke entry earlier than the expiration date, you simply must take away the trusted signer from the CloudFront distribution.

To utterly tear-down and clear up all of your assets, you should first disable your CloudFront distribution. Then, you possibly can delete your distribution, key group, and public key in CloudFront. Lastly, you possibly can empty and delete your S3 bucket.

Conclusion

On this weblog publish we went over the fundamental functionalities of CloudFront signed URLs and signed cookies. We additionally confirmed code examples of a signed URL with a canned coverage and a signed cookie with a customized coverage. To be taught extra about the right way to arrange and start utilizing the characteristic, go to our Developer Information. In case you are interested by how it’s applied, take a look at the supply code on GitHub. As all the time, we welcome bug reviews, characteristic requests, and pull requests on the aws-sdk-java-v2 GitHub repository.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments