"Spring Boot" 2.0 increase Cross-domain requests support global configuration and local configuration

Source: Internet
Author: User

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) {
        
    }
}


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.