美文网首页
响应头设置跨域和Spring注解跨域

响应头设置跨域和Spring注解跨域

作者: Ethan_Walker | 来源:发表于2017-08-29 11:20 被阅读376次

    CORS跨域原理详解
    Spring解决跨域

    1. 响应头设置跨域
        @RequestMapping(value = "/ajax")
        public @ResponseBody
        Customer ajax(Integer id, HttpServletResponse response) {
            Customer customer = customerService.queryCustomerById(id);
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
            response.setHeader("Access-Control-Allow-Credentials", "true");
            return customer;
        }
    
    
    1. Spring注解跨域
      @CrossOrigin 可添加到方法上,也可添加到类上
        // orgins=''http://localhost:63343"表示允许域名为
     http://localhost:63343 访问该方法
      // origins="*", 表示允许所有其他的网站访问该方法
        @CrossOrigin(origins = "http://localhost:63343")
        @RequestMapping(value = "/ajax")
        public @ResponseBody
        Customer ajax(Integer id, HttpServletResponse response) {
            Customer customer = customerService.queryCustomerById(id);
            return customer;
        }
    

    相关文章

      网友评论

          本文标题:响应头设置跨域和Spring注解跨域

          本文链接:https://www.haomeiwen.com/subject/tgcodxtx.html