背景介紹
客戶需要將專案前後端作為一個整體打包成jar,不使用nginx方式轉發。使用框架是若依前後端分離,後端springboot,前端vue,目的就是把vue打入jar。
前端修改
ruoyi-ui/src/router/index.js檔案 ,將 mode: ‘history’ 改成 mode: ‘hash’
export default new Router({ mode: 'hash', scrollBehavior: () => ({ y: 0 }), routes: constantRoutes })
修改ruoyi-ui/.env.production檔案 將’/prod-api’ 改成’/’
# 生產環境 VUE_APP_BASE_API = '/'
去bin裡build前端專案
後端修改
引入依賴spring-boot-starter-thymeleaf
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
修改yml檔案配置(dev和local可以都改),增加thymeleaf配置
# Spring配置 spring: thymeleaf: prefix: classpath:/dist/ mode: HTML encoding: utf-8 cache: false
修改ResourcesConfig檔案內容,新增以下部分addViewControllers
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/index").setViewName("index.html"); registry.addViewController("/").setViewName("index.html"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); }
修改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/"); }
根據情況配置訪問許可權修改ruoyi-framework專案中的SecurityConfig.java類,配置靜態資源訪問許可權
如果全放行可以不用管,可以把程式碼中的.anyRequest().authenticated()那行註釋掉
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); } }
把前端build完成的dist目錄放到ruoyi-admin專案的resources目錄下
7. 最後去後端打包編譯 package.bat,java -jar執行專案,瀏覽器訪問http://localhost:8080