当前位置: 代码网 > it编程>编程语言>Javascript > Vue+Vite+Axios项目多环境以及部署前后端跨域

Vue+Vite+Axios项目多环境以及部署前后端跨域

2024年11月25日 Javascript 我要评论
最近在前端多环境和部署服务器之后出现的跨域的问题。多环境前端多环境 vite axios1.首先在项目目录下定义多环境的文件。这里列举开发环境和发布环境.env.development 环境# 开发时

最近在前端多环境和部署服务器之后出现的跨域的问题。

多环境

前端多环境 vite axios

1.首先在项目目录下定义多环境的文件。

这里列举开发环境和发布环境

.env.development 环境

# 开发时加载

// 此处为开发时接口
vite_api_url = 'http://localhost:8080/api'

.env production 环境

# 发布时加载

// 生产时接口
vite_api_url = 'http://xxxxxxxxxxx/api'  线上后端地址

2. 在配置的 axios 识别环境

const myaxios = axios.create({
    //识别环境
    baseurl: import.meta.env.vite_api_url as any,
    timeout: 5000,
    headers: { 'content-type': 'application/json;charset=utf-8' },
    // @ts-ignore
    //跨域
    changeorigin: true
});

3. 项目因为使用的是 vite 打包构建,所以在package文件下的 vite 的 build 命令加上 production

"scripts": {
    "dev": "vite",
    "build": "vite build --mode production",
    "preview": "vite preview"
  },

后端多环境 spring boot

创建 application-prod.yml 文件,配置信息为线上环境的地址,比如数据库,redis等

#项目名称,此处是spring boot 2.5版本之后的写法,之前的写法不能识别
spring:
  config:
    activate:
      on-profile:
        prod
  application:
    name: guanlixitong
    #数据库配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.driver
    username: dazi
    password: 123456
    url: jdbc:mysql://localhost:3306/dazi
  #sesson 失效时间 86400秒
  session:
    timeout: 86400  
    store-type: redis

部署命令

java -jar ./{项目打包之后的 jar 包名称,比如maven打包之后target里的 jar 包} --spring.profiles.active=prod

项目启动日志

info 14040 --- [           main] c.p.d.usercenterbackendapplication       : the following 1 profile is active: "prod"
info 14040 --- [           main] o.a.catalina.core.aprlifecyclelistener   : apr capabilities: ipv6 [true], sendfile [true], accept filters [false], random [true], uds [true].   
info 14040 --- [           main] o.a.catalina.core.aprlifecyclelistener   : apr/openssl configuration: useaprconnector [false], useopenssl [true]
info 14040 --- [           main] o.a.catalina.core.aprlifecyclelistener   : openssl successfully initialized [openssl 1.1.1v  1 aug 2023]
info 14040 --- [           main] o.apache.catalina.core.standardservice   : starting service [tomcat]

可以看到识别到了 prod 环境,后端测试也可以发现能够连接上线上数据库了。

(后来线上测试发现 redis 能连上,也能存储数据,但是不能识别登录状态,头疼)

跨域

参考文档:springboot设置cors跨域的四种方式 

官方文档:spring 和 cors 跨域 - spring 中文网 (springdoc.cn)

1. nginx 配置

#跨域配置
location ^~ /api/ {
    proxy_pass http://127.0.0.1:8080;   #反向代理配置
    add_header 'access-control-allow-origin' $http_origin; #预检查请求也需要这行
    add_header 'access-control-allow-credentials' 'true';
    add_header access-control-allow-methods 'get, post, options';
    add_header access-control-allow-headers '*';
	  if ($request_method = 'options'){
        add_header 'access-control-allow-credentials' 'true';
        add_header 'access-control-allow-origin' $http_origin;
        add_header 'access-control-allow-methods' 'get, post, options';
        add_header 'access-control-allow-headers' 'dnt,user-agent,x-requested-with,if-modified-since,cache-control,content-type,range';
        add_header 'access-control-max-age' 1728000;
        add_header 'content-type' 'text/plain; charset=utf-8';
        add_header 'content-length' 0;
        return 204;
    }
}

2. 后端 @crossorigin 注解

在 controller 文件加上注解

@crossoringin(origins = {允许跨域的地址}, methods = {可以跨域的请求方式}, allowcredentials = "true")

3. 添加 web 全局请求拦截器

//新建config目录,新建在该目录下
@configuration
public class webmvcconfg implements webmvcconfigurer {
 
    @override
    public void addcorsmappings(corsregistry registry) {
        //设置允许跨域的路径
        registry.addmapping("/**")
                //设置允许跨域请求的域名
                //当**credentials为true时,**origin不能为星号,需为具体的ip地址【如果接口不带cookie,ip无需设成具体ip】
                .allowedorigins("http://localhost:9527", "http://127.0.0.1:9527", "http://127.0.0.1:8082", "http://127.0.0.1:8083")
                //是否允许证书 不再默认开启
                .allowcredentials(true)
                .allowedheaders(corsconfiguration.all)
                //设置允许的方法
                .allowedmethods(corsconfiguration.all)
                //跨域允许时间
                .maxage(3600);
    }
}

二选一即可
---------------------------------------------------------------
//spring 中文网
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 webmvcconfiguration implements webmvcconfigurer{
    @override
    public void addcorsmappings(corsregistry registry) {
        registry.addmapping("/**")                  // 允许跨域请求的path,支持路径通配符,如:/api/**
            .allowedorigins("*")                    // 允许发起请求的源
            .allowedheaders("*")                    // 允许客户端的提交的 header,通配符 * 可能有浏览器兼容问题
            .allowedmethods("get")                  // 允许客户端使用的请求方法
            .allowcredentials(false)                // 不允许携带凭证
            .exposedheaders("x-auth-token, x-foo")  // 允许额外访问的 response header
            .maxage(3600)                           // 预检缓存一个小时
            ;
    }
}

4. corsfilter

import java.time.duration;

import org.springframework.boot.web.servlet.filterregistrationbean;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.core.ordered;
import org.springframework.http.httpheaders;
import org.springframework.util.stringutils;
import org.springframework.web.cors.corsconfiguration;
import org.springframework.web.filter.corsfilter;
import org.springframework.web.servlet.config.annotation.webmvcconfigurer;

@configuration
public class webmvcconfiguration implements webmvcconfigurer{

    // 通过 filterregistrationbean 注册 corsfilter
    @bean
    public filterregistrationbean<corsfilter> corsfilter() {
        
        // 跨域 filter
        corsfilter corsfilter = new corsfilter(request -> {
            
            // 请求源
            string origin = request.getheader(httpheaders.origin);
            
            if (!stringutils.hastext(origin)) {
                return null; // 非跨域请求
            }
            
            // 针对每个请求,编程式设置跨域
            corsconfiguration config = new corsconfiguration();
            
            // 允许发起跨域请求的源,直接取 origin header 值,不论源是哪儿,服务器都接受
            config.addallowedorigin(origin);
            
            
            // 允许客户端的请求的所有 header
            string headers = request.getheader(httpheaders.access_control_request_headers);
            if (stringutils.hastext(headers)) {
                config.setallowedheaders(stream.of(headers.split(",")).map(string::trim).distinct().tolist());
            }
            
            // 允许客户端的所有请求方法
            config.addallowedmethod(request.getheader(httpheaders.access_control_request_method));
            // 允许读取所有 header
            // 注意,"*" 通配符,可能在其他低版本浏览中不兼容。
            config.addexposedheader("*");
            // 缓存30分钟
            config.setmaxage(duration.ofminutes(30));
            // 允许携带凭证
            config.setallowcredentials(true);
            return config;
        });
        
        filterregistrationbean<corsfilter> bean = new filterregistrationbean<>(corsfilter);
        bean.addurlpatterns("/*");                  // filter 拦截路径
        bean.setorder(ordered.lowest_precedence);   // 保证最先执行
        return bean;
    }
}

可能出现的问题

//报错信息
the 'access-control-allow-origin' header contains multiple values'*, *', but only one is allowed.

域名冲突,可能是上述配置跨域重复,比如 nginx 配置和后端配置,只需要删除某一个即可,比如 nginx 配置中的关于请求头,请求方法等,具体看实际测试情况。

上述配置 最后选择了 nginx 配置,注解在开发时有效,但是一部署线上之后就不生效,原因不知。其他的或多或少会报错,比如 get 请求不跨域,post 请求就跨域。。。

到此这篇关于vue+vite+axios项目多环境以及部署前后端跨域的文章就介绍到这了,更多相关vue vite axios 多环境及跨域内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com