切换导航条
此项目
正在载入...
登录
zhangFan
/
gateway
·
提交
转到一个项目
GitLab
转到仪表盘
项目
活动
文件
提交
管道
0
构建
0
图表
里程碑
问题
0
合并请求
0
成员
标记
维基
派生
网络
创建新的问题
下载为
邮件补丁
差异文件
浏览文件
作者
朱兆平
3 years ago
提交
c117b80ef87de102a08a826518f8db8840ea969c
1 个父辈
97f3e4bf
优化基于win或者linux的乱码返回方法
隐藏空白字符变更
内嵌
并排对比
正在显示
4 个修改的文件
包含
115 行增加
和
46 行删除
pom.xml
src/main/java/com/example/gateway/filter/WrapperResponseGlobalFilter.java
src/main/java/com/example/gateway/routerImpl/GBKServerHttpResponseDecorator.java
src/main/java/com/example/gateway/routerImpl/UTF8ServerHttpResponseDecorator.java
pom.xml
查看文件 @
c117b80
...
...
@@ -11,7 +11,7 @@
</parent>
<groupId>
com.example
</groupId>
<artifactId>
gateway
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<version>
1.0-WIN
</version>
<name>
gateway
</name>
<description>
gateway project for Spring Boot
</description>
...
...
src/main/java/com/example/gateway/filter/WrapperResponseGlobalFilter.java
查看文件 @
c117b80
...
...
@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONArray;
import
com.alibaba.fastjson.JSONObject
;
import
com.example.gateway.model.PERMISSION
;
import
com.example.gateway.model.ROLE
;
import
com.example.gateway.routerImpl.GBKServerHttpResponseDecorator
;
import
com.example.gateway.routerImpl.UTF8ServerHttpResponseDecorator
;
import
com.example.gateway.util.JsonToBean
;
import
lombok.extern.slf4j.Slf4j
;
import
org.reactivestreams.Publisher
;
...
...
@@ -85,44 +87,8 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
// }
// };
/**
* 测试返回中文乱码
*/
ServerHttpResponseDecorator
decoratedResponseWindows
=
new
ServerHttpResponseDecorator
(
response
)
{
@Override
public
Mono
<
Void
>
writeWith
(
Publisher
<?
extends
DataBuffer
>
body
)
{
if
(
HttpStatus
.
OK
.
equals
(
getStatusCode
())
&&
body
instanceof
Flux
)
{
Flux
<?
extends
DataBuffer
>
fluxBody
=
Flux
.
from
(
body
);
return
super
.
writeWith
(
fluxBody
.
buffer
().
map
(
dataBuffers
->
{
StringBuffer
stringBuffer
=
new
StringBuffer
();
dataBuffers
.
forEach
(
dataBuffer
->
{
byte
[]
content
=
new
byte
[
dataBuffer
.
readableByteCount
()];
dataBuffer
.
read
(
content
);
DataBufferUtils
.
release
(
dataBuffer
);
String
tempStr
=
new
String
(
content
,
Charset
.
forName
(
"ISO_8859_1"
));
stringBuffer
.
append
(
tempStr
);
});
String
str
=
new
String
(
stringBuffer
);
// String fileKey = "\":\"" + Const.DATA_BASE_FILE_URL_HEAD + Const.M00;
// if (str.contains(fileKey)) {
// //修改
// String value = "\":\"" + Const.SYSTEMS_FILE_URL_HEAD + Const.DATA_BASE_FILE_URL_HEAD + Const.M00;
// str = str.replaceAll(fileKey, value);
// }
byte
[]
content
=
str
.
getBytes
(
Charset
.
forName
(
"ISO_8859_1"
));
return
bufferFactory
().
wrap
(
content
);
}));
}
// if body is not a flux. never got there.
return
super
.
writeWith
(
body
);
}
};
return
chain
.
filter
(
exchange
.
mutate
().
response
(
decoratedResponseWindows
).
build
());
ServerHttpResponseDecorator
decoratedResponseGBK
=
new
GBKServerHttpResponseDecorator
(
response
);
return
chain
.
filter
(
exchange
.
mutate
().
response
(
decoratedResponseGBK
).
build
());
//
// return chain.filter(exchange);
}
...
...
@@ -137,8 +103,6 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
if
(
anonymousUrlFilter
(
request
)){
return
true
;
}
boolean
flag
=
false
;
String
token
=
""
;
if
(!
request
.
getPath
().
toString
().
contains
(
LOGIN
))
{
HttpHeaders
rqHeader
=
request
.
getHeaders
();
...
...
@@ -157,15 +121,14 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
log
.
trace
(
"访问url:[{}]<->权限[{}]"
,
request
.
getPath
().
toString
(),
permission
.
getUrl
());
if
(
pathMatcher
.
match
(
permission
.
getUrl
(),
request
.
getPath
().
toString
()))
{
log
.
info
(
"[FILTER]-[URL:{}]->鉴权成功"
,
request
.
getPath
().
toString
());
flag
=
true
;
break
;
return
true
;
}
}
}
else
{
log
.
info
(
"[FILTER]-没有对应token的redis缓存,鉴权失败"
);
}
log
.
info
(
"[FILTER]-[URL:{}]->鉴权失败"
,
request
.
getPath
().
toString
());
return
f
lag
;
return
f
alse
;
}
public
boolean
anonymousUrlFilter
(
ServerHttpRequest
request
){
...
...
@@ -185,6 +148,7 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
if
(
permissionList
!=
null
&&
!
permissionList
.
isEmpty
()){
for
(
PERMISSION
permission
:
permissionList
)
{
if
(
pathMatcher
.
match
(
permission
.
getUrl
(),
request
.
getPath
().
toString
()))
{
log
.
info
(
"[ANONYMOUS-FILTER-SUCCESS]-匿名者过滤器适配到规则-PATH:[{}]"
,
request
.
getPath
().
toString
());
return
true
;
}
}
...
...
@@ -192,13 +156,13 @@ public class WrapperResponseGlobalFilter implements GlobalFilter, Ordered {
}
}
}
catch
(
Exception
e
){
log
.
error
(
"[ANONYMOUS-FILTER]匿名者过滤规则审核出错->{}"
,
e
.
toString
());
log
.
error
(
"[ANONYMOUS-FILTER
-ERR
]匿名者过滤规则审核出错->{}"
,
e
.
toString
());
return
false
;
}
}
log
.
info
(
"[ANONYMOUS-FILTER
]-匿名者过滤器为适配到符合条件的
规则-PATH:[{}]"
,
request
.
getPath
().
toString
());
log
.
info
(
"[ANONYMOUS-FILTER
-FALSE]-匿名者过滤器未适配到
规则-PATH:[{}]"
,
request
.
getPath
().
toString
());
return
false
;
}
}
...
...
src/main/java/com/example/gateway/routerImpl/GBKServerHttpResponseDecorator.java
0 → 100644
查看文件 @
c117b80
package
com
.
example
.
gateway
.
routerImpl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.reactivestreams.Publisher
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.core.io.buffer.DataBufferFactory
;
import
org.springframework.core.io.buffer.DataBufferUtils
;
import
org.springframework.core.io.buffer.DefaultDataBufferFactory
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.http.server.reactive.ServerHttpResponseDecorator
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.nio.charset.Charset
;
import
java.nio.charset.StandardCharsets
;
@Slf4j
public
class
GBKServerHttpResponseDecorator
extends
ServerHttpResponseDecorator
{
public
GBKServerHttpResponseDecorator
(
ServerHttpResponse
delegate
)
{
super
(
delegate
);
}
@Override
public
Mono
<
Void
>
writeWith
(
Publisher
<?
extends
DataBuffer
>
body
)
{
if
(
HttpStatus
.
OK
.
equals
(
getStatusCode
())
&&
body
instanceof
Flux
)
{
Flux
<?
extends
DataBuffer
>
fluxBody
=
Flux
.
from
(
body
);
return
super
.
writeWith
(
fluxBody
.
buffer
().
map
(
dataBuffers
->
{
StringBuffer
stringBuffer
=
new
StringBuffer
();
dataBuffers
.
forEach
(
dataBuffer
->
{
byte
[]
content
=
new
byte
[
dataBuffer
.
readableByteCount
()];
dataBuffer
.
read
(
content
);
DataBufferUtils
.
release
(
dataBuffer
);
String
tempStr
=
new
String
(
content
,
Charset
.
forName
(
"ISO_8859_1"
));
stringBuffer
.
append
(
tempStr
);
});
String
str
=
new
String
(
stringBuffer
);
// String fileKey = "\":\"" + Const.DATA_BASE_FILE_URL_HEAD + Const.M00;
// if (str.contains(fileKey)) {
// //修改
// String value = "\":\"" + Const.SYSTEMS_FILE_URL_HEAD + Const.DATA_BASE_FILE_URL_HEAD + Const.M00;
// str = str.replaceAll(fileKey, value);
// }
// log.info("[RESPONSE]-{}",str);
byte
[]
content
=
str
.
getBytes
(
Charset
.
forName
(
"ISO_8859_1"
));
return
bufferFactory
().
wrap
(
content
);
}));
}
// if body is not a flux. never got there.
return
super
.
writeWith
(
body
);
}
}
...
...
src/main/java/com/example/gateway/routerImpl/UTF8ServerHttpResponseDecorator.java
0 → 100644
查看文件 @
c117b80
package
com
.
example
.
gateway
.
routerImpl
;
import
lombok.extern.slf4j.Slf4j
;
import
org.reactivestreams.Publisher
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.core.io.buffer.DataBufferFactory
;
import
org.springframework.core.io.buffer.DataBufferUtils
;
import
org.springframework.core.io.buffer.DefaultDataBufferFactory
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.http.server.reactive.ServerHttpResponseDecorator
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Date
;
@Slf4j
public
class
UTF8ServerHttpResponseDecorator
extends
ServerHttpResponseDecorator
{
public
UTF8ServerHttpResponseDecorator
(
ServerHttpResponse
delegate
)
{
super
(
delegate
);
}
@Override
public
Mono
<
Void
>
writeWith
(
Publisher
<?
extends
DataBuffer
>
body
)
{
if
(
body
instanceof
Flux
)
{
Flux
<?
extends
DataBuffer
>
fluxBody
=
(
Flux
<?
extends
DataBuffer
>)
body
;
return
super
.
writeWith
(
fluxBody
.
buffer
().
map
(
dataBuffer
->
{
DataBufferFactory
dataBufferFactory
=
new
DefaultDataBufferFactory
();
DataBuffer
join
=
dataBufferFactory
.
join
(
dataBuffer
);
byte
[]
content
=
new
byte
[
join
.
readableByteCount
()];
join
.
read
(
content
);
//释放掉内存
DataBufferUtils
.
release
(
join
);
String
s
=
new
String
(
content
,
StandardCharsets
.
UTF_8
);
//TODO,s就是response的值,
// cacheService.saveCacheList(path,s,"10");
log
.
info
(
"[RESPONSE]-{}"
,
s
);
byte
[]
uppedContent
=
new
String
(
s
.
getBytes
(),
StandardCharsets
.
UTF_8
).
getBytes
();
return
bufferFactory
().
wrap
(
uppedContent
);
}));
}
// if body is not a flux. never got there.
return
super
.
writeWith
(
body
);
}
}
...
...
请
注册
或
登录
后发表评论