DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • CRUDing NoSQL Data With Quarkus, Part Two: Elasticsearch
  • Unlocking the Power of Oracle NoSQL With Quarkus: Seamless Integration for Cloud-Age Applications
  • CRUDing NoSQL Data With Quarkus, Part One: MongoDB
  • Demystifying Virtual Thread Performance: Unveiling the Truth Beyond the Buzz

Trending

  • Advanced-Data Processing With AWS Glue
  • Navigating the Digital Frontier: A Journey Through Information Technology Progress
  • RRR Retro and IPL for Rewards and Recognition
  • Harnessing the Power of SIMD With Java Vector API
  1. DZone
  2. Coding
  3. Java
  4. Simple Quarkus Example With CORS

Simple Quarkus Example With CORS

Learn how to enable the CORS filter with Quarkus.

By 
Chandra Shekhar Pandey user avatar
Chandra Shekhar Pandey
·
Apr. 06, 21 · Tutorial
Like (2)
Save
Tweet
Share
16.8K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will discuss how we can enable the CORS filter with Quarkus. This is very well described at Quarkus Documentation. As per the documentation:

Quarkus comes with a CORS filter which implements the javax.servlet.Filter interface and intercepts all incoming HTTP requests. It can be enabled in the Quarkus configuration file, src/main/resources/application.properties:

Java
 




x


 
1
quarkus.http.cors=true


Once we enable this, all requests will be allowed to reach Quarkus service endpoint. I will just demonstrate this with an example which is available at my GitHub repository.

Here, two folders are important.

1. corsRequest/src: HTML (index.html) and JavaScript (app.js): This is a simple HTML which accesses the Quarkus endpoint via app.js.

2. quarkusCorsRestEasy: Quarkus simple resteasy endpoint. One can also create this application from https://code.quarkus.io/ - there is no need to select any extension. This is a basic example.

Let’s now execute the example:

1. Run Quarkus Service. Here, we have set "quarkus.http.cors=true" in application.properties.

Shell
 




xxxxxxxxxx
1
10


 
1
[chandrashekhar@localhost quarkusCorsRestEasy]$ ./mvnw compile quarkus:dev
2

          
3
__  ____  __  _____   ___  __ ____  ______ 
4
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
5
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
6
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
7
2021-04-04 15:13:06,876 INFO  [io.quarkus] (Quarkus Main Thread) quarkuscors 1.0 on JVM (powered by Quarkus 1.13.0.Final) started in 2.505s. Listening on: http://localhost:8080
8
2021-04-04 15:13:06,897 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
9
2021-04-04 15:13:06,897 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, kubernetes, resteasy]
10

          


2. In app.js, we just invoke the backend Quarkus service.

3. Open the browser to view index.html within corsRequest/src.

Shell
 




xxxxxxxxxx
1


 
1
[chandrashekhar@localhost quarkusCORS]$ cd corsRequest/src/
2
[chandrashekhar@localhost src]$ ls
3
app.js  index.html  style.css
4
[chandrashekhar@localhost src]$ firefox index.html


4. In the browser, we will find the text "Hello RESTEasy quarkus example". This matches GreetingResource.java, so all is well - the request from the browser is able to access Quarkus service.

5. Now, let’s set "quarkus.http.cors=false" in application.properties. Start the Quarkus service again.

Shell
 




x


 
1
[chandrashekhar@localhost quarkusCorsRestEasy]$ ./mvnw compile quarkus:dev
2
----
3
Listening for transport dt_socket at address: 5005
4
[INFO] Checking for existing resources in: /mnt/79fece0c-d480-4c54-8268-eaf9ce6ba53d/Development_SSD/GIT_REPO/POC/quarkusCORS/quarkusCorsRestEasy/src/main/kubernetes.
5
__  ____  __  _____   ___  __ ____  ______ 
6
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
7
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
8
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
9
2021-04-04 15:29:16,557 INFO  [io.quarkus] (Quarkus Main Thread) quarkuscors 1.0 on JVM (powered by Quarkus 1.13.0.Final) started in 2.403s. Listening on: http://localhost:8080
10
2021-04-04 15:29:16,562 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
11
2021-04-04 15:29:16,562 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, kubernetes, resteasy]
12

          


6. If we open index.html again from browser, we will find text "script execution" as response in Page. In the Console tab of browser's developer tool we will find errors like.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/hello-resteasy. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

That's it. I hope this POC will help you to test out CORS with Quarkus quickly. Also, setting "quarkus.http.cors=true" may not meet your requirement as it enables all requests to go through. Thus, you would need more quarkus CORS headers as described in the Quarkus Documentation to even have more control of request from which domains should access endpoint.

Quarkus

Opinions expressed by DZone contributors are their own.

Related

  • CRUDing NoSQL Data With Quarkus, Part Two: Elasticsearch
  • Unlocking the Power of Oracle NoSQL With Quarkus: Seamless Integration for Cloud-Age Applications
  • CRUDing NoSQL Data With Quarkus, Part One: MongoDB
  • Demystifying Virtual Thread Performance: Unveiling the Truth Beyond the Buzz

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: