Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
practical_train
Commits
cd112ea9
Commit
cd112ea9
authored
Aug 03, 2026
by
wujipeng
Browse files
init
parents
Pipeline
#346
canceled with stages
Changes
327
Pipelines
2
Show whitespace changes
Inline
Side-by-side
practical-business/src/main/resources/污水五级.docx
0 → 100644
View file @
cd112ea9
File added
practical-business/src/main/resources/污水四级.docx
0 → 100644
View file @
cd112ea9
File added
practical-business/src/main/resources/污泥三级.docx
0 → 100644
View file @
cd112ea9
File added
practical-business/src/main/resources/污泥五级.docx
0 → 100644
View file @
cd112ea9
File added
practical-business/src/main/resources/污泥四级.docx
0 → 100644
View file @
cd112ea9
File added
practical-gateway/pom.xml
0 → 100644
View file @
cd112ea9
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
practical-gateway
</artifactId>
<version>
1.0.0
</version>
<parent>
<groupId>
com.platform
</groupId>
<artifactId>
practical-train
</artifactId>
<version>
1.0.0
</version>
</parent>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-gateway -->
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-gateway
</artifactId>
<version>
${spring.cloud.alibaba.version}
</version>
</dependency>
<!--注册中心客户端-->
<dependency>
<groupId>
com.alibaba.cloud
</groupId>
<artifactId>
spring-cloud-starter-alibaba-nacos-discovery
</artifactId>
<version>
${spring.cloud.alibaba.version}
</version>
</dependency>
<!-- 添加必须的响应式依赖 -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-webflux
</artifactId>
</dependency>
<dependency>
<groupId>
com.alibaba.cloud
</groupId>
<artifactId>
spring-cloud-starter-alibaba-nacos-config
</artifactId>
<version>
${spring.cloud.alibaba.version}
</version>
</dependency>
<!--验证码-->
<dependency>
<groupId>
com.github.axet
</groupId>
<artifactId>
kaptcha
</artifactId>
<version>
0.0.9
</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.platform</groupId>-->
<!-- <artifactId>common-core</artifactId>-->
<!-- <version>1.0.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>
com.ruzhoukj
</groupId>
<artifactId>
common-core
</artifactId>
<version>
1.0.3
</version>
</dependency>
<!-- logstash -->
<!-- <dependency>-->
<!-- <groupId>net.logstash.logback</groupId>-->
<!-- <artifactId>logstash-logback-encoder</artifactId>-->
<!-- <version>7.1.1</version>-->
<!-- </dependency>-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-dependencies
</artifactId>
<version>
${spring.boot.version}
</version>
<type>
pom
</type>
<scope>
import
</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.8.1
</version>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
<encoding>
UTF-8
</encoding>
</configuration>
</plugin>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
<version>
${spring.boot.version}
</version>
<configuration>
<mainClass>
com.platform.gateway.PracticalGatewayApplication
</mainClass>
<skip>
false
</skip>
</configuration>
<executions>
<execution>
<id>
repackage
</id>
<goals>
<goal>
repackage
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
practical-gateway/src/main/java/com/platform/gateway/PracticalGatewayApplication.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
@SpringBootApplication
public
class
PracticalGatewayApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
PracticalGatewayApplication
.
class
,
args
);
}
}
practical-gateway/src/main/java/com/platform/gateway/config/IgnoreUrlsConfig.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.config
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.cloud.context.config.annotation.RefreshScope
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
/**
* @Description: 网关白名单配置
* @Author: zhuxiang
**/
@Data
@EqualsAndHashCode
(
callSuper
=
false
)
@Component
@ConfigurationProperties
(
prefix
=
"secure.ignore"
)
@RefreshScope
public
class
IgnoreUrlsConfig
{
private
List
<
String
>
urls
;
}
practical-gateway/src/main/java/com/platform/gateway/config/KaptchaConfiguration.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.config
;
import
com.google.code.kaptcha.impl.DefaultKaptcha
;
import
com.google.code.kaptcha.util.Config
;
import
com.platform.constants.SecurityConstants
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
java.util.Properties
;
/**
* @author zhuxiang
*/
@Configuration
public
class
KaptchaConfiguration
{
private
static
final
String
KAPTCHA_BORDER
=
"kaptcha.border"
;
private
static
final
String
KAPTCHA_TEXTPRODUCER_FONT_COLOR
=
"kaptcha.textproducer.font.color"
;
private
static
final
String
KAPTCHA_TEXTPRODUCER_CHAR_SPACE
=
"kaptcha.textproducer.char.space"
;
private
static
final
String
KAPTCHA_IMAGE_WIDTH
=
"kaptcha.image.width"
;
private
static
final
String
KAPTCHA_IMAGE_HEIGHT
=
"kaptcha.image.height"
;
private
static
final
String
KAPTCHA_TEXTPRODUCER_CHAR_LENGTH
=
"kaptcha.textproducer.char.length"
;
private
static
final
Object
KAPTCHA_IMAGE_FONT_SIZE
=
"kaptcha.textproducer.font.size"
;
private
static
final
Object
KAPTCHA_NOISE_COLOR
=
"kaptcha.noise.color"
;
private
static
final
Object
KAPTCHA_OBSCURIFICATOR_IMPL
=
"kaptcha.obscurificator.impl"
;
/**
* 默认图片样式
*/
private
static
final
String
DEFAULT_OBSCURIFICATOR_IMPL
=
"com.google.code.kaptcha.impl.WaterRipple"
;
/**
* 默认生成图形验证码宽度
*/
private
static
final
String
DEFAULT_IMAGE_WIDTH
=
"120"
;
/**
* 默认生成图像验证码高度
*/
private
static
final
String
DEFAULT_IMAGE_HEIGHT
=
"50"
;
/**
* 文字颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.
*/
private
static
final
String
DEFAULT_COLOR_FONT
=
"blue"
;
/**
* 图片边框
*/
private
static
final
String
DEFAULT_IMAGE_BORDER
=
"no"
;
/**
* 默认图片间隔
*/
private
static
final
String
DEFAULT_CHAR_SPACE
=
"5"
;
/**
* 验证码文字大小
*/
private
static
final
String
DEFAULT_IMAGE_FONT_SIZE
=
"30"
;
@Bean
public
DefaultKaptcha
producer
()
{
Properties
properties
=
new
Properties
();
properties
.
put
(
KAPTCHA_OBSCURIFICATOR_IMPL
,
DEFAULT_OBSCURIFICATOR_IMPL
);
properties
.
put
(
KAPTCHA_BORDER
,
DEFAULT_IMAGE_BORDER
);
properties
.
put
(
KAPTCHA_TEXTPRODUCER_FONT_COLOR
,
DEFAULT_COLOR_FONT
);
properties
.
put
(
KAPTCHA_TEXTPRODUCER_CHAR_SPACE
,
DEFAULT_CHAR_SPACE
);
properties
.
put
(
KAPTCHA_IMAGE_WIDTH
,
DEFAULT_IMAGE_WIDTH
);
properties
.
put
(
KAPTCHA_IMAGE_HEIGHT
,
DEFAULT_IMAGE_HEIGHT
);
properties
.
put
(
KAPTCHA_IMAGE_FONT_SIZE
,
DEFAULT_IMAGE_FONT_SIZE
);
properties
.
put
(
KAPTCHA_TEXTPRODUCER_CHAR_LENGTH
,
SecurityConstants
.
CODE_SIZE
);
properties
.
put
(
KAPTCHA_NOISE_COLOR
,
DEFAULT_COLOR_FONT
);
Config
config
=
new
Config
(
properties
);
DefaultKaptcha
defaultKaptcha
=
new
DefaultKaptcha
();
defaultKaptcha
.
setConfig
(
config
);
return
defaultKaptcha
;
}
}
practical-gateway/src/main/java/com/platform/gateway/config/RateLimiterConfiguration.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.config
;
import
org.springframework.cloud.gateway.filter.ratelimit.KeyResolver
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
reactor.core.publisher.Mono
;
import
java.util.Objects
;
/**
* @author zhuxiang
* 路由限流配置
*/
@Configuration
public
class
RateLimiterConfiguration
{
@Bean
(
value
=
"remoteAddrKeyResolver"
)
public
KeyResolver
remoteAddrKeyResolver
()
{
return
exchange
->
Mono
.
just
(
Objects
.
requireNonNull
(
exchange
.
getRequest
().
getRemoteAddress
()).
getAddress
().
getHostAddress
());
}
}
practical-gateway/src/main/java/com/platform/gateway/config/RouterFunctionConfiguration.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.config
;
import
com.platform.gateway.handler.HystrixFallbackHandler
;
import
lombok.AllArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.http.MediaType
;
import
org.springframework.web.reactive.function.server.RequestPredicates
;
import
org.springframework.web.reactive.function.server.RouterFunction
;
import
org.springframework.web.reactive.function.server.RouterFunctions
;
/**
* @author zhuxiang
* 路由配置信息
*/
@Slf4j
@Configuration
@AllArgsConstructor
public
class
RouterFunctionConfiguration
{
private
final
HystrixFallbackHandler
hystrixFallbackHandler
;
@Bean
public
RouterFunction
routerFunction
()
{
return
RouterFunctions
.
route
(
RequestPredicates
.
path
(
"/fallback"
)
.
and
(
RequestPredicates
.
accept
(
MediaType
.
APPLICATION_JSON_UTF8
)),
hystrixFallbackHandler
);
}
}
practical-gateway/src/main/java/com/platform/gateway/entity/CommonResult.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.entity
;
/**
* @Description: 通用返回对象
* @Author: ZHUXIANG
**/
public
class
CommonResult
<
T
>
{
private
long
code
;
private
String
message
;
private
T
data
;
protected
CommonResult
()
{
}
protected
CommonResult
(
long
code
,
String
message
,
T
data
)
{
this
.
code
=
code
;
this
.
message
=
message
;
this
.
data
=
data
;
}
/**
* 成功返回结果
*
* @param data 获取的数据
*/
public
static
<
T
>
CommonResult
<
T
>
success
(
T
data
)
{
return
new
CommonResult
<
T
>(
ResultCode
.
SUCCESS
.
getCode
(),
ResultCode
.
SUCCESS
.
getMessage
(),
data
);
}
/**
* 成功返回结果
*
* @param data 获取的数据
* @param message 提示信息
*/
public
static
<
T
>
CommonResult
<
T
>
success
(
T
data
,
String
message
)
{
return
new
CommonResult
<
T
>(
ResultCode
.
SUCCESS
.
getCode
(),
message
,
data
);
}
/**
* 失败返回结果
* @param errorCode 错误码
*/
public
static
<
T
>
CommonResult
<
T
>
failed
(
IErrorCode
errorCode
)
{
return
new
CommonResult
<
T
>(
errorCode
.
getCode
(),
errorCode
.
getMessage
(),
null
);
}
/**
* 失败返回结果
* @param errorCode 错误码
* @param message 错误信息
*/
public
static
<
T
>
CommonResult
<
T
>
failed
(
IErrorCode
errorCode
,
String
message
)
{
return
new
CommonResult
<
T
>(
errorCode
.
getCode
(),
message
,
null
);
}
/**
* 失败返回结果
* @param message 提示信息
*/
public
static
<
T
>
CommonResult
<
T
>
failed
(
String
message
)
{
return
new
CommonResult
<
T
>(
ResultCode
.
FAILED
.
getCode
(),
message
,
null
);
}
/**
* 失败返回结果
*/
public
static
<
T
>
CommonResult
<
T
>
failed
()
{
return
failed
(
ResultCode
.
FAILED
);
}
/**
* 参数验证失败返回结果
*/
public
static
<
T
>
CommonResult
<
T
>
validateFailed
()
{
return
failed
(
ResultCode
.
VALIDATE_FAILED
);
}
/**
* 参数验证失败返回结果
* @param message 提示信息
*/
public
static
<
T
>
CommonResult
<
T
>
validateFailed
(
String
message
)
{
return
new
CommonResult
<
T
>(
ResultCode
.
VALIDATE_FAILED
.
getCode
(),
message
,
null
);
}
/**
* 未登录返回结果
*/
public
static
<
T
>
CommonResult
<
T
>
unauthorized
(
T
data
)
{
return
new
CommonResult
<
T
>(
ResultCode
.
UNAUTHORIZED
.
getCode
(),
ResultCode
.
UNAUTHORIZED
.
getMessage
(),
data
);
}
/**
* token即将失效返回
*/
public
static
<
T
>
CommonResult
<
T
>
expToken
(
T
data
)
{
return
new
CommonResult
<
T
>(
ResultCode
.
EXPTOKEN
.
getCode
(),
ResultCode
.
EXPTOKEN
.
getMessage
(),
data
);
}
/**
* 未授权返回结果
*/
public
static
<
T
>
CommonResult
<
T
>
forbidden
(
T
data
)
{
return
new
CommonResult
<
T
>(
ResultCode
.
FORBIDDEN
.
getCode
(),
ResultCode
.
FORBIDDEN
.
getMessage
(),
data
);
}
public
long
getCode
()
{
return
code
;
}
public
void
setCode
(
long
code
)
{
this
.
code
=
code
;
}
public
String
getMessage
()
{
return
message
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
public
T
getData
()
{
return
data
;
}
public
void
setData
(
T
data
)
{
this
.
data
=
data
;
}
}
practical-gateway/src/main/java/com/platform/gateway/entity/GatewayRouteList.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.entity
;
import
lombok.Data
;
import
org.springframework.cloud.gateway.route.RouteDefinition
;
import
java.util.List
;
/**
* 路由定义模型
*
* @author zhuxiang
*/
@Data
public
class
GatewayRouteList
{
private
static
final
long
serialVersionUID
=
1L
;
List
<
RouteDefinition
>
routes
;
}
practical-gateway/src/main/java/com/platform/gateway/entity/IErrorCode.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.entity
;
/**
* @Description: 封装API的错误码
**/
public
interface
IErrorCode
{
long
getCode
();
String
getMessage
();
}
practical-gateway/src/main/java/com/platform/gateway/entity/ResultCode.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.entity
;
/**
* @Description: 枚举了一些常用API操作码
**/
public
enum
ResultCode
implements
IErrorCode
{
SUCCESS
(
200
,
"操作成功"
),
FAILED
(
500
,
"操作失败"
),
VALIDATE_FAILED
(
404
,
"参数检验失败"
),
UNAUTHORIZED
(
401
,
"暂未登录或token已经过期"
),
EXPTOKEN
(
601
,
"token 即将失效请刷新token"
),
FORBIDDEN
(
403
,
"没有相关权限"
);
private
long
code
;
private
String
message
;
private
ResultCode
(
long
code
,
String
message
)
{
this
.
code
=
code
;
this
.
message
=
message
;
}
public
long
getCode
()
{
return
code
;
}
public
String
getMessage
()
{
return
message
;
}
}
practical-gateway/src/main/java/com/platform/gateway/exception/ValidateCodeException.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.exception
;
/**
* @author zhuxiang
*/
public
class
ValidateCodeException
extends
Exception
{
private
static
final
long
serialVersionUID
=
-
7285211528095468156L
;
public
ValidateCodeException
()
{
}
public
ValidateCodeException
(
String
msg
)
{
super
(
msg
);
}
}
practical-gateway/src/main/java/com/platform/gateway/filter/AuthGlobalFilter.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.filter
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.nimbusds.jose.JWSObject
;
import
com.platform.utils.StringUtils
;
import
lombok.extern.slf4j.Slf4j
;
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.web.server.ServerWebExchange
;
import
reactor.core.publisher.Mono
;
import
java.text.ParseException
;
import
java.util.Objects
;
/**
* @Description: 将登录用户的JWT转化成用户信息的全局过滤器
* @Author: zhuxiang
**/
@Slf4j
@Component
public
class
AuthGlobalFilter
implements
GlobalFilter
,
Ordered
{
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
String
token
=
exchange
.
getRequest
().
getHeaders
().
getFirst
(
"Authorization"
);
if
(
StringUtils
.
isEmpty
(
token
))
{
return
chain
.
filter
(
exchange
);
}
try
{
//从token中解析用户信息并设置到Header中去
String
realToken
=
Objects
.
requireNonNull
(
token
).
replace
(
"Bearer "
,
""
);
JWSObject
jwsObject
=
JWSObject
.
parse
(
realToken
);
String
payLoad
=
jwsObject
.
getPayload
().
toString
();
JSONObject
json
=
JSON
.
parseObject
(
payLoad
);
String
userId
=
json
.
getString
(
"sub"
);
ServerHttpRequest
request
=
exchange
.
getRequest
().
mutate
().
header
(
"userId"
,
userId
).
build
();
exchange
=
exchange
.
mutate
().
request
(
request
).
build
();
}
catch
(
ParseException
e
)
{
log
.
info
(
"oauth2认证失败"
);
}
return
chain
.
filter
(
exchange
);
}
@Override
public
int
getOrder
()
{
return
0
;
}
}
practical-gateway/src/main/java/com/platform/gateway/filter/BaseRequestGlobalFilter.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.filter
;
import
com.platform.constants.SecurityConstants
;
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 zhuxiang
* <p>
* 全局拦截器,作用所有的微服务
* <p>
* 1. 对请求头中参数进行处理 from 参数进行清洗
* 2. 重写StripPrefix = 1,支持全局
* <p>
* 支持swagger添加X-Forwarded-Prefix header (F SR2 已经支持,不需要自己维护)
*/
@Component
public
class
BaseRequestGlobalFilter
implements
GlobalFilter
,
Ordered
{
/**
* 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
))
.
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
());
return
chain
.
filter
(
exchange
.
mutate
()
.
request
(
newRequest
.
mutate
()
.
build
()).
build
());
}
@Override
public
int
getOrder
()
{
return
-
1000
;
}
}
practical-gateway/src/main/java/com/platform/gateway/filter/HttpBasicGatewayFilter.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.filter
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.cloud.gateway.filter.GatewayFilter
;
import
org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ServerWebExchange
;
/**
* @author zhuxiang
* <p>
* 自定义basic认证,针对特殊场景使用
*/
@Slf4j
@Component
public
class
HttpBasicGatewayFilter
extends
AbstractGatewayFilterFactory
{
@Override
public
GatewayFilter
apply
(
Object
config
)
{
return
(
exchange
,
chain
)
->
{
if
(
hasAuth
(
exchange
))
{
return
chain
.
filter
(
exchange
);
}
else
{
ServerHttpResponse
response
=
exchange
.
getResponse
();
response
.
setStatusCode
(
HttpStatus
.
UNAUTHORIZED
);
response
.
getHeaders
().
add
(
HttpHeaders
.
WWW_AUTHENTICATE
,
"Basic Realm=\"Basic\""
);
return
response
.
setComplete
();
}
};
}
/**
* 简单的basic认证
*
* @param exchange 上下文
* @return 是否有权限
*/
private
boolean
hasAuth
(
ServerWebExchange
exchange
)
{
ServerHttpRequest
request
=
exchange
.
getRequest
();
String
auth
=
request
.
getHeaders
().
getFirst
(
HttpHeaders
.
AUTHORIZATION
);
log
.
info
(
"Basic认证信息为:{}"
,
auth
);
return
true
;
}
}
practical-gateway/src/main/java/com/platform/gateway/filter/IgnoreUrlsRemoveJwtFilter.java
0 → 100644
View file @
cd112ea9
package
com.platform.gateway.filter
;
import
cn.hutool.core.collection.CollUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.nimbusds.jose.JWSObject
;
import
com.platform.cache.constant.CacheConstants
;
import
com.platform.enums.RedisDBType
;
import
com.platform.gateway.config.IgnoreUrlsConfig
;
import
com.platform.gateway.entity.CommonResult
;
import
com.platform.util.RedisUtil
;
import
com.platform.utils.StringUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.jetbrains.annotations.NotNull
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.AntPathMatcher
;
import
org.springframework.util.PathMatcher
;
import
org.springframework.web.server.ServerWebExchange
;
import
org.springframework.web.server.WebFilter
;
import
org.springframework.web.server.WebFilterChain
;
import
reactor.core.publisher.Mono
;
import
javax.annotation.Resource
;
import
java.net.URI
;
import
java.nio.charset.StandardCharsets
;
import
java.text.ParseException
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Objects
;
/**
* @Description: 白名单路径访问时需要移除JWT请求头
* @Author: zhuxiang
**/
@Slf4j
@Component
public
class
IgnoreUrlsRemoveJwtFilter
implements
WebFilter
{
@Resource
private
IgnoreUrlsConfig
ignoreUrlsConfig
;
@NotNull
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
@NotNull
WebFilterChain
chain
)
{
ServerHttpRequest
request
=
exchange
.
getRequest
();
ServerHttpResponse
response
=
exchange
.
getResponse
();
URI
uri
=
request
.
getURI
();
PathMatcher
pathMatcher
=
new
AntPathMatcher
();
//1.判断是否是白名单的url,如果是则移除JWT请求头,并通过过
List
<
String
>
ignoreUrls
=
ignoreUrlsConfig
.
getUrls
();
if
(
CollUtil
.
isNotEmpty
(
ignoreUrls
))
{
for
(
String
ignoreUrl
:
ignoreUrls
)
{
if
(
pathMatcher
.
match
(
ignoreUrl
,
uri
.
getPath
()))
{
request
=
exchange
.
getRequest
().
mutate
().
header
(
"Authorization"
,
""
).
build
();
exchange
=
exchange
.
mutate
().
request
(
request
).
build
();
return
chain
.
filter
(
exchange
);
}
}
}
//2.判断token有误 如果没有返回401
String
token
=
exchange
.
getRequest
().
getHeaders
().
getFirst
(
"Authorization"
);
if
(
StringUtils
.
isEmpty
(
token
))
{
response
.
setStatusCode
(
HttpStatus
.
UNAUTHORIZED
);
response
.
getHeaders
().
add
(
HttpHeaders
.
CONTENT_TYPE
,
MediaType
.
APPLICATION_JSON_VALUE
);
String
body
=
JSON
.
toJSONString
(
CommonResult
.
unauthorized
(
"token 无效"
));
DataBuffer
buffer
=
response
.
bufferFactory
().
wrap
(
body
.
getBytes
(
StandardCharsets
.
UTF_8
));
return
response
.
writeWith
(
Mono
.
just
(
buffer
));
}
try
{
String
realToken
=
Objects
.
requireNonNull
(
token
).
replace
(
"Bearer "
,
""
);
JWSObject
jwsObject
=
JWSObject
.
parse
(
realToken
);
String
payLoad
=
jwsObject
.
getPayload
().
toString
();
JSONObject
json
=
JSON
.
parseObject
(
payLoad
);
//检查token是否过期
String
exp
=
json
.
getString
(
"exp"
);
long
now
=
System
.
currentTimeMillis
();
if
((
now
/
1000
)
>
Long
.
parseLong
(
exp
)){
response
.
setStatusCode
(
HttpStatus
.
UNAUTHORIZED
);
response
.
getHeaders
().
add
(
HttpHeaders
.
CONTENT_TYPE
,
MediaType
.
APPLICATION_JSON_VALUE
);
String
body
=
JSON
.
toJSONString
(
CommonResult
.
unauthorized
(
"token 已过期"
));
DataBuffer
buffer
=
response
.
bufferFactory
().
wrap
(
body
.
getBytes
(
StandardCharsets
.
UTF_8
));
return
response
.
writeWith
(
Mono
.
just
(
buffer
));
}
}
catch
(
ParseException
e
)
{
log
.
error
(
"网关层解析token异常"
);
log
.
error
(
e
.
getMessage
(),
e
);
response
.
setStatusCode
(
HttpStatus
.
UNAUTHORIZED
);
response
.
getHeaders
().
add
(
HttpHeaders
.
CONTENT_TYPE
,
MediaType
.
APPLICATION_JSON_VALUE
);
String
body
=
JSON
.
toJSONString
(
CommonResult
.
unauthorized
(
"token 无效"
));
DataBuffer
buffer
=
response
.
bufferFactory
().
wrap
(
body
.
getBytes
(
StandardCharsets
.
UTF_8
));
return
response
.
writeWith
(
Mono
.
just
(
buffer
));
}
return
chain
.
filter
(
exchange
);
}
}
Prev
1
…
12
13
14
15
16
17
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment