若依前后端分离vue打进jar包操作

背景介绍

客户需要将项目前后端作为一个整体打包成jar,不使用nginx方式转发。使用框架是若依前后端分离,后端springboot,前端vue,目的就是把vue打入jar。

前端修改

  1. ruoyi-ui/src/router/index.js文件 ,将 mode: ‘history’ 改成 mode: ‘hash’
export default new Router({
  mode: 'hash', 
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})
  1. 修改ruoyi-ui/.env.production文件 将’/prod-api’ 改成’/’
# 生产环境
VUE_APP_BASE_API = '/'
  1. 去bin里build前端项目

后端修改

  1. 引入依赖spring-boot-starter-thymeleaf
        <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
  1. 修改yml文件配置(dev和local可以都改),增加thymeleaf配置
# Spring配置
spring:
  thymeleaf:
    prefix: classpath:/dist/
    mode: HTML
    encoding: utf-8
    cache: false

  1. 修改ResourcesConfig文件内容,新增以下部分addViewControllers
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index.html");
        registry.addViewController("/").setViewName("index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
  1. 修改ResourcesConfig文件内容,替换addResourceHandlers内容如下:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    /** 本地文件上传路径 */
    registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");
    /** 页面静态化 */
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/dist/static/");
    /** swagger配置 */
    registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
  1. 根据情况配置访问权限修改ruoyi-framework项目中的SecurityConfig.java类,配置静态资源访问权限

如果全放行可以不用管,可以把代码中的.anyRequest().authenticated()那行注释掉

package com.ruoyi.framework.config;

@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter{
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception    {
        httpSecurity
                // CSRF禁用,因为不使用session
                .csrf().disable()
                // 认证失败处理类
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                // 基于token,所以不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                // 过滤请求
                .authorizeRequests()
                // 对于登录login 验证码captchaImage 允许匿名访问
                .antMatchers("/login", "/captchaImage").anonymous()
                .antMatchers(
                        HttpMethod.GET,
                        "/*.html",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/static/**",
                        "/",
                        "/index"
                ).permitAll()
                .antMatchers("/profile/**").anonymous()
                .antMatchers("/common/download**").anonymous()
                .antMatchers("/common/download/resource**").anonymous()
                .antMatchers("/swagger-ui.html").anonymous()
                .antMatchers("/doc.html").anonymous()
                .antMatchers("/swagger-resources/**").anonymous()
                .antMatchers("/webjars/**").anonymous()
                .antMatchers("/*/api-docs").anonymous()
                .antMatchers("/druid/**").anonymous()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated()
                .and()
                .headers().frameOptions().disable();
        httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
        // 添加JWT filter
        httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        // 添加CORS filter
        httpSecurity.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
        httpSecurity.addFilterBefore(corsFilter, LogoutFilter.class);
    }
}
  1. 把前端build完成的dist目录放到ruoyi-admin项目的resources目录下

image.png
7. 最后去后端打包编译 package.bat,java -jar运行项目,浏览器访问http://localhost:8080

### 若依框架 Vue 3 前后端分离环境搭建教程 #### 准备工作 为了成功搭建若依框架的Vue 3版本前后端分离项目,需先准备好必要的开发工具和依赖项。确保安装了Node.js、npm/yarn以及JDK等基本运行环境[^3]。 #### 下载源码 前往官方仓库获取最新版适用于Vue 3的若依框架源码。注意区分不同分支下的前端技术栈差异,选择对应于Vue 3的分支进行克隆操作[^2]。 #### 配置数据库连接 默认情况下,该框架会尝试连接名为`ry-vue`的本地MySQL实例。可根据实际需求修改位于`admin`项目的`application-druid.yml`文件中的数据库设置部分来适配不同的数据库服务或调整现有链接参数[^4]。 ```yaml spring: datasource: druid: url: jdbc:mysql://localhost:3306/your_database_name?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: your_password_here ``` #### 启动后端服务 进入后端工程目录执行Maven命令构建并启动应用程序: ```bash mvn clean install -DskipTests cd ruoyi-admin java -jar target/*.jar ``` 此时应该可以在浏览器访问http://localhost:8080验证API接口是否正常响应。 #### 构建与启动前端应用 切换到前端项目路径下利用NPM/YARN完成依赖安装及打部署流程: ```bash cd ruoyi-ui yarn install # 或者 npm i yarn serve # 开发模式下热更新预览站点 ``` 当看到提示信息表明编译过程顺利完成之后打开新的标签页输入地址 http://localhost:9527 即可加载由Vue驱动的用户界面。 通过上述步骤便完成了整个基于Spring Boot + Vue 3架构的企业级解决方案——若依框架的初始化配置工作。后续还可以进一步探索更多高级特性和功能模块定制化选项以满足特定业务场景的要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十五001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值