mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-10 21:19:00 +08:00
♻️ 重构代码。重构网关代码,缩减配置filter
This commit is contained in:
1 parent
c5bf52b8ab
commit
622cc31b33
5 files changed
+95
-69
No files matched your search
@@ -20,7 +20,7 @@
|
||||
package com.pig4cloud.pigx.auth.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.pig4cloud.pigx.common.security.mobile.MobileLoginSuccessHandler;
|
||||
import com.pig4cloud.pigx.common.security.handle.MobileLoginSuccessHandler;
|
||||
import com.pig4cloud.pigx.common.security.mobile.MobileSecurityConfigurer;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUserDetailsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
package com.pig4cloud.pigx.common.security.mobile;
|
||||
package com.pig4cloud.pigx.common.security.handle;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
@@ -14,26 +14,18 @@ spring:
|
||||
- ImageCodeGatewayFilter
|
||||
# 前端密码解密
|
||||
- PasswordDecoderFilter
|
||||
- StripPrefix=1
|
||||
#UPMS 模块
|
||||
- id: pigx-upms
|
||||
uri: lb://pigx-upms
|
||||
predicates:
|
||||
- Path=/admin/**
|
||||
filters:
|
||||
# Swagger请求头处理
|
||||
- SwaggerHeaderFilter
|
||||
# 请求头清理避免不合法请求头
|
||||
- name: RemoveRequestHeader
|
||||
args:
|
||||
name: from
|
||||
# 限流配置
|
||||
- name: RequestRateLimiter
|
||||
args:
|
||||
key-resolver: '#{@remoteAddrKeyResolver}'
|
||||
redis-rate-limiter.replenishRate: 10
|
||||
redis-rate-limiter.burstCapacity: 20
|
||||
- StripPrefix=1
|
||||
# 降级配置
|
||||
- name: Hystrix
|
||||
args:
|
||||
@@ -44,22 +36,17 @@ spring:
|
||||
uri: lb://pigx-codegen
|
||||
predicates:
|
||||
- Path=/gen/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
# 定时任务模块
|
||||
- id: pigx-daemon
|
||||
uri: lb://pigx-daemon
|
||||
predicates:
|
||||
- Path=/daemon/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
# 分布式事务管理模块
|
||||
- id: pigx-tx-manager
|
||||
uri: lb://pigx-tx-manager
|
||||
predicates:
|
||||
- Path=/tx/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
|
||||
security:
|
||||
encode:
|
||||
# 前端密码密钥,必须16位
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.gateway.filter;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.gateway.config.SwaggerProvider;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.addOriginalRequestUrl;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2018/10/8
|
||||
* <p>
|
||||
* 全局拦截器,作用所有的微服务
|
||||
* <p>
|
||||
* 1. 对请求头中参数进行处理 from 参数进行清洗
|
||||
* 2. 重写StripPrefix = 1,支持全局
|
||||
* 3. 支持swagger添加X-Forwarded-Prefix header
|
||||
*/
|
||||
@Component
|
||||
public class PigxRequestGlobalFilter implements GlobalFilter, Ordered {
|
||||
private static final String HEADER_NAME = "X-Forwarded-Prefix";
|
||||
|
||||
/**
|
||||
* Process the Web request and (optionally) delegate to the next
|
||||
* {@code WebFilter} through the given {@link GatewayFilterChain}.
|
||||
*
|
||||
* @param exchange the current server exchange
|
||||
* @param chain provides a way to delegate to the next filter
|
||||
* @return {@code Mono<Void>} to indicate when request processing is complete
|
||||
*/
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
// 1. 清洗请求头中from 参数
|
||||
ServerHttpRequest request = exchange.getRequest().mutate()
|
||||
.headers(httpHeaders -> httpHeaders.remove(SecurityConstants.FROM_IN))
|
||||
.build();
|
||||
|
||||
// 2. 重写StripPrefix
|
||||
addOriginalRequestUrl(exchange, request.getURI());
|
||||
String rawPath = request.getURI().getRawPath();
|
||||
String newPath = "/" + Arrays.stream(StringUtils.tokenizeToStringArray(rawPath, "/"))
|
||||
.skip(1L).collect(Collectors.joining("/"));
|
||||
ServerHttpRequest newRequest = request.mutate()
|
||||
.path(newPath)
|
||||
.build();
|
||||
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());
|
||||
|
||||
// 3. 支持swagger添加X-Forwarded-Prefix header
|
||||
String path = newRequest.getURI().getPath();
|
||||
if (!StringUtils.endsWithIgnoreCase(path, SwaggerProvider.API_URI)) {
|
||||
return chain.filter(exchange.mutate().request(newRequest).build());
|
||||
}
|
||||
String basePath = path.substring(0, path.lastIndexOf(SwaggerProvider.API_URI));
|
||||
return chain.filter(exchange.mutate()
|
||||
.request(newRequest.mutate()
|
||||
.header(HEADER_NAME, basePath)
|
||||
.build()).build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return -1000;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2025, lengleng All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the pig4cloud.com developer nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.gateway.filter;
|
||||
|
||||
import com.pig4cloud.pigx.gateway.config.SwaggerProvider;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
/**
|
||||
* @author Sywd
|
||||
* 添加X-Forwarded-Prefix header
|
||||
*/
|
||||
@Component
|
||||
public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory {
|
||||
private static final String HEADER_NAME = "X-Forwarded-Prefix";
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Object config) {
|
||||
return (exchange, chain) -> {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getURI().getPath();
|
||||
if (!StringUtils.endsWithIgnoreCase(path, SwaggerProvider.API_URI)) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
String basePath = path.substring(0, path.lastIndexOf(SwaggerProvider.API_URI));
|
||||
|
||||
|
||||
ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build();
|
||||
ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
|
||||
return chain.filter(newExchange);
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user