zuul轉發springboot服務靜態資源訪問問題


1、頁面

<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>賬號授權失敗</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="stylesheet" type="text/css" th:href="@{/h5/static/style/style.css}">
</head>
<body>
<div class="auth" style="text-align: center;margin-top: 40%">
    <p class="fail-icon"></p>
    <h5>賬號授權失敗</h5>
    <h6 th:text="${errorMsg}"></h6>
</div>
</body>
</html>
View Code

 請注意:<link rel="stylesheet" type="text/css" th:href="{/h5/static/style/style.css}">

2、java代碼

package com.softcore.msoft.h5.client.config;

import com.softcore.msoft.core.interceptor.AppSignInterceptor;
import com.softcore.msoft.core.interceptor.DebugInterceptor;
import com.softcore.msoft.core.interceptor.FansInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author gaowang
 * @createTime 2020/11/11 17:14
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private DebugInterceptor debugInterceptor;

//    @Autowired
//    private FansInterceptor fansInterceptor;

    /**
     * 攔截器注冊
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(debugInterceptor).addPathPatterns("/**");
    }

    /**
     * 跨域訪問支持
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowCredentials(true).allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH").maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/h5/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
    }
}
View Code
這句話起到路徑映射:registry.addResourceHandler("/h5/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");

3、zuul配置

zuul.routes.h5-client.path=/h5/**
zuul.routes.h5-client.strip-prefix=false
zuul.routes.h5-client.serviceId=msoft-h5-client

 如此可以解決zuul轉發靜態資源訪問問題並支持分布式服務

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM