码迷,mamicode.com
首页 > 其他好文 > 详细

@CrossOrigin 跨域注解

时间:2017-04-12 13:21:30      阅读:488      评论:0      收藏:0      [点我收藏+]

标签:tom   xposed   not   param   nbsp   ret   count   lin   ann   

在spring 4.2后,提供了跨域注解@CrossOrigin

https://spring.io/guides/gs/rest-service-cors/

Enabling CORS

Controller method CORS configuration

So that the RESTful web service will include CORS access control headers in its response, you just have to add a @CrossOrigin annotation to the handler method:

src/main/java/hello/GreetingController.java

用例:

  @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));
    }

 

This @CrossOrigin annotation enables cross-origin requests only for this specific method. By default, its allows all origins, all headers, the HTTP methods specified in the @RequestMapping annotation and a maxAge of 30 minutes is used. You can customize this behavior by specifying the value of one of the annotation attributes: origins, methods, allowedHeaders, exposedHeaders, allowCredentials or maxAge. In this example, we only allow http://localhost:9000 to send cross-origin requests.

@CrossOrigin注解是被注解的方法具备接受跨域请求的功能。默认情况下,它使方法具备接受所有域,所有请求消息头的请求。。。。这个例子中,我们仅接受

http://localhost:9000发送来的跨域请求。

 

 

 

 

 

 

@CrossOrigin 跨域注解

标签:tom   xposed   not   param   nbsp   ret   count   lin   ann   

原文地址:http://www.cnblogs.com/guazi/p/6698281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!