ruoyi-vue は、フロントエンドとバックエンドのプロジェクトを分離して、統合パッケージ化 (フロントエンドとバックエンドの統合パッケージ化) を実現します。

#転載元:https://blog.csdn.net/weixin_43860634/article/details/131401678

注: この記事は、リンク内の操作と、私が実際に構成した後に発見したいくつかの問題を補足するものです。

  1. ruo-vue の最新バージョンをダウンロードしたところ、リンクをたどったところ、localhost:8080 ではページを表示できないことがわかりました。
  2. ページがブラウザーで非現実的であり、500 または 403 をレポートするか、読み込みに失敗し続けます。
    解決方法は?
    前文:
    Vue のフロントエンドを実行するには、nodejs 環境も必要です。同志、教えません。
    私のフロントエンド開発ツールは WebStorm2022 です。
    バージョンは、最新の Zoyi フロントエンドとバックエンド分離プロジェクトです。 gitee から抜粋。https://gitee.com/y_project/RuoYi-Vue

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 ファイルを変更し、モード: 'history' をモード: 'hash' に変更します。

export default new Router({
    
    
  mode: 'hash', // 去掉url中的#
  scrollBehavior: () => ({
    
     y: 0 }),
  routes: constantRoutes
})

2. ruoyi-ui/package.json ファイルを変更します (ここでテストしたことに注意してください。変更しなくても問題ありません。前のページを開くこともできます。このステップの役割としては、わかりません) 私のは 3.25.3 で、最初は変更しましたが、後から特に ^ を削除して別のパッケージを作成し、アクセスできるかどうかを確認しました。これも正常です。

  "dependencies": {
    
    
    "core-js": "^3.8.1",

ここに画像の説明を挿入します

  1. ruoyi-ui/.env.production ファイルを変更し、「/prod-api」を「/」に変更します。これは変更する必要があります。変更しないと、localhost:8080 に直接アクセスしてページを開くことができません。
# 若依管理系统/生产环境 VUE_APP_BASE_API = '/prod-api'
VUE_APP_BASE_API = '/'

パッケージの展開

フロントエンドをパッケージ化したら、dist ディレクトリを手動でコピーしてバックエンドのリソース ディレクトリに配置し、バックエンドの jar パッケージを直接開きます。この時点では、フロントエンドとバックエンドはjar パッケージ。次にバックエンドを実行し、ブラウザに「localhost」と入力します。:8080 で大丈夫です (笑)

ここに画像の説明を挿入します

                                 																									结束了.

おすすめ

転載: blog.csdn.net/qq_37120477/article/details/131704301