Java cross-domain resource access Corsfilter

Source: Internet
Author: User

  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.