【问题标题】:'Access-Control-Allow-Origin' with spring boot带有弹簧靴的“访问控制允许来源”
【发布时间】:2018-02-14 08:25:03
【问题描述】:

我有一个简单的spring boot 服务在docker 容器中运行,该容器暴露在端口8080 上,该容器正在调用mysql 数据库。

当我点击localhost:8080/blogs 时,我会返回[{"author":"Christopher Bolton","title":"Test Title 1","content":"This is some content","date":"2017-08-29"}]

当我直接在浏览器中点击它时,它工作得很好。但是,当我从 jQuery 尝试时,我得到的是正常的 Access-Control-Allow-Origin

这是我的 Spring Boot 服务:

@SpringBootApplication
@RestController
public class ChrisboltonServiceApplication {

public static void main(String[] args) {
    SpringApplication.run(ChrisboltonServiceApplication.class, args);
}

@Autowired
private JdbcTemplate jdbcTemplate;

@CrossOrigin
@RequestMapping(path="/blogs")
public @ResponseBody Iterable<ChrisBolton> getAllUsers() {
    List<ChrisBolton> result = jdbcTemplate.query(
            "SELECT * FROM blog",
            (rs, rowNum) -> new ChrisBolton(rs.getString("author"), 
                                               rs.getString("title"), 
                                               rs.getString("content"), 
                                               rs.getDate("date"))
    );

    return result;
}
}

这是我的jQuery

$.ajax({
  url: "http://localhost:8080/blogs",
  crossDomain: true
}).done(function(data) {

  console.log(data);
});

但我仍然收到此错误:

XMLHttpRequest cannot load http://localhost:8080/blogs. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

我已经通过将@CrossOrigin 添加到getAllUsers() 方法来尝试this,并且我已经在类级别进行了尝试。我还查看了this,因为我在端口3000 上运行我的UI。但该链接不是特定于 spring 的。

编辑

添加我的请求标头:

GET /blogs HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Accept: */*
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 
Safari/537.36
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

网络标签 (Chrome) 中的响应:

[{"author":"Christopher Bolton","title":"Test Title 1","content":"This is some content","date":"2017-08-29"}]

所以看起来我正在网络选项卡中获取数据。但是,我的 console.log(data) 会生成 Access-Control-Allow-Origin

【问题讨论】:

    标签: java spring spring-boot cors


    【解决方案1】:

    只需在任何包中添加 DevConfiguration,然后更新 application.properties 文件

    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    @Profile("development")
    public class DevConfiguration implements WebMvcConfigurer {
     
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS").allowedOrigins("*");
        }
    }
    

    更新 application.properties 文件

    # application.properties
    spring.profiles.active=development
    server.port=8090
    

    【讨论】:

      【解决方案2】:

      如果您希望:8080 允许那里的请求,您可以将@CrossOrigin("http://localhost:8080") 添加到正确的方法中。这是一个端点/控制器的简单配置。当然,您也可以在那里使用变量进行自定义。

      【讨论】:

      • 我知道您在技术上是正确的,但这是一个糟糕的建议。人们看到一次性配置并将它们应用到他们在代码中适用的 N 个地方。
      【解决方案3】:

      尝试将其添加到您的应用程序中:

      @SpringBootApplication
      @RestController
      public class ChrisboltonServiceApplication {
      
          @Bean
          public WebMvcConfigurer corsConfigurer() {
              return new WebMvcConfigurerAdapter() {
                  @Override
                  public void addCorsMappings(CorsRegistry registry) {
                      registry.addMapping("/**").allowedOrigins("*");
                  }
              };
          }
      
      ...
      

      另外,请尝试从$.ajax() 中删除crossDomain: true

      【讨论】:

      • 我还保留@CrossOrigin吗?仍然出现同样的错误。
      • 另外,我在上面添加了我正在使用 docker 并暴露端口 8080 的事实。也许这可能是问题的一部分?
      • 实际上,一切都应该单独使用@CrossOrigin。我刚刚在这里测试过,效果很好。您使用 docker 容器的事实应该会影响它。您的容器基于什么图像?容器内的spring boot前面不是有某种代理吗?
      • 容器正在我的机器上运行,并且 Spring Boot 应用程序暴露在端口 8080 上。我的 UI 在端口 3000 上运行。所以它是localhost 在不同的端口上调用localhost。我在这里缺少一些代理概念吗?
      • 是的,它确实有效。现在按预期返回响应。没有改变任何东西,只是擦除了我所有的容器并从头开始重建我的图像。我每次都在重建我的罐子并将其导入我的容器中。不是 100% 发生了什么。但码头工人是问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2018-10-18
      • 2017-07-21
      • 1970-01-01
      • 2016-01-02
      • 2017-12-15
      相关资源
      最近更新 更多