前后端分离跨域springBoot跨域有效快速解决方案

之前一直听过跨域这个词,以前的项目也有跨域需要处理,但是自己未参与也未曾看过别人是怎么解决的。

最近有个前后端完全分离项目, 需要解决一下跨域问题;解决完了就简单在此记录一下;

同源策略:

请求的url地址,必须与浏览器上的url地址处于同域上,也就是域名,端口,协议相同.

比如:我在前端页面上的域名是3000端口,请求后台项目端口为8000的域名

这个时候在浏览器上会报错:

这样后台肯定是进不去,所以直接上代码。以下是spring boot 解决方案,

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class MyConfiguration {
    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 设置域名你要允许的网站,如果全允许则设为 *
        //config.addAllowedOrigin("http://127.0.0.1:3000");
       config.addAllowedOrigin("*");
        // 如果要限制 HEADER 或 METHOD 请自行更改
        config.addAllowedHeader("x-requested-with,content-type,requesttype");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        // 这个顺序很重要哦,为避免麻烦请设置在最前
        bean.setOrder(0);
        return bean;
    }
}

页面ajax正常请求就可以访问了,url示例:

http://127.0.0.1:80/person/checkStatusPerson

如果有问题请及时留言,我会第一时间反馈,也可添加我的qq:983469079

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
跨域问题是指前端页面向不同的源请求数据时,由于浏览器同源策略的限制,导致请求失败的问题。 在 Spring Boot 中,可以通过使用 Spring Security 解决跨域问题,同时在 Vue 中也可以通过配置代理解决跨域问题。 以下是 Spring Boot + Spring Security + Vue 前后端分离跨域解决方法: 1. 启用 Spring Security 在 Spring Boot 项目中,可以通过使用 Spring Security 来添加安全性。可以通过在 pom.xml 文件中添加以下依赖来启用 Spring Security: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 添加跨域配置 在 Spring Security 中,可以通过添加跨域配置来允许跨域请求。可以在 WebSecurityConfigurerAdapter 类中添加以下配置: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable(); } @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOrigins(Arrays.asList("*")); configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE")); configuration.setAllowedHeaders(Arrays.asList("content-type")); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } } ``` 这里我们设置了允许任何来源的请求,允许 GET、POST、PUT、DELETE 方法的请求,并且允许 content-type 头部的信息。 3. 配置代理 在 Vue 中,可以通过配置代理来解决跨域问题。可以在 vue.config.js 文件中添加以下配置: ``` module.exports = { devServer: { proxy: { '/api': { target: 'http://localhost:8080', changeOrigin: true, pathRewrite: { '^/api': '' } } } } } ``` 这里我们设置了代理地址为 http://localhost:8080,将所有以 /api 开头的请求转发到代理地址,并且开启了 changeOrigin 选项,解决跨域问题。 现在我们就成功地解决Spring Boot + Spring Security + Vue 前后端分离跨域的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值