若依前后端分离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

相关推荐
Mike_jia19 小时前
OpenDeRisk:AI 原生风险智能系统 ——7*24H 应用系统AI数字运维助手(AI-SRE)
前端
码农水水19 小时前
宇树科技Java被问:数据库连接池的工作原理
java·数据库·后端·oracle
朱穆朗19 小时前
electron升级到33.0.x版本后,devtools字体的修改方法
前端·javascript·electron
思成Codes19 小时前
Go语言的多返回值是如何实现的?
开发语言·后端·golang
风象南19 小时前
除了 ELK、Loki,你还应该知道的日志收集方式:Syslog
后端
IT_陈寒19 小时前
Java 21新特性实战:5个必学的性能优化技巧让你的应用提速40%
前端·人工智能·后端
HarrySunCn19 小时前
大夏龙雀DX-CT511N-B实战之路-第1步
前端·单片机·物联网·iot
未来之窗软件服务19 小时前
幽冥大陆(七十七)C# 调用 中文huayan-medium.onnx —东方仙盟练气期
前端·ui·c#·仙盟创梦ide·东方仙盟
古茗前端团队19 小时前
用 NAudio 做一个音频播放器及原理
前端
JaguarJack19 小时前
如何使用 PHP 的 for、while 和 foreach 循环实现极致性能与零 Bug 代码
后端·php