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

背景介绍

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

前端修改

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

后端修改

  1. 引入依赖spring-boot-starter-thymeleaf
xml 复制代码
        <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
  1. 修改yml文件配置(dev和local可以都改),增加thymeleaf配置
yml 复制代码
# Spring配置
spring:
  thymeleaf:
    prefix: classpath:/dist/
    mode: HTML
    encoding: utf-8
    cache: false
  1. 修改ResourcesConfig文件内容,新增以下部分addViewControllers
java 复制代码
    @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内容如下:
java 复制代码
@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()那行注释掉

java 复制代码
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目录下

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

相关推荐
wl85113 分钟前
Vue 入门到实战 七
前端·javascript·vue.js
2501_9032386518 分钟前
自定义登录页面的Spring Security实践
java·后端·spring·个人开发
Enti7c30 分钟前
用 HTML、CSS 和 JavaScript 实现抽奖转盘效果
前端·css
LCG元35 分钟前
Vue.js组件开发-使用Vue3如何实现上传word作为打印模版
前端·vue.js·word
dal118网工任子仪1 小时前
94,【2】buuctf web [安洵杯 2019]easy_serialize_php
android·前端·php
大模型铲屎官2 小时前
HTML5 技术深度解读:本地存储与地理定位的最佳实践
前端·html·html5·本地存储·localstorage·地理定位·geolocation api
一 乐2 小时前
基于vue船运物流管理系统设计与实现(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端·船运系统
m0_528723812 小时前
在React中使用redux
前端·javascript·react.js
傻小胖3 小时前
vue3中customRef的用法以及使用场景
前端·javascript·vue.js
谦谦橘子3 小时前
手把手教你实现一个富文本
前端·javascript