如何用jar包一键启动前后端分离springboot项目

背景介绍

项目组组长想去掉nginx,把项目前后端作为一个整体打成jar,然后就只用启动jar,前后端都启动了,使用框架是el-admin前后端分离,后端springboot,前端Vue

目的

把前端编译后的文件dist放入jar包

前端修改

  1. 打开routers.js文件,添加mode: ‘hash’,按照以下修改
export default new Router({
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})
  1. 修改 .env.development 文件,将 ‘http://127.0.0.1:8000’ 改成 ‘/’
VUE_APP_BASE_API  = '/'
  1. 在前端项目的目录下,打开控制台,并打包
npm run build
# 或者
yarn build
  1. 打好的dist文件在前端项目目录/dist,并且把dist文件移动到src/main/resources目录下

后端修改

  1. 引入依赖spring-boot-starter-thymeleaf
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 引入依赖spring-boot-starter-web
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. 先看application.yml使用的是哪个配置文件,我这使用的是application-dev.yml,在此添加thymeleaf配置
spring:
  thymeleaf:
    prefix: classpath:/dist/
    mode: HTML
    encoding: utf-8
    cache: false
  1. 找到项目中是否有继承WebMvcConfigurer的Java文件,若没有,则新增一个名为WebConfig.java的java文件,若有,则根据以下内容做相应的修改
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @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 addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/dist/static/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/dist/");
    }
}
  1. 根据情况配置访问权限修改项目中的SecurityConfig.java类,配置静态资源访问权限
@Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // 禁用 CSRF
                .csrf().disable()
                .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)
                // 授权异常
                .exceptionHandling()
                .authenticationEntryPoint(authenticationErrorHandler)
                .accessDeniedHandler(jwtAccessDeniedHandler)

                // 防止iframe 造成跨域
                .and()
                .headers()
                .frameOptions()
                .disable()

                // 不创建会话
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)

                .and()
                .authorizeRequests()
                // 静态资源等等
                .antMatchers(
                        HttpMethod.GET,
                        "/*.html",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/webSocket/**",
                        "/static/**",
                        "/",
                        "/index"
                ).permitAll()
                // swagger 文档
                .antMatchers("/.*").permitAll()
                .antMatchers("/**").permitAll()
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/swagger-resources/**").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/*/api-docs").permitAll()
                // 文件
                .antMatchers("/avatar/**").permitAll()
                .antMatchers("/file/**").permitAll()
                // 阿里巴巴 druid
                .antMatchers("/druid/**").permitAll()
                // 放行OPTIONS请求
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                // 自定义匿名访问所有url放行 : 允许匿名和带权限以及登录用户访问
                .antMatchers(anonymousUrls.toArray(new String[0])).permitAll()
                // 所有请求都需要认证
                .anyRequest().authenticated()
                .and().apply(securityConfigurerAdapter());
    }

  1. 打包
mvn clean package -Dmaven.test.skip=true
  1. 在服务器或本地启动jar包,访问平台验证http://localhost:8000
java -jar xxx.jar
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值