弹簧启动'Access-Control-Allow-Origin'

Chr*_*ton 7 java spring cors spring-boot

我有一个简单的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.

这是我的春季启动服务:

@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;
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的jQuery:

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

  console.log(data);
});
Run Code Online (Sandbox Code Playgroud)

但我仍然收到此错误:

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.
Run Code Online (Sandbox Code Playgroud)

我已经通过添加到方法尝试了这一点,我已经在类级别尝试过.我也看了这个,因为我在端口上运行我的UI .但是这个链接不是特定于春天的.@CrossOrigingetAllUsers()3000

编辑

添加我的请求标头:

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
Run Code Online (Sandbox Code Playgroud)

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

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

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

acd*_*ior 11

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

@SpringBootApplication
@RestController
public class ChrisboltonServiceApplication {

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

...
Run Code Online (Sandbox Code Playgroud)

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


abh*_*sen 5

只需在任何包中添加 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("*");
    }
}
Run Code Online (Sandbox Code Playgroud)

更新 application.properties 文件

# application.properties
spring.profiles.active=development
server.port=8090
Run Code Online (Sandbox Code Playgroud)