簡體   English   中英

帶有彈簧靴的“Access-Control-Allow-Origin”

[英]'Access-Control-Allow-Origin' with spring boot

我有一個簡單的spring boot服務,它運行在一個暴露在端口8080上的docker容器中,該服務正在調用一個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;
}
}

這是我的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()方法來嘗試這個,並且我已經在類級別嘗試過。 我也看過這個,因為我在端口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

嘗試將其添加到您的應用程序中:

@SpringBootApplication
@RestController
public class ChrisboltonServiceApplication {

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

...

另外,嘗試從$.ajax()刪除crossDomain: true

只需在任何包中添加 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

如果您希望:8080允許在那里請求,您可以將@CrossOrigin("http://localhost:8080")到正確的方法中。 這是一個端點/控制器的簡單配置。 當然,您也可以在此處使用變量進行自定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM