若依前端后打成一个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
    })

    2、修改ruoyi-ui/.env.production文件

    复制代码
    说明:VUE_APP_BASE_API = '/prod-api'是原来,我这前后端加了访问路径如VUE_APP_BASE_API = '/ReportApi'
    #VUE_APP_BASE_API = '/prod-api'
    VUE_APP_BASE_API = '/ReportApi'

    3、修改vue.config.js

二、后端修改

1、ruoyi-admin pom文件加入以下依赖

复制代码
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2、增加配置文件 (dev和local可以都改),增加thymeleaf配置和访问路径配置

复制代码
  #前后端打成一个JAR包配置
  thymeleaf:
    prefix: classpath:/dist/
    mode: HTML
    encoding: utf-8
    cache: false

3、修改ResourcesConfig文件内容,新增以下部分addViewControllers

复制代码
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index.html");
        registry.addViewController("/").setViewName("index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

4、修改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/");
}

5、根据情况配置访问权限修改ruoyi-framework项目中的SecurityConfig.java类,配置静态资源访问权限

复制代码
@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception
    {
        // 注解标记允许匿名访问的url
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = httpSecurity.authorizeRequests();
        permitAllUrl.getUrls().forEach(url -> registry.antMatchers(url).permitAll());

        httpSecurity
                // CSRF禁用,因为不使用session
                .csrf().disable()
                // 禁用HTTP响应标头
                .headers().cacheControl().disable().and()
                // 认证失败处理类
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                // 基于token,所以不需要session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                // 过滤请求
                .authorizeRequests()
                // 对于登录login 注册register 验证码captchaImage 允许匿名访问
                .antMatchers("/login", "/register", "/captchaImage").permitAll()
                // 静态资源,可匿名访问
                .antMatchers(HttpMethod.GET, "/**/**","/**","/index","/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
                .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
                .antMatchers("/test/**","/aicccms/**","/ICD/**").permitAll()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated()
                .and()
                .headers().frameOptions().disable();
        // 添加Logout filter
        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);
    }

6、将前端dist移动到resources目录下

相关推荐
.又是新的一天.3 分钟前
使用IDEA创建Maven版本的web项目以及lombok的使用
java·maven·intellij-idea
zimoyin5 分钟前
Java 快速转 C# 教程
java·开发语言·c#
读心悦7 分钟前
5000 字总结CSS 中的过渡、动画和变换详解
前端·css·tensorflow
Haooog9 分钟前
Java 面向对象详解和JVM底层内存分析
java·jvm
__BMGT()10 分钟前
C++ QT 打开图片
前端·c++·qt
CopyLower20 分钟前
Java在微服务架构中的最佳实践:从设计到部署
java·微服务·架构
oneDay++22 分钟前
# IntelliJ IDEA企业版安装与配置全指南:避坑详解
java·开发语言·经验分享·学习·学习方法
仍然探索未知中1 小时前
前端扫盲HTML
前端·html
supingemail1 小时前
面试之 Java 新特性 一览表
java·面试·职场和发展
星星点点洲1 小时前
【Java】应对高并发的思路
java