spring+vue项目打包部署到后端,解决跨域问题

10 篇文章 0 订阅
8 篇文章 0 订阅

问题描述

  • 环境描述
    后端使用springboot+jpa,后端访问端口 8089;前端使用vue开发,前端访问端口8080。
  • 具体问题
    在开发时,前端配置了跨域请求服务,并且前端类似于部署到了服务器上(和webpack相关),所以能够直接访问后端。但是部署到tomcat服务器上就是完全静态的css+js+html了,这时候访问使用微服务启动的项目就导致了跨域问题,因为端口不一致。

问题解决

在xxxApplication.java同级建立CrosConfig.java.内容如下,即可解决跨域访问。

package com.ktvme.bigdata;

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 CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
        corsConfiguration.addAllowedHeader("*"); // 2允许任何头
        corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等)
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4
        return new CorsFilter(source);
    }
}

注意事项

vue前端配置的ip访问地址是直接对axios指定访问的地址。
本地开发时的代理配置config/index.js文件中配置

  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        //target: 'http://192.168.97.36:8088',//后端接口地址
        target: 'http://localhost:8089',
        changeOrigin: true,//是否允许跨越
        pathRewrite: {
            '^/api': '/siteSelection',//重写,
        }
      }
    },

但是在部署到tomcat上这个是无效的。

部署之前 修改 http-service.js,指定访问端口和访问地址。

axios.defaults.baseURL = 'http://192.168.97.216:8089/siteSelection'; //填写域名  /aip

我之前是/api,部署到项目后访问地址就变成了http://192.168.97.216:8080/api/xxxController
因为放在了tomcat下。直接/api就是到根目录下找api文件夹。

其中 不要使用localhost代替ip。

跨域取数据测试代码

对于是否可以跨域取到数据可以使用以下的jquery代码.修改访问地址和参数、请求类型即可。

<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    $(function(){
        $.ajax({
            url:"http://192.168.97.74:8089/data/sq_city",
            data:{"province":"北京市"},
            dataType: "json", // 表示返回类型
            type:"POST",   // 请求方式
            //crossDomain:true,  //是否跨域  可后端设置
            //contentType:"application/json;charset=UTF-8", 
            contentType: "application/x-www-form-urlencoded",
            success:function(data){
                console.log(data);
            }
        });
    });
</script>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
宝塔面板可以通过Nginx来部署Vue项目,并解决跨域问题。在部署过程中,需要进行以下步骤: 1. 首先,使用npm run build命令打包Vue项目,成功后会在项目根目录下生成一个dist文件夹。 2. 在宝塔面板中,选择对应的网站,点击设置,进入网站设置页面。 3. 在左侧菜单中,点击Nginx,进入Nginx配置页面。 4. 在Nginx配置页面的顶部,可以看到一段配置示例,将这段示例替换为以下配置代码: ``` location / { root /path/to/your/dist; # 指向Vue项目打包后的dist文件夹路径 index index.html index.htm; } location /api/ { proxy_pass http://localhost:9777/; # 将请求转发到后端接口的运行端口 } ``` 注意将`/path/to/your/dist`替换为你实际的Vue项目dist文件夹的路径,将`http://localhost:9777/`替换为你实际的后端接口的运行端口。 5. 保存并应用配置。 通过以上步骤,宝塔面板已经成功部署Vue项目,并且解决跨域问题。你可以通过公网IP加上Vue项目的端口号和项目名称来访问部署项目。例如,`公网IP:Vue项目端口号/项目名称`。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [宝塔部署项目&跨域](https://blog.csdn.net/u014632228/article/details/129066281)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Spring Boot + Vue后端分离项目部署](https://blog.csdn.net/qq_45716120/article/details/112985281)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值