Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
platform
/
platform-gateway
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 309fac94
authored
Apr 19, 2023
by
刘宇峰
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
将组织标识,token放到请求头
1 parent
0ad2d894
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
21 deletions
pom.xml
src/main/java/cn/com/glsx/gateway/filter/JWTAuthGlobalFilter.java
src/main/java/cn/com/glsx/gateway/filter/OpenApiAuthCheckFilter.java
pom.xml
View file @
309fac9
...
...
@@ -40,11 +40,11 @@
<groupId>
com.glsx
</groupId>
<artifactId>
rest-admin-jwt
</artifactId>
</dependency>
<dependency
>
<groupId>
com.github.xiaoymin
</groupId
>
<artifactId>
knife4j-gateway-spring-boot-starter
</artifactId
>
<version>
4.1.0
</version
>
</dependency
>
<!-- <dependency>--
>
<!-- <groupId>com.github.xiaoymin</groupId>--
>
<!-- <artifactId>knife4j-gateway-spring-boot-starter</artifactId>--
>
<!-- <version>4.1.0</version>--
>
<!-- </dependency>--
>
<!--内部缓存框架统一采用caffeine-->
<!--这样Spring cloud loadbalancer用的本地实例缓存也是基于Caffeine-->
...
...
src/main/java/cn/com/glsx/gateway/filter/JWTAuthGlobalFilter.java
View file @
309fac9
package
cn
.
com
.
glsx
.
gateway
.
filter
;
import
cn.com.glsx.auth.config.ApiGatewayServiceConfig
;
import
cn.com.glsx.auth.model.GatewayService
;
import
cn.com.glsx.auth.utils.GatewayJwtUtils
;
import
cn.com.glsx.gateway.common.Constants
;
import
com.alibaba.fastjson.JSON
;
import
com.auth0.jwt.interfaces.DecodedJWT
;
import
com.glsx.plat.common.utils.StringUtils
;
import
com.glsx.plat.core.web.R
;
import
com.glsx.plat.exception.SystemMessage
;
import
cn.com.glsx.gateway.common.Constants
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -49,6 +49,9 @@ public class JWTAuthGlobalFilter implements GlobalFilter, Ordered {
private
GatewayJwtUtils
gatewayJwtUtils
;
@Autowired
private
ApiGatewayServiceConfig
apiGatewayServiceConfig
;
@Autowired
private
StringRedisTemplate
stringRedisTemplate
;
@Override
...
...
@@ -63,10 +66,6 @@ public class JWTAuthGlobalFilter implements GlobalFilter, Ordered {
if
(!
Objects
.
isNull
(
skipAuthUrls
)
&&
Arrays
.
asList
(
skipAuthUrls
).
contains
(
url
))
{
return
chain
.
filter
(
exchange
);
}
GatewayService
gatewayService
=
exchange
.
getAttribute
(
Constants
.
GATEWAY_SERVICE_NAME
);
if
(!
gatewayService
.
getJwtAuth
()){
return
chain
.
filter
(
exchange
);
}
//api接口不需要验证
String
[]
urlPaths
=
url
.
split
(
"/"
);
if
(!
CollectionUtils
.
sizeIsEmpty
(
urlPaths
))
{
...
...
@@ -75,6 +74,13 @@ public class JWTAuthGlobalFilter implements GlobalFilter, Ordered {
return
chain
.
filter
(
exchange
);
}
}
//从请求头中取出org
String
serName
=
exchange
.
getRequest
().
getHeaders
().
getFirst
(
HttpHeaders
.
AUTHORIZATION
+
"-Org"
);
GatewayService
gatewayService
=
apiGatewayServiceConfig
.
getServiceByName
(
serName
);
if
(!
gatewayService
.
getJwtAuth
())
{
return
chain
.
filter
(
exchange
);
}
//从请求头中取出token
String
token
=
exchange
.
getRequest
().
getHeaders
().
getFirst
(
HttpHeaders
.
AUTHORIZATION
);
//未携带token或token在黑名单内
...
...
src/main/java/cn/com/glsx/gateway/filter/OpenApiAuthCheckFilter.java
View file @
309fac9
...
...
@@ -46,8 +46,8 @@ public class OpenApiAuthCheckFilter implements GlobalFilter, Ordered {
@Value
(
"${gateway.skip.urls:#{null}}"
)
private
String
[]
skipAuthUrls
;
@Value
(
"${gateway.
a
pi.urls:#{null}}"
)
private
String
[]
a
piUrls
;
@Value
(
"${gateway.
openA
pi.urls:#{null}}"
)
private
String
[]
openA
piUrls
;
@Autowired
private
GatewayJwtUtils
gatewayJwtUtils
;
...
...
@@ -65,9 +65,9 @@ public class OpenApiAuthCheckFilter implements GlobalFilter, Ordered {
}
//跳过不符合需要验证的路径
if
(!
Objects
.
isNull
(
a
piUrls
))
{
if
(!
Objects
.
isNull
(
openA
piUrls
))
{
AtomicBoolean
flag
=
new
AtomicBoolean
(
false
);
Arrays
.
asList
(
a
piUrls
).
forEach
(
apiUrl
->
{
Arrays
.
asList
(
openA
piUrls
).
forEach
(
apiUrl
->
{
//全匹配或前缀匹配
flag
.
set
(
url
.
equals
(
apiUrl
)
||
url
.
startsWith
(
apiUrl
.
replace
(
"*"
,
""
)));
});
...
...
@@ -104,12 +104,10 @@ public class OpenApiAuthCheckFilter implements GlobalFilter, Ordered {
//解码Token
DecodedJWT
decodedJWT
=
gatewayJwtUtils
.
decode
(
accessToken
);
String
orgName
=
decodedJWT
.
getClaim
(
"name"
).
asString
();
//封装request,传给下一级
ServerWebExchange
webExchange
=
exchange
.
mutate
().
request
(
request
).
build
();
ServerHttpRequest
mutableReq
=
appendRequestHeaders
(
webExchange
,
decodedJWT
.
getId
());
ServerHttpRequest
mutableReq
=
appendRequestHeaders
(
webExchange
,
decodedJWT
.
getId
()
,
accessToken
);
ServerHttpResponse
mutableResp
=
appendResponseHeaders
(
webExchange
,
accessToken
);
ServerWebExchange
mutableExchange
=
webExchange
.
mutate
()
.
request
(
mutableReq
)
...
...
@@ -154,16 +152,19 @@ public class OpenApiAuthCheckFilter implements GlobalFilter, Ordered {
return
buffer
;
}
public
ServerHttpRequest
appendRequestHeaders
(
ServerWebExchange
exchange
,
String
jwtId
)
{
public
ServerHttpRequest
appendRequestHeaders
(
ServerWebExchange
exchange
,
String
jwtId
,
String
authorization
)
{
//将现在的request,添加当前身份
return
exchange
.
getRequest
().
mutate
()
.
header
(
"Authorization-Org"
,
jwtId
)
.
header
(
HttpHeaders
.
AUTHORIZATION
,
authorization
)
.
header
(
HttpHeaders
.
AUTHORIZATION
+
"-Org"
,
jwtId
)
.
build
();
}
public
ServerHttpResponse
appendResponseHeaders
(
ServerWebExchange
exchange
,
String
accessToken
)
{
ServerHttpResponse
mutableResp
=
exchange
.
getResponse
();
mutableResp
.
getHeaders
().
add
(
HttpHeaders
.
AUTHORIZATION
,
accessToken
);
if
(
StringUtils
.
isNotEmpty
(
accessToken
))
{
mutableResp
.
getHeaders
().
add
(
HttpHeaders
.
AUTHORIZATION
,
accessToken
);
}
return
mutableResp
;
}
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment