ruoyi-vue前后端分离项目实现一体化打包(前后端合并打包)

#转载自: https://blog.csdn.net/weixin_43860634/article/details/131401678

说明: 该文章是我对链接中的操作,经过自己真实配置之后发现一些问题做的一些补充

  1. 刚拉下来的最新版本的ruo-vue. 按照 链接 之后发现localhost:8080展示不出来页面.

  2. 要不就是页面在浏览器中不现实不是报500就是403. 要不就是一直加载出不来
    怎么解决呢?
    前言:
    要运行vue的前段还需要nodejs环境啊.同志们. 我是不会告诉你们的.
    我的前段开发工具用的是WebStorm2022 .
    版本是从gitee上最新拉下来的若依前后端分离项目.https://gitee.com/y_project/RuoYi-Vue

    有问题加我微信 一起沟通解决
    在这里插入图片描述

改造ruoyi-vue项目

后端改造

1、引入依赖spring-boot-starter-thymeleaf

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、修改yml文件配置,增加thymeleaf配置

# Spring配置
spring:
  thymeleaf:
    prefix: classpath:/dist/
    mode: HTML
    encoding: utf-8
    cache: false

3、修改ruoyi-framework项目中的ResourcesConfig.java类,配置资源跳转

package com.ruoyi.framework.config;

/**
 * 通用配置
 * 
 * @author ruoyi
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;

    @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/");
        
    }

    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index.html");
        registry.addViewController("/").setViewName("index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
    
    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
    }

    /**
     * 跨域配置
     */
    @Bean
    public CorsFilter corsFilter()
    {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 设置访问源地址
        config.addAllowedOrigin("*");
        // 设置访问源请求头
        config.addAllowedHeader("*");
        // 设置访问源请求方法
        config.addAllowedMethod("*");
        // 对接口配置跨域设置
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}

4、修改ruoyi-framework项目中的SecurityConfig.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、修改 ruoyi-ui/src/router/index.js文件 ,将 mode: ‘history’ 改成 mode: ‘hash’

export default new Router({
  mode: 'hash', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

2、修改 ruoyi-ui/package.json文件(注意这里我测试了一下. 不改也行.照样可以打开前段页面.至于这一步 的作用我也不清楚) 我的是3.25.3.本来我改了后来专门把^ 去掉又打了一个包看看是否可以访问. 也是正常的

  "dependencies": {
    "core-js": "^3.8.1",

在这里插入图片描述

  1. 修改ruoyi-ui/.env.production文件 将’/prod-api’ 改成’/’ 这个必须要改要不你直接访问localhost:8080打不开页面
# 若依管理系统/生产环境 VUE_APP_BASE_API = '/prod-api'
VUE_APP_BASE_API = '/'

打包部署

前端打好包之后,手动将dist目录复制,放到后端的resources目录下面即可,然后直接打后端的jar包,此时前后端就在一个jar包里面了 然后运行后端 在浏览器输入 localhost:8080就行了 哈哈

在这里插入图片描述

                                 																									结束了.
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值