Commit 7302fd93 by jianan

knife4j

parent 243a95c0
...@@ -66,7 +66,13 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -66,7 +66,13 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
"/auth/register", "/auth/register",
"/swagger-ui/**", "/swagger-ui/**",
"/v3/api-docs/**", "/v3/api-docs/**",
"/scrm/test" "/scrm/test",
"/doc.html",
"/webjars/**",
"/favicon.ico",
"/user/api/v3/api-docs",
"/insurance/base/api/v3/api-docs",
"/csf/api/v3/api-docs"
).permitAll() ).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()
......
...@@ -14,6 +14,13 @@ ...@@ -14,6 +14,13 @@
<description>网关模块</description> <description>网关模块</description>
<dependencies> <dependencies>
<!-- knife4j 网关依赖 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId> <artifactId>spring-cloud-starter-gateway</artifactId>
......
...@@ -2,6 +2,7 @@ package com.yd.gateway.config; ...@@ -2,6 +2,7 @@ package com.yd.gateway.config;
import com.yd.gateway.exception.JwtAuthException; import com.yd.gateway.exception.JwtAuthException;
import com.yd.gateway.utils.JwtTokenUtil; import com.yd.gateway.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -9,6 +10,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -9,6 +10,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
...@@ -16,11 +18,27 @@ import reactor.core.publisher.Mono; ...@@ -16,11 +18,27 @@ import reactor.core.publisher.Mono;
* 网关JWT认证过滤器:只做轻量校验(验证token有效性),不查询业务数据 * 网关JWT认证过滤器:只做轻量校验(验证token有效性),不查询业务数据
*/ */
@Configuration @Configuration
@Slf4j
public class GatewayJwtAuthFilterConfig { public class GatewayJwtAuthFilterConfig {
@Autowired @Autowired
private JwtTokenUtil jwtTokenUtil; private JwtTokenUtil jwtTokenUtil;
private AntPathMatcher matcher = new AntPathMatcher();
private static final String[] KNIFE4J_WHITELIST = {
"/doc.html",
"/webjars/**",
"/v3/api-docs/*",
"/swagger-resources/*",
"/swagger-ui/*",
"/auth/v2/api-docs/*",
"/favicon.ico",
"/user/api/v3/api-docs/**",
"/insurance/base/api/v3/api-docs/**",
"/csf/api/v3/api-docs/**"
};
/** /**
* 注册全局过滤器,优先级高于路由过滤器(确保先校验再路由) * 注册全局过滤器,优先级高于路由过滤器(确保先校验再路由)
*/ */
...@@ -65,6 +83,15 @@ public class GatewayJwtAuthFilterConfig { ...@@ -65,6 +83,15 @@ public class GatewayJwtAuthFilterConfig {
return null; return null;
} }
private boolean isKnife4jWhitelist(String requestPath) {
for (String pattern : KNIFE4J_WHITELIST) {
if (matcher.match(pattern, requestPath)) {
return true;
}
}
return false;
}
/** /**
* 判断路径是否在白名单中(无需认证) * 判断路径是否在白名单中(无需认证)
*/ */
...@@ -72,7 +99,8 @@ public class GatewayJwtAuthFilterConfig { ...@@ -72,7 +99,8 @@ public class GatewayJwtAuthFilterConfig {
// 白名单路径:与SecurityWebFilterChain中的配置保持一致 // 白名单路径:与SecurityWebFilterChain中的配置保持一致
return path.startsWith("/auth/") return path.startsWith("/auth/")
|| path.startsWith("/swagger-ui/") || path.startsWith("/swagger-ui/")
|| path.startsWith("/v3/api-docs/"); || path.startsWith("/v3/api-docs/")
|| isKnife4jWhitelist(path);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment