First, Introduction
Web Development often encounters cross-domain issues, and the solution is: jsonp,iframe,cors, etc.
1.1. Cors compared to JSONP
1. JSONP can only implement get requests, and Cors supports all types of HTTP requests.
2, using cors, developers can use ordinary XMLHttpRequest to initiate requests and obtain data, than JSONP have better error handling.
3, Jsonp mainly by the old browser support, they often do not support cors, and most modern browsers have supported the Cors browser support situation
Chrome
Firefox 3.5+
Opera 12+
Safari 4+
Internet Explorer 8+
Ii. implementation of Cors
Description: You can configure global rules in SPRINGMVC, or use @crossorigin annotations for fine-grained configuration
2.1. Global configuration
way one: Registering Beans
package com.example.demo.utils.configuration;import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration;import org.springframework.web.servlet.config.annotation.corsregistry;import org.springframework.web.servlet.config.annotation.webmvcconfigurer;import Org.springframework.web.servlet.config.annotation.webmvcconfigureradapter;/** * created by dell on 2017/6/18. */@Configurationpublic class customcorsconfiguration { @Bean public webmvcconfigurer corsconfigurer () { return new webmvcconfigureradapter () { @Override public void addcorsmappings (CorsRegistry registry) { &Nbsp; registry.addmapping ("/api/* * "). Allowedorigins (" http://localhost:8080 "); } }; }}
Description: Represents the resource under/API request, allowing http://localhost:8080 access
Way two: Inherit Webmvcconfigureradapter
Package Com.example.demo.utils.configuration;import Org.springframework.web.servlet.config.annotation.corsregistry;import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter;/** * cross-domain request processing * @Author: I love big gold * Description: cross-domain Request processing * @Date: Created in 10:12 2017/6/18 */@Configurationpublic class CustomCorsConfiguration2 extends WebM Vcconfigureradapter {@Override public void Addcorsmappings (Corsregistry registry) {registry.addmapping ("/ap i/** "). Allowedorigins (" http://localhost:8080 "); }}
Description: Represents the resource under/API request, allowing http://localhost:8080 access
2.2, fine-grained configuration
Add @crossorigin annotations to the controller, such as: @CrossOrigin (Origins = "http://localhost:8080")
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/99/38/wKiom1lF6bXzHTE0AACZlc9X59I860.png "title=" 0{(A ' {Bz7vb (OHVW0 (jxbe6.png "alt=" Wkiom1lf6bxzhte0aaczlc9x59i860.png "/>
This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1939451
Springboot (9): Web Development-cors Support