zoukankan      html  css  js  c++  java
  • Spring Boot Web应用开发 CORS 跨域请求支持

    一、Web开发经常会遇到跨域问题,解决方案有:jsonp,iframe,CORS等等

    CORS与JSONP相比

    1、 JSONP只能实现GET请求,而CORS支持所有类型的HTTP请求。

    2、 使用CORS,开发者可以使用普通的XMLHttpRequest发起请求和获得数据,比起JSONP有更好的错误处理。

    3、 JSONP主要被老的浏览器支持,它们往往不支持CORS,而绝大多数现代浏览器都已经支持了CORS

    浏览器支持情况

    Chrome 3+

    Firefox 3.5+

    Opera 12+

    Safari 4+

    Internet Explorer 8+

    二、在spring MVC 中可以配置全局的规则,也可以使用@CrossOrigin注解进行细粒度的配置。

    全局配置:

    @Configuration
    
    public class CustomCorsConfiguration {
    
     
    
      @Bean
    
      public WebMvcConfigurer corsConfigurer() {
    
        return new WebMvcConfigurerAdapter() {
    
             @Override
    
             public void addCorsMappings(CorsRegistry registry) {
    
                 registry.addMapping("/api/**").allowedOrigins("http://localhost:8080");
    
             }
    
        };
    
      }
    
    }

    或者是

    /**
    
     * 全局设置
    
     *
    
     * @author wujing
    
     */
    
    @Configuration
    
    public class CustomCorsConfiguration2 extends WebMvcConfigurerAdapter {
    
     
    
      @Override
    
      public void addCorsMappings(CorsRegistry registry) {
    
        registry.addMapping("/api/**").allowedOrigins("http://localhost:8080");
    
      }
    
    }
    
     

    定义方法:

    /**
    
     * @author wujing
    
     */
    
    @RestController
    
    @RequestMapping("/api")
    
    public class ApiController {
    
     
    
      @RequestMapping(value = "/get")
    
      public HashMap<String, Object> get(@RequestParam String name) {
    
        HashMap<String, Object> map = new HashMap<String, Object>();
    
        map.put("title", "hello world");
    
        map.put("name", name);
    
        return map;
    
      }
    
    }

    测试js:

    $.ajax({
    
                       url: "http://localhost:8081/api/get",
    
                    type: "POST",
    
                    data: {
    
                        name: "测试"
    
                    },
    
                    success: function(data, status, xhr) {
    
                       console.log(data);
    
                       alert(data.name);
    
                    }
    
                  });

    细粒度配置

    /**
    
     * @author wujing
    
     */
    
    @RestController
    
    @RequestMapping(value = "/api", method = RequestMethod.POST)
    
    public class ApiController {
    
     
    
      @CrossOrigin(origins = "http://localhost:8080")
    
      @RequestMapping(value = "/get")
    
      public HashMap<String, Object> get(@RequestParam String name) {
    
        HashMap<String, Object> map = new HashMap<String, Object>();
    
        map.put("title", "hello world");
    
        map.put("name", name);
    
        return map;
    
      }
    
    }

    转自:http://www.roncoo.com/article/detail/125503

     


    延伸阅读
     
  • 相关阅读:
    Emmet使用
    正则参考网址
    sublime通用快捷键 汉化 安装 插件
    【真正福利】成为专业程序员路上用到的各种优秀资料、神器及框架
    生产事故的优化经历
    Windows下oracle打补丁步骤
    Oracle10g完全卸载正确步骤
    在windows2003系统上安装两个版本的oracle
    oracle11g数据库升级数据库升级
    oracle Imp和exp以及导入常见的错误
  • 原文地址:https://www.cnblogs.com/lywJ/p/10270970.html
Copyright © 2011-2022 走看看