Posted in

在 Spring Boot 中测试 CORS_AI阅读总结 — 包阅AI

包阅导读总结

1. 关键词:

Spring Boot、CORS、配置、测试、MockMvc

2. 总结:

本文介绍了在 Spring Boot 中 CORS(跨域资源共享)的配置与测试。指出 CORS 是控制不同域资源请求的机制,Spring Boot 提供多种配置方式,如全局配置等,并使用 MockMvc 进行测试,通过示例代码展示配置和测试过程,以确保应用在跨域环境中正确运行且保证安全性。

3. 主要内容:

– 跨域资源共享(CORS)

– 是控制网页服务器资源跨域请求的机制

– 现代 Web 应用前端和后端常处于不同域,需正确配置

– CORS 在 Spring Boot 中的配置

– 有全局配置、控制器特定配置和使用过滤器等方式

– 给出全局 CORS 配置的示例代码及解释

– 使用 MockMvc 测试 CORS

– 创建测试类

– 模拟 OPTIONS 请求并设置相关头

– 验证响应状态和 CORS 相关头的设置

– 展示预期的测试输出

– 结论

– 总结配置和测试方法,强调通过示例代码确保应用正确处理 CORS 请求和通过测试

思维导图:

文章地址:https://www.javacodegeeks.com/testing-cors-in-spring-boot.html

文章来源:javacodegeeks.com

作者:Yatin Batra

发布时间:2024/8/16 10:12

语言:英文

总字数:728字

预计阅读时间:3分钟

评分:82分

标签:CORS,Spring Boot,MockMvc,Web Security,Java


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

CORS (Cross-Origin Resource Sharing) is a mechanism that allows resources on a web server to be requested from another domain. In Spring Boot, configuring and testing CORS can be straightforward, especially with the support of tools like MockMvc. Let us delve to discuss and understand how to configure CORS in a Spring Boot application and how to test it using MockMvc.

1. Introduction

Cross-Origin Resource Sharing (CORS) is a security feature implemented by web browsers to control how resources on a web server can be requested from a different domain than the one that served the web page. This is essential in modern web applications where front-end applications, often served from a different domain (e.g., localhost:3000), need to interact with back-end services on another domain (e.g., api.example.com). Without CORS, browsers would block such cross-origin requests to protect users from potential security threats. However, when configured correctly, CORS allows safe and controlled communication between different domains. In a Spring Boot application, CORS can be configured at various levels to specify which domains are allowed to access the server’s resources, what HTTP methods are permitted, and whether credentials such as cookies can be shared. Understanding and testing CORS configuration is crucial for ensuring that your application behaves as expected in a cross-domain environment while maintaining security.

2. Configuring CORS in Spring Boot

Spring Boot provides several ways to configure CORS, such as through global configuration, controller-specific configuration, and using filters. Below is an example of global CORS configuration using a Spring configuration class.

2.1 Global CORS Configuration

The global CORS configuration is applied across the entire application. It can be done by defining a bean of type WebMvcConfigurer in a configuration class.

// Import statementsimport org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class CorsConfig {    @Bean    public WebMvcConfigurer corsConfigurer() {        return new WebMvcConfigurer() {            @Override            public void addCorsMappings(CorsRegistry registry) {                registry.addMapping("/**")                        .allowedOrigins("http://localhost:3000")                        .allowedMethods("GET", "POST", "PUT", "DELETE")                        .allowedHeaders("*")                        .allowCredentials(true);            }        };    }}

In the above code:

  • addMapping("/**"): Configures CORS to be applied to all endpoints in the application.
  • allowedOrigins("http://localhost:3000"): Specifies the allowed origin for CORS requests. Here, only requests from http://localhost:3000 are allowed.
  • allowedMethods("GET", "POST", "PUT", "DELETE"): Specifies the HTTP methods allowed for CORS requests.
  • allowedHeaders("*"): Allows all headers in CORS requests.
  • allowCredentials(true): Allows sending credentials such as cookies in CORS requests.

Once the global configuration is in place, CORS is enabled for all endpoints across the application.

3. Testing CORS Using MockMvc

MockMvc is a powerful tool for testing Spring MVC applications. It can be used to simulate HTTP requests, including those with CORS configurations. Below is an example of how to test CORS using MockMvc.

3.1 Testing CORS with MockMvc

Let’s create a test class to verify the CORS configuration.

// Import statementsimport org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.web.servlet.MockMvc;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@SpringBootTest@AutoConfigureMockMvcpublic class CorsTest {    @Autowired    private MockMvc mockMvc;    @Test    public void testCorsHeaders() throws Exception {        mockMvc.perform(options("/api/resource")                .header("Origin", "http://localhost:3000")                .header("Access-Control-Request-Method", "GET"))                .andExpect(status().isOk())                .andExpect(header().string("Access-Control-Allow-Origin", "http://localhost:3000"))                .andExpect(header().string("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE"))                .andExpect(header().string("Access-Control-Allow-Headers", "*"))                .andExpect(header().string("Access-Control-Allow-Credentials", "true"));    }}

In the above test:

  • options("/api/resource"): Simulates an HTTP OPTIONS request to the endpoint /api/resource. OPTIONS requests are typically used by browsers to determine the allowed CORS methods and headers.
  • header("Origin", "http://localhost:3000"): Sets the Origin header to match the allowed origin in the CORS configuration.
  • header("Access-Control-Request-Method", "GET"): Simulates a CORS preflight request for the GET method.
  • andExpect(status().isOk()): Asserts that the response status is 200 OK.
  • andExpect(header().string("Access-Control-Allow-Origin", "http://localhost:3000")): Verifies that the Access-Control-Allow-Origin header is correctly set.
  • andExpect(header().string("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE")): Verifies that the Access-Control-Allow-Methods header is correctly set.
  • andExpect(header().string("Access-Control-Allow-Headers", "*")): Verifies that the Access-Control-Allow-Headers header is correctly set.
  • andExpect(header().string("Access-Control-Allow-Credentials", "true")): Verifies that the Access-Control-Allow-Credentials header is correctly set.

This test ensures that the CORS configuration is correctly applied to the specified endpoint.

3.1.1 Test Output

The expected output for the above test is as follows:

MockHttpServletResponse:           Status = 200    Error message = null          Headers = {Access-Control-Allow-Origin=[http://localhost:3000],                      Access-Control-Allow-Methods=[GET,POST,PUT,DELETE],                      Access-Control-Allow-Headers=[*],                      Access-Control-Allow-Credentials=[true]}     Content type = null             Body =     Forwarded URL = null   Redirected URL = null

The test output shows that the CORS headers are correctly applied to the response.

4. Conclusion

In this article, we explored how to configure CORS in a Spring Boot application and how to test it using MockMvc. By using the provided code examples, you can ensure that your application correctly handles CORS requests and passes CORS-related tests.