【问题标题】:Spring Boot Application allows cross origin requests when it shouldntSpring Boot 应用程序在不应该的情况下允许跨源请求
【发布时间】:2021-10-14 18:36:53
【问题描述】:

我有一个服务器应用程序:

@RestController
@SpringBootApplication
public class ServerApplication {
    
    @GetMapping("/data")
    public ResponseEntity<String> getData() {
        return ResponseEntity.ok("Some Data.");
    }

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

}

和一个客户端应用程序:

@RestController
@SpringBootApplication
public class ClientApplication {
    
    RestTemplate restTemplate = new RestTemplate();
    
    @GetMapping("/test")
    public ResponseEntity<String> test(){
        ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8023/data", String.class);
        return ResponseEntity.ok("Received: " + response.getBody());
    }

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

}

两者都具有完全相同的安全配置(启用 Spring 安全性):

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers("/**")
            .permitAll();
    }

}

我预计这个 restTemplate 调用会失败,因为我没有使用 @CrossOrigin 或任何其他方法激活 CORS。但这工作得很好。当我搜索类似的问题时,我只找到关于为什么不能到达某个端点的问题,而不是关于为什么可以到达它的问题。

两个应用程序共享相同的依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

【问题讨论】:

    标签: java spring-boot cors


    【解决方案1】:

    请注意,CORS 仅适用于从浏览器发出的请求。当您使用 RestTemplate 从另一个服务查询您的服务时,您无需担心 CORS 或任何相关限制。

    还请查看CORS - Is it a client-side thing, a server-side thing, or a transport level thing?,其中提供了有关 CORS 工作原理的更多详细信息。

    【讨论】:

    • 谢谢 - 但我的服务器应用程序不应该阻止来自其他来源的请求吗?
    • 默认情况下,Spring Boot 应用程序不会阻止请求。如果您想阻止来自除少数几个来源之外的所有来源的请求,则需要将它们列入白名单。查看Enabling Cross Origin Requests for a RESTful Web Service 的指南,了解有关启用它的更多信息。
    • 也许这对我来说是一个很大的误解,但我看过多个帖子和教程如何启用跨域,因为它被完全禁用为默认设置在这里查看接受的答案:stackoverflow.com/questions/58126723/… 我为什么要当 spring boot 不阻止它们时,需要使用 @CrossOrigin("*") 启用它吗?
    • 你的 Spring Boot 服务默认不会阻塞任何请求。 CORS 是一种在浏览器发出跨域请求时会阻止跨域请求的机制。为了允许这样做,服务器应用程序需要添加 CORS 标头来管理并可能允许这些请求。但正如最初提到的,这仅适用于从浏览器发出的请求。它不适用于服务到服务的请求,甚至不适用于从另一台机器发出的 curl 请求。
    • 好的 - 到目前为止谢谢。当我希望我的服务只允许特定来源的调用而不管它是什么(浏览器/服务/curl/whatever)时 - 我需要寻找什么?
    【解决方案2】:

    正如这篇文章中所说的Spring RestTemplate call to API worked but jQuery failed because same-origin policy

    同源策略应用于网络浏览器。 https://en.wikipedia.org/wiki/Same-origin_policy.

    @CrossOrigin 注释只是将所需的标头添加到响应中,这样浏览器就不会抛出 CORS 异常。

    我无法评论,但不,您的应用程序服务器不会阻止来自其他来源的呼叫(默认情况下)

    【讨论】:

      猜你喜欢
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 2015-09-19
      • 2018-07-12
      相关资源
      最近更新 更多