背景介绍
项目组组长想去掉nginx,把项目前后端作为一个整体打成jar,然后就只用启动jar,前后端都启动了,使用框架是el-admin前后端分离,后端springboot,前端Vue
目的
把前端编译后的文件dist放入jar包
前端修改
- 打开routers.js文件,添加mode: ‘hash’,按照以下修改
export default new Router({
mode: 'hash',
scrollBehavior: () => ({ y: 0 }),
routes: constantRouterMap
})
- 修改 .env.development 文件,将 ‘http://127.0.0.1:8000’ 改成 ‘/’
VUE_APP_BASE_API = '/'
- 在前端项目的目录下,打开控制台,并打包
npm run build
# 或者
yarn build
- 打好的dist文件在前端项目目录/dist,并且把dist文件移动到src/main/resources目录下
后端修改
- 引入依赖spring-boot-starter-thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 引入依赖spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 先看application.yml使用的是哪个配置文件,我这使用的是application-dev.yml,在此添加thymeleaf配置
spring:
thymeleaf:
prefix: classpath:/dist/
mode: HTML
encoding: utf-8
cache: false
- 找到项目中是否有继承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/");
}
}
- 根据情况配置访问权限修改项目中的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());
}
- 打包
mvn clean package -Dmaven.test.skip=true
- 在服务器或本地启动jar包,访问平台验证http://localhost:8000
java -jar xxx.jar