When you are developing Javascript client and server application separately, you have to deal with cross domain issues. We have to request from a origin domain distinct to target domain and browser does not allow this.
So , we need to prepare server side to accept request from other domains. We need to cover the following points:
-
Accept request from other domains.
-
Accept devonfw used headers like
X-CSRF-TOKEN
orcorrelationId.
-
Be prepared to receive secured request (cookies).
It is important to note that if you are using security in your request (sending cookies) you have to set withCredentials
flag to true
in your client side request and deal with special IE8 characteristics.
To enable the CORS support from the server side add the below dependency.
<dependency>
<groupId>com.devonfw.java.starters</groupId>
<artifactId>devon4j-starter-security-cors</artifactId>
</dependency>
Add the below properties in your application.properties file.
#CORS support
security.cors.spring.allowCredentials=true
security.cors.spring.allowedOriginPatterns=*
security.cors.spring.allowedHeaders=*
security.cors.spring.allowedMethods=OPTIONS,HEAD,GET,PUT,POST,DELETE,PATCH
security.cors.pathPattern=/**
Attribute | Description | HTTP Header |
---|---|---|
allowCredentials |
Decides the browser should include any cookies associated with the request ( |
Access-Control-Allow-Credentials |
allowedOrigins |
List of allowed origins (use |
Access-Control-Allow-Origin |
allowedMethods |
List of allowed HTTP request methods ( |
- |
allowedHeaders |
List of allowed headers that can be used during the request (use |
Access-Control-Allow-Headers |
pathPattern |
Ant-style pattern for the URL paths where to apply CORS. Use "/**" to match all URL paths. |
More information about the CORS headers can be found here