场景
现在要对ruoyi-vue前后端分离项目,进行一体化打包,即 将前后端项目打在一个jar里面
一体化打包优点
不需要再使用nginx,直接将前端文件放到后端项目里面
改造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文件
将原来的 “core-js”: “3.8.1” 改为 “core-js”: “^3.8.1”,
"dependencies": { "core-js": "^3.8.1",
打包部署
前端打好包之后,手动将dist目录复制,放到后端的resources目录下面即可,然后直接打后端的jar包,此时前后端就在一个jar包里面了
项目结构是这样的:
至此 ,前后端分离项目实现一体化打包就介绍到这里。