Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
程序猿小郑
microservices-platform
提交
4be31c40
microservices-platform
项目概览
程序猿小郑
/
microservices-platform
与 Fork 源项目一致
Fork自
zlt2000 / microservices-platform
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
microservices-platform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4be31c40
编写于
5月 05, 2020
作者:
zlt2000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
拆分ResponseUtil‘
上级
540c46e9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
61 addition
and
47 deletion
+61
-47
zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/ResponseUtil.java
.../src/main/java/com/central/common/utils/ResponseUtil.java
+0
-43
zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/WebfluxResponseUtil.java
...in/java/com/central/common/utils/WebfluxResponseUtil.java
+57
-0
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAccessDeniedHandler.java
...ava/com/central/gateway/auth/JsonAccessDeniedHandler.java
+2
-2
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAuthenticationEntryPoint.java
...om/central/gateway/auth/JsonAuthenticationEntryPoint.java
+2
-2
未找到文件。
zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/ResponseUtil.java
浏览文件 @
4be31c40
package
com.central.common.utils
;
import
com.alibaba.fastjson.JSONObject
;
import
com.central.common.model.Result
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.core.io.buffer.DataBufferFactory
;
import
org.springframework.core.io.buffer.DataBufferUtils
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.web.server.ServerWebExchange
;
import
reactor.core.publisher.Mono
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.io.Writer
;
import
java.nio.charset.Charset
;
/**
* @author zlt
...
...
@@ -71,38 +62,4 @@ public class ResponseUtil {
writer
.
flush
();
}
}
/**
* webflux的response返回json对象
*/
public
static
Mono
<
Void
>
responseWriter
(
ServerWebExchange
exchange
,
int
httpStatus
,
String
msg
)
{
Result
result
=
Result
.
of
(
null
,
httpStatus
,
msg
);
return
responseWrite
(
exchange
,
httpStatus
,
result
);
}
public
static
Mono
<
Void
>
responseFailed
(
ServerWebExchange
exchange
,
String
msg
)
{
Result
result
=
Result
.
failed
(
msg
);
return
responseWrite
(
exchange
,
HttpStatus
.
INTERNAL_SERVER_ERROR
.
value
(),
result
);
}
public
static
Mono
<
Void
>
responseFailed
(
ServerWebExchange
exchange
,
int
httpStatus
,
String
msg
)
{
Result
result
=
Result
.
failed
(
msg
);
return
responseWrite
(
exchange
,
httpStatus
,
result
);
}
public
static
Mono
<
Void
>
responseWrite
(
ServerWebExchange
exchange
,
int
httpStatus
,
Result
result
)
{
if
(
httpStatus
==
0
)
{
httpStatus
=
HttpStatus
.
INTERNAL_SERVER_ERROR
.
value
();
}
ServerHttpResponse
response
=
exchange
.
getResponse
();
response
.
getHeaders
().
setAccessControlAllowCredentials
(
true
);
response
.
getHeaders
().
setAccessControlAllowOrigin
(
"*"
);
response
.
setStatusCode
(
HttpStatus
.
valueOf
(
httpStatus
));
response
.
getHeaders
().
setContentType
(
MediaType
.
APPLICATION_JSON_UTF8
);
DataBufferFactory
dataBufferFactory
=
response
.
bufferFactory
();
DataBuffer
buffer
=
dataBufferFactory
.
wrap
(
JSONObject
.
toJSONString
(
result
).
getBytes
(
Charset
.
defaultCharset
()));
return
response
.
writeWith
(
Mono
.
just
(
buffer
)).
doOnError
((
error
)
->
{
DataBufferUtils
.
release
(
buffer
);
});
}
}
zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/WebfluxResponseUtil.java
0 → 100644
浏览文件 @
4be31c40
package
com.central.common.utils
;
import
com.alibaba.fastjson.JSONObject
;
import
com.central.common.model.Result
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.core.io.buffer.DataBufferFactory
;
import
org.springframework.core.io.buffer.DataBufferUtils
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.server.reactive.ServerHttpResponse
;
import
org.springframework.web.server.ServerWebExchange
;
import
reactor.core.publisher.Mono
;
import
java.nio.charset.Charset
;
/**
* @author zlt
* @date 2020/5/5
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
public
class
WebfluxResponseUtil
{
/**
* webflux的response返回json对象
*/
public
static
Mono
<
Void
>
responseWriter
(
ServerWebExchange
exchange
,
int
httpStatus
,
String
msg
)
{
Result
result
=
Result
.
of
(
null
,
httpStatus
,
msg
);
return
responseWrite
(
exchange
,
httpStatus
,
result
);
}
public
static
Mono
<
Void
>
responseFailed
(
ServerWebExchange
exchange
,
String
msg
)
{
Result
result
=
Result
.
failed
(
msg
);
return
responseWrite
(
exchange
,
HttpStatus
.
INTERNAL_SERVER_ERROR
.
value
(),
result
);
}
public
static
Mono
<
Void
>
responseFailed
(
ServerWebExchange
exchange
,
int
httpStatus
,
String
msg
)
{
Result
result
=
Result
.
failed
(
msg
);
return
responseWrite
(
exchange
,
httpStatus
,
result
);
}
public
static
Mono
<
Void
>
responseWrite
(
ServerWebExchange
exchange
,
int
httpStatus
,
Result
result
)
{
if
(
httpStatus
==
0
)
{
httpStatus
=
HttpStatus
.
INTERNAL_SERVER_ERROR
.
value
();
}
ServerHttpResponse
response
=
exchange
.
getResponse
();
response
.
getHeaders
().
setAccessControlAllowCredentials
(
true
);
response
.
getHeaders
().
setAccessControlAllowOrigin
(
"*"
);
response
.
setStatusCode
(
HttpStatus
.
valueOf
(
httpStatus
));
response
.
getHeaders
().
setContentType
(
MediaType
.
APPLICATION_JSON_UTF8
);
DataBufferFactory
dataBufferFactory
=
response
.
bufferFactory
();
DataBuffer
buffer
=
dataBufferFactory
.
wrap
(
JSONObject
.
toJSONString
(
result
).
getBytes
(
Charset
.
defaultCharset
()));
return
response
.
writeWith
(
Mono
.
just
(
buffer
)).
doOnError
((
error
)
->
{
DataBufferUtils
.
release
(
buffer
);
});
}
}
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAccessDeniedHandler.java
浏览文件 @
4be31c40
package
com.central.gateway.auth
;
import
com.central.common.utils.ResponseUtil
;
import
com.central.common.utils.
Webflux
ResponseUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.security.access.AccessDeniedException
;
...
...
@@ -21,6 +21,6 @@ import reactor.core.publisher.Mono;
public
class
JsonAccessDeniedHandler
implements
ServerAccessDeniedHandler
{
@Override
public
Mono
<
Void
>
handle
(
ServerWebExchange
exchange
,
AccessDeniedException
e
)
{
return
ResponseUtil
.
responseFailed
(
exchange
,
HttpStatus
.
FORBIDDEN
.
value
(),
e
.
getMessage
());
return
Webflux
ResponseUtil
.
responseFailed
(
exchange
,
HttpStatus
.
FORBIDDEN
.
value
(),
e
.
getMessage
());
}
}
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAuthenticationEntryPoint.java
浏览文件 @
4be31c40
package
com.central.gateway.auth
;
import
com.central.common.utils.ResponseUtil
;
import
com.central.common.utils.
Webflux
ResponseUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.security.core.AuthenticationException
;
...
...
@@ -21,6 +21,6 @@ import reactor.core.publisher.Mono;
public
class
JsonAuthenticationEntryPoint
implements
ServerAuthenticationEntryPoint
{
@Override
public
Mono
<
Void
>
commence
(
ServerWebExchange
exchange
,
AuthenticationException
e
)
{
return
ResponseUtil
.
responseFailed
(
exchange
,
HttpStatus
.
UNAUTHORIZED
.
value
(),
e
.
getMessage
());
return
Webflux
ResponseUtil
.
responseFailed
(
exchange
,
HttpStatus
.
UNAUTHORIZED
.
value
(),
e
.
getMessage
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录