I. INTRODUCTION
When spring boot is upgraded to 2.0 and discovers that the inheritance Webmvcconfigureradapter is out of date, we follow the trend.
Two • Global configuration
2.0 cross-domain request code was previously supported:
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.web.servlet.config.annotation.CorsRegistry;
Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* Description: Cross-domain Request
* *
@author wangbin
* @version v1.0
* @date 2018/1/21/
* * @Configuration The public
class Corsconfig extends Webmvcconfigureradapter {@Override the public
void Addcorsmappings ( Corsregistry registry) {
registry.addmapping ("/**")
. Allowedorigins ("*")
. Allowcredentials (True)
. Allowedmethods ("*")
. MaxAge (3600);
}
2.0 as follows:
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.web.servlet.config.annotation.CorsRegistry;
Import ORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;
Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Description: Cross-domain Request
* *
@author wangbin
* @version v1.0
* @date 2018/1/21/
* * @Configuration
@EnableWebMvc Public
class Corsconfig implements Webmvcconfigurer {
@Override public
Void Addcorsmappings (Corsregistry registry) {
//Set allow Cross-domain path
registry.addmapping ("/**")
//Set domain name that allows cross-domain requests
. Allowedorigins ("*")
//Whether the certificate is allowed to no longer be enabled by default
. Allowcredentials (True)
//Set allowed methods
. Allowedmethods ("*")
//cross-domain Allow time
. MaxAge (3600);
}
Three • Local Configuration example
The main use of @crossorigin annotations, the overall configuration in the @crossorigin is still available
Can be annotated on a single method
@RestController
@RequestMapping (/account) public
class AccountController {
@CrossOrigin
@ GetMapping (/{id}) Public account
Retrieve (@PathVariable Long ID) {
}
@DeleteMapping (/{id})
Public void Remove (@PathVariable Long ID) {
}
}
You can also annotate the entire controller.
@CrossOrigin (Origins = http://domain2.com, maxage = 3600)
@RestController
@RequestMapping (/account)
public class AccountController {
@GetMapping (/{id}) Public account
Retrieve (@PathVariable Long ID) {
}< c16/> @DeleteMapping (/{id}) public
void Remove (@PathVariable Long ID) {
}
}
can also be solved on the whole controller while annotations are on a single method
@CrossOrigin (maxage = 3600)
@RestController
@RequestMapping (/account) public
class AccountController {
@CrossOrigin (http://domain2.com)
@GetMapping (/{id}) Public account
Retrieve (@PathVariable Long ID) {
}
@DeleteMapping (/{id}) public
void Remove (@PathVariable Long ID) {
}
}