Vue打包文件dist放在SpringBoot项目下运行(正确实现全过程)

项目开发中,一般我们都会使用SpringBoot+Vue进行前后端开发。

在部署时,会后端启动一个服务,再启动一个nginx,nginx中配置前端打包文件dist进行项目访问。

实际上,我们也可以把打包好的dist目录放在SpringBoot项目下进行部署。

将dist包放入resources下
配置拦截器
java 复制代码
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private TokenInterceptor tokenInterceptor ;
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**");
    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 将根路径 "/" 的请求重定向到 "/index.html"
        registry.addViewController("/").setViewName("forward:/index.html");
        WebMvcConfigurer.super.addViewControllers(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 添加资源处理器,用于映射静态资源路径
        registry.addResourceHandler("/**").addResourceLocations("classpath:/dist/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}

上面代码中的TokenInterceptor是自定义的token拦截器,表示对所有的请求进行token校验。

需要手动加上如果请求地址为"/"时,放行即可。

项目启动

我的项目端口为3000,访问http://localhost:3000

配置成功!

相关推荐
聆风吟º2 分钟前
【Spring Boot 报错已解决】Error creating bean with entityManagerFactory 原因分析与解决方案
java·spring boot·后端
o***74173 分钟前
SpringBoot【十一】mybatis-plus实现多数据源配置,开箱即用!
spring boot·后端·mybatis
S***84883 分钟前
【spring boot】 IDEA 启动springboot项目报missing ServletWebServerFactory
spring boot·后端·intellij-idea
J总裁的小芒果4 分钟前
el-table 假数据合并
javascript·vue.js·elementui
W***D4555 分钟前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
r***93489 分钟前
【JavaEE】Spring Boot 项目创建
java·spring boot·java-ee
VX:Fegn089511 分钟前
计算机毕业设计|基于springboot + vue毕业设计选题管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
h***673715 分钟前
springboot设置多环境配置文件
java·spring boot·后端
VX:Fegn089516 分钟前
计算机毕设|基springboot+Vue的校园打印系统设计与实现
java·前端·javascript·vue.js·spring boot·后端·课程设计
W.Y.B.G17 分钟前
vue3项目中集成天地图使用示例
android·前端