1. 程式人生 > 實用技巧 >zuul轉發springboot服務靜態資源訪問問題

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轉發靜態資源訪問問題並支援分散式服務