若依前端后打成一个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目录下

相关推荐
非 白8 分钟前
【Java分布式】Nacos注册中心
java·开发语言·nacos·注册中心
Vicky&James12 分钟前
WPF到Web的无缝过渡:英雄联盟客户端项目OpenSilver迁移实战
前端·wpf
吃海鲜的骆驼14 分钟前
服务异步通讯与RabbitMQ
java·分布式·后端·rabbitmq
m0_7482336415 分钟前
RabbitMQ 进阶
android·前端·后端
羱滒17 分钟前
sql调优之数据库开发规范
java·数据库·数据库开发
不想有bug的小菜鸟24 分钟前
vue3使用iframe全屏展示pdf效果
前端·pdf
m0_7482386325 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
u01005596025 分钟前
前端代理,解决跨域问题讲解
前端
quitv30 分钟前
react脚手架配置别名
前端·javascript·react.js
m0_5287238139 分钟前
前端如何进行性能优化
前端·性能优化