解决Spring Boot中的跨域请求问题

本文涉及的产品
.cn 域名,1个 12个月
简介: 解决Spring Boot中的跨域请求问题

解决Spring Boot中的跨域请求问题

今天我们来探讨如何解决Spring Boot中的跨域请求问题。跨域请求(CORS)问题是前后端分离开发中常见的一个问题,当浏览器中的前端代码向不同域名的服务器发送请求时,浏览器会阻止请求以保护用户的安全。我们将介绍如何在Spring Boot应用中配置跨域请求,确保前端应用能够正常与后端通信。

1. 什么是跨域请求

跨域请求是指浏览器从一个域名向另一个不同域名发起的HTTP请求。现代浏览器基于安全考虑,会对跨域请求进行限制,这就是所谓的同源策略。跨域资源共享(CORS)是一种W3C标准,它允许服务器明确告诉浏览器允许哪些跨域请求。

2. 跨域请求的基本原理

当浏览器发起跨域请求时,会先发送一个预检请求(OPTIONS),以确定实际请求是否被允许。服务器需要在响应头中设置相关的CORS策略,告诉浏览器允许跨域访问。

3. 在Spring Boot中配置CORS

在Spring Boot中,有几种方法可以配置CORS。我们将介绍如何在控制器级别、全局配置和通过过滤器配置CORS。

3.1 在控制器级别配置CORS

可以在具体的控制器或方法上使用@CrossOrigin注解来配置CORS。

package cn.juwatech.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
    @CrossOrigin(origins = "http://example.com")
    @GetMapping("/greet")
    public String greet() {
        return "Hello, World!";
    }
}

3.2 全局配置CORS

如果需要全局配置CORS,可以在WebMvcConfigurer中配置。

package cn.juwatech.config;
import 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;
@Configuration
public class WebConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("http://example.com")
                        .allowedMethods("GET", "POST", "PUT", "DELETE")
                        .allowedHeaders("*")
                        .allowCredentials(true);
            }
        };
    }
}

3.3 通过过滤器配置CORS

可以自定义一个过滤器来配置CORS。

package cn.juwatech.config;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
@Component
public class CORSFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "http://example.com");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            filterChain.doFilter(request, response);
        }
    }
}

4. 处理复杂的CORS请求

有时,我们可能需要处理更复杂的CORS请求,例如允许多个源、指定具体的头部等。我们可以在CorsRegistry中灵活配置。

package cn.juwatech.config;
import 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;
@Configuration
public class WebConfig {
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("http://example.com", "http://anotherdomain.com")
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                        .allowedHeaders("Content-Type", "Authorization")
                        .allowCredentials(true)
                        .maxAge(3600);
            }
        };
    }
}

5. 在Spring Security中配置CORS

如果你的应用使用了Spring Security,还需要在Spring Security配置中允许CORS。

package cn.juwatech.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.session.HttpSessionEventPublisher;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .httpBasic();
    }
    @Bean
    public HttpSessionEventPublisher httpSessionEventPublisher() {
        return new HttpSessionEventPublisher();
    }
}

6. 测试CORS配置

配置完成后,可以使用浏览器或者工具(如Postman)测试CORS配置是否正确。确保预检请求(OPTIONS)和实际请求都能够正确返回,并且浏览器不会报CORS错误。

7. 注意事项

在配置CORS时,需要注意安全问题。例如,允许所有源(*)是不安全的,应该尽量限制为可信任的域名。同时,允许的HTTP方法和头部也应该尽量少,以减少潜在的安全风险。

通过正确配置CORS,我们可以确保前后端分离的应用能够正常通信,从而提高开发效率和用户体验。

相关文章
|
2月前
|
缓存 Java 微服务
Spring Boot中的跨域请求处理
Spring Boot中的跨域请求处理
|
前端开发 Java
SpringBoot开发秘籍 - 处理跨域请求CORS
SpringBoot开发秘籍 - 处理跨域请求CORS
86 0
|
安全 JavaScript 前端开发
Springboot如何优雅的解决ajax+自定义headers的跨域请求
Springboot如何优雅的解决ajax+自定义headers的跨域请求
Springboot如何优雅的解决ajax+自定义headers的跨域请求
|
前端开发 Java Spring
Spring配置跨域请求
本文主要是Spring+SpringMVC+MyBatis/MyBatis Plus框架环境,包括SpringBoot同样适用。 1.编写拦截器 package com.interceptor; import javax.
1163 0
|
Web App开发 JavaScript 前端开发
SpringBoot多跨域请求的支持(JSONP)
在我们做项目的过程中,有可能会遇到跨域请求,所以需要我们自己组装支持跨域请求的JSONP数据,而在4.1版本以后的SpringMVC中,为我们提供了一个AbstractJsonpResponseBodyAdvice的类用来支持jsonp的数据(SpringBoot接收解析web请求是依赖于SpringMVC实现的)。下面我们就看一下怎么用AbstractJsonpResponseBodyA
2493 0
|
1月前
|
缓存 Java Maven
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
|
2月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本节课主要讲解了 Spring Boot 中如何在业务代码中读取相关配置,包括单一配置和多个配置项,在微服务中,这种情况非常常见,往往会有很多其他微服务需要调用,所以封装一个配置类来接收这些配置是个很好的处理方式。除此之外,例如数据库相关的连接参数等等,也可以放到一个配置类中,其他遇到类似的场景,都可以这么处理。最后介绍了开发环境和生产环境配置的快速切换方式,省去了项目部署时,诸多配置信息的修改。
|
2月前
|
Java 应用服务中间件 开发者
Java面试题:解释Spring Boot的优势及其自动配置原理
Java面试题:解释Spring Boot的优势及其自动配置原理
99 0
|
1天前
|
SQL 监控 druid
springboot-druid数据源的配置方式及配置后台监控-自定义和导入stater(推荐-简单方便使用)两种方式配置druid数据源
这篇文章介绍了如何在Spring Boot项目中配置和监控Druid数据源,包括自定义配置和使用Spring Boot Starter两种方法。
|
1天前
|
XML Java 关系型数据库
springboot 集成 mybatis-plus 代码生成器
本文介绍了如何在Spring Boot项目中集成MyBatis-Plus代码生成器,包括导入相关依赖坐标、配置快速代码生成器以及自定义代码生成器模板的步骤和代码示例,旨在提高开发效率,快速生成Entity、Mapper、Mapper XML、Service、Controller等代码。
springboot 集成 mybatis-plus 代码生成器