Spring-boot rest接口跨源访问限制 Access-Control-Allow-Origin

 跨源访问控制,见官网说明 https://spring.io/guides/gs/rest-service-cors/

1.接口层做限制

 @CrossOrigin(origins = "http://localhost:9000")
    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(required=false, defaultValue="World") String name) {
        System.out.println("==== in greeting ====");
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

2.全局限制控制

  @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/greeting-javaconfig").allowedOrigins("http://localhost:9000");
            }
        };
    }

猜你喜欢

转载自blog.csdn.net/m1361459098/article/details/79990751