Showing posts with label cors-policy. Show all posts
Showing posts with label cors-policy. Show all posts

Wednesday, July 6, 2022

How to enable CORS in Grails Application

In this tutorial, we are going to learn how to enable the cross-origin resources sharing CORS in a Grail Application.

Introduction:

Cross-origin resource sharing is the way to trust between the two web services or applications. So if the two web services don't satisfy then a CORS issue may arise.

Cors is a mechanism for the web application that controls listening to certain request from other web applications which is not hosted on the same server. It will not grant access to the content from other applications. So, in order to interact between two different web applications, we need to enable the Cors for that particular app.

Enable CORS in Grails Application:

We can enable cors in application.yml as below

grails:
    cors:
        enabled: true

The above configuration enables the cors for all the origins i.e all applications can interact with our application.

Enable Cors for a specific domain:

application.yml

grails:
    cors:
        enabled: true
        allowedOrigins:
            - https://example.com

This will allow the request only for the domain example.com

Enable Cors for specific requests or URLs:

application.yml

grails:
    cors:
        enabled: true
        allowedHeaders:
            - Content-Type
        mappings:
            '[/api/**]':
                allowedOrigins:
                    - https://example.com

This will allow all the requests from URLs that start with /api for example.com domain. Note that the mapping key must be made with bracket notation i.e [/api/**]

For more detail please visit Grails CORS.

Share:

Sunday, January 9, 2022

How to enable cross origin resources sharing CORS on amazon s3 bucket

In this tutorial, we will learn how we can resolve the CORS issue for the amazon s3 object link. This is typically the configuration setup on the s3 bucket.

Cross origin resources sharing is the way to trust between the two web services. So if the two web services don't satisfy then a CORS issue may arise.

Cors is a mechanism for the web application that controls to listen to certain request from other web applications which is not hosted on the same server. It will not grant access to the content from other applications. So, in order to interact between two different web applications, we need to enable the Cors for that particular app.

Hence, we need to enable the CORS in the amazon s3 bucket to get access to the content inside it.

To enable CORS policy in amazon s3, follow the following steps.

Login in to your amazon management console and go to the services and select s3

Now click on the bucket name listed inside s3 where you want to configure the CORS policy

You can see the different tabs as shown below, so click on the Permissions.



Scroll down to the bottom, we can see the Cross-origin resource sharing (CORS) option. Now click on the Edit button to add the policy. After that add the following JSON configuration

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "Content-Range",
            "Content-Length",
            "ETag"
        ],
        "MaxAgeSeconds": 3000
    }
]

Click on save changes. For more advanced configuration please visit the s3 CORS config



Share: