前后端分离后,通讯问题 springboot + vue

springboot + vue

前端使用vue, 后端使用springboot

分开后,必将带来跨越问题,网上有文章说,开发好的vue前端,可以放到springboot一起运行,确实这样挺好的,只需要修改几个配置,而且在部署的过程中也不需要部署两个应用,但是在开发的时候,还是需要分开来开发,所以跨域问题必须存在,网上有很多方法,可以去搜一下,但在这里我就使用我觉得最适合我的方式

首先前端,几乎不用任何改动,按正常使用axios请求方式

然后后端,加一个配置类就可以了,改成跨域,如下

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 WebAppConfigurer implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*")
                .allowCredentials(true).allowedMethods("GET", "POST").maxAge(3600 * 24);
    }
}

总结:这种方式最简单,有效,所以就使用这种方式了

原文地址:https://www.cnblogs.com/oscar1987121/p/10496708.html