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

配置成功!

相关推荐
lizhongxuan3 小时前
Claude Code 防上下文爆炸:源码级深度解析
前端·后端
柳杉4 小时前
震惊!字符串还能这么玩!
前端·javascript
于先生吖5 小时前
SpringBoot+MQTT 无人健身房智能管控系统源码实战
java·spring boot·后端
毕设源码-小云学姐5 小时前
计算机毕业设计springboot网上招聘系统 基于SpringBoot的在线人才对接平台设计与实现 SpringBoot框架下的数字化求职招聘服务系统开发
spring boot·后端·课程设计
是上好佳佳佳呀5 小时前
【前端(五)】CSS 知识梳理:浮动与定位
前端·css
wefly20175 小时前
纯前端架构深度解析:jsontop.cn,JSON 格式化与全栈开发效率平台
java·前端·python·架构·正则表达式·json·php
我命由我123457 小时前
React - 类组件 setState 的 2 种写法、LazyLoad、useState
前端·javascript·react.js·html·ecmascript·html5·js
weyyhdke7 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
自由生长20247 小时前
IndexedDB的观察
前端
IT_陈寒8 小时前
Vite热更新坑了我三天,原来配置要这么写
前端·人工智能·后端