前言:

项目本身使用的是若依前后端分离版进行开发的。但是部署时要把分离版改成不分离版,将前端项目打包进jar包运行。下面就是改造的过程.....

1、修改前端项目中的配置

  • 修改ruoyi-ui中的.env.production(生产环境的配置)

    //下面两个选一个就行,建议选第二个
    // 本机地址访问
    VUE_APP_BASE_API = '/'
    // 任意地址访问
    VUE_APP_BASE_API = 'http://xxx.xxx.xxx.xxx:8080'
  • 修改ruoyi-ui中的router/index.js,设置mode属性为hash

    export default new Router({
      mode: 'hash',
      scrollBehavior: () => ({ y: 0 }),
      routes: constantRoutes
    })
  • 修改src/layout/components/Navbar.vue的logout方法

    async logout() {
          this.$confirm('确定注销并退出系统吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$store.dispatch('LogOut').then(() => {
              location.href = '/#/index';//将跳转index前加个/#/防止出现定向错误
            })
          }).catch(() => {});
        }
  • 运行命令打包项目

    npm run build:prod

2、修改后端项目配置

  • 修改后端ruoyi-admin/src/main/resources中的application.yml,添加thymeleaf模板引擎配置

    spring:
      # 模板引擎
      thymeleaf:
        mode: HTML
        encoding: utf-8
        cache: false
  • 修改后端ruoyi-admin/pom.xml,增加thymeleaf模板引擎依赖

    <!-- spring-boot-thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  • 修改后端ruoyi-framework/config/ResourcesConfig.java中的 addResourceHandlers,添加静态资源映射地址

    /** 前端静态资源配置 */
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
  • 修改后端ruoyi-framework/config/SecurityConfig.java中的 configure,添加允许访问的地址。

    //静态资源,可匿名访问,主要是添加"/static/**","/index","/*.ico"
    .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**","/static/**","/index","/*.ico").permitAll()
  • 修改后端访问控制处理SysIndexController.java设置对应访问页面。

      @RequestMapping("/")
        public ModelAndView index() {
            return new ModelAndView("index.html");
        }
  • 整合打包后的前端dist静态资源文件到后端

    在ruoyi-admin/src/main/resources下新建templates目录,放静态页面
    在ruoyi-admin/src/main/resources下新建static目录,放静态文件