Java-spring MVC for cross-domain resource CORS requests

Source: Internet
Author: User

Interceptor Setting the response header this way the principle is to use interceptors before the method executes, we increase the requested response header to support cross-domain requests. Such a programme is feasible, and most of it is based on this approach. I was also planning to use this approach until I found out that the spring framework has supported CORS and has adopted the Spring Framework's built-in approach, but the same principle is true.

Direct configuration:

    <!-- API 接口跨域配置 --><mvc:cors>      <mvc:mapping path="/api/**" allowed-origins="*" allowed-methods="POST, GET, OPTIONS, DELETE, PUT" allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" allow-credentials="true" /></mvc:cors>

Of course you can also use annotations on the controller @CrossOrigin . Similar to this:

Here, the support for CORS cross-domain requests is complete, and it is important to note that your Spring MVC version must be 4.2 and above .

Java-spring MVC for cross-domain resource CORS requests

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.