A resource initiates a cross-domain HTTP request when a resource requests a resource from a domain or a different port from a different domain or port than the server on which the resource itself resides.
For security reasons, the browser restricts cross-domain HTTP requests originating from within the script. The cross-domain resource sharing mechanism allows the Web application server to perform cross-domain access control, allowing cross-domain data transfer to be secured. Browsers support the use of CORS in the API container to mitigate the risks associated with cross-domain HTTP requests.
For Java development, in order to better do business tiering, often will be separated from the front-end code, published on different servers, at this time, you will encounter cross-domain problems.
There are a number of solutions across domains, and this article will help you if you are using SPRINGMVC to develop a server. It defines a filter to implement cross-domain requests, enabling the acquisition of HTTP request resources from different servers.
The code examples are as follows:
# # 1. Add a jar package to implement the Cors feature:
Gradle: ' com.thetransactioncompany:cors-filter:2.5 '
# # 2. Under the project's Web. xml file, add a filter tag:
<!--CORS cross-domain resource access - <Filter> <Filter-name>CORS</Filter-name> <Filter-class>Com.thetransactioncompany.cors.CORSFilter</Filter-class> <Init-param> <Param-name>Cors.allowgenerichttprequests</Param-name> <Param-value>True</Param-value> </Init-param> <Init-param> <Param-name>Cors.alloworigin</Param-name> <Param-value>*</Param-value> </Init-param> <Init-param> <Param-name>Cors.allowsubdomains</Param-name> <Param-value>False</Param-value> </Init-param> <Init-param> <Param-name>Cors.supportedmethods</Param-name> <Param-value>GET, HEAD, POST, OPTIONS</Param-value> </Init-param> <Init-param> <Param-name>Cors.supportedheaders</Param-name> <Param-value>*</Param-value> </Init-param> <Init-param> <Param-name>Cors.exposedheaders</Param-name> <Param-value>X-test-1, X-test-2</Param-value> </Init-param> <Init-param> <Param-name>Cors.supportscredentials</Param-name> <Param-value>True</Param-value> </Init-param> <Init-param> <Param-name>Cors.maxage</Param-name> <Param-value>3600</Param-value> </Init-param> </Filter> <filter-mapping> <Filter-name>CORS</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping>
# # 3. By restarting your service, you can implement cross-domain requests.
Java cross-domain resource access Corsfilter