Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhangjian1949
microservices-platform
提交
647b54f2
microservices-platform
项目概览
zhangjian1949
/
microservices-platform
与 Fork 源项目一致
Fork自
zlt2000 / microservices-platform
通知
6
Star
0
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,体验更适合开发者的 AI 搜索 >>
提交
647b54f2
编写于
3月 30, 2020
作者:
zlt2000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
规范异常返回json
上级
588cd5b0
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
194 addition
and
20 deletion
+194
-20
zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/DefaultSecurityHandlerConfig.java
...al/oauth2/common/config/DefaultSecurityHandlerConfig.java
+2
-3
zlt-commons/zlt-common-core/src/main/java/com/central/common/model/Result.java
...n-core/src/main/java/com/central/common/model/Result.java
+6
-11
zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/ResponseUtil.java
.../src/main/java/com/central/common/utils/ResponseUtil.java
+20
-3
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAccessDeniedHandler.java
...ava/com/central/gateway/auth/JsonAccessDeniedHandler.java
+1
-1
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAuthenticationEntryPoint.java
...om/central/gateway/auth/JsonAuthenticationEntryPoint.java
+1
-1
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/error/CustomErrorWebFluxAutoConfiguration.java
...al/gateway/error/CustomErrorWebFluxAutoConfiguration.java
+75
-0
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/error/JsonErrorWebExceptionHandler.java
...m/central/gateway/error/JsonErrorWebExceptionHandler.java
+88
-0
zlt-uaa/src/main/java/com/central/oauth/filter/OauthTokenAspect.java
.../main/java/com/central/oauth/filter/OauthTokenAspect.java
+1
-1
未找到文件。
zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/DefaultSecurityHandlerConfig.java
浏览文件 @
647b54f2
...
...
@@ -4,7 +4,6 @@ import com.central.common.utils.ResponseUtil;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.security.access.AccessDeniedException
;
import
org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler
;
import
org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler
;
...
...
@@ -30,7 +29,7 @@ public class DefaultSecurityHandlerConfig {
*/
@Bean
public
AuthenticationEntryPoint
authenticationEntryPoint
()
{
return
(
request
,
response
,
authException
)
->
ResponseUtil
.
response
Writer
(
objectMapper
,
response
,
authException
.
getMessage
(),
HttpStatus
.
UNAUTHORIZED
.
valu
e
());
return
(
request
,
response
,
authException
)
->
ResponseUtil
.
response
Failed
(
objectMapper
,
response
,
authException
.
getMessag
e
());
}
@Bean
...
...
@@ -49,7 +48,7 @@ public class DefaultSecurityHandlerConfig {
@Override
public
void
handle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
AccessDeniedException
authException
)
throws
IOException
,
ServletException
{
ResponseUtil
.
response
Writer
(
objectMapper
,
response
,
authException
.
getMessage
(),
HttpStatus
.
FORBIDDEN
.
valu
e
());
ResponseUtil
.
response
Failed
(
objectMapper
,
response
,
authException
.
getMessag
e
());
}
};
}
...
...
zlt-commons/zlt-common-core/src/main/java/com/central/common/model/Result.java
浏览文件 @
647b54f2
...
...
@@ -19,31 +19,26 @@ public class Result<T> implements Serializable {
private
String
resp_msg
;
public
static
<
T
>
Result
<
T
>
succeed
(
String
msg
)
{
return
succeedWith
(
null
,
CodeEnum
.
SUCCESS
.
getCode
(),
msg
);
return
of
(
null
,
CodeEnum
.
SUCCESS
.
getCode
(),
msg
);
}
public
static
<
T
>
Result
<
T
>
succeed
(
T
model
,
String
msg
)
{
return
succeedWith
(
model
,
CodeEnum
.
SUCCESS
.
getCode
(),
msg
);
return
of
(
model
,
CodeEnum
.
SUCCESS
.
getCode
(),
msg
);
}
public
static
<
T
>
Result
<
T
>
succeed
(
T
model
)
{
return
succeedWith
(
model
,
CodeEnum
.
SUCCESS
.
getCode
(),
""
);
return
of
(
model
,
CodeEnum
.
SUCCESS
.
getCode
(),
""
);
}
public
static
<
T
>
Result
<
T
>
succeedWith
(
T
datas
,
Integer
code
,
String
msg
)
{
public
static
<
T
>
Result
<
T
>
of
(
T
datas
,
Integer
code
,
String
msg
)
{
return
new
Result
<>(
datas
,
code
,
msg
);
}
public
static
<
T
>
Result
<
T
>
failed
(
String
msg
)
{
return
failedWith
(
null
,
CodeEnum
.
ERROR
.
getCode
(),
msg
);
return
of
(
null
,
CodeEnum
.
ERROR
.
getCode
(),
msg
);
}
public
static
<
T
>
Result
<
T
>
failed
(
T
model
,
String
msg
)
{
return
failedWith
(
model
,
CodeEnum
.
ERROR
.
getCode
(),
msg
);
return
of
(
model
,
CodeEnum
.
ERROR
.
getCode
(),
msg
);
}
public
static
<
T
>
Result
<
T
>
failedWith
(
T
datas
,
Integer
code
,
String
msg
)
{
return
new
Result
<>(
datas
,
code
,
msg
);
}
}
zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/ResponseUtil.java
浏览文件 @
647b54f2
...
...
@@ -35,7 +35,7 @@ public class ResponseUtil {
* @throws IOException
*/
public
static
void
responseWriter
(
ObjectMapper
objectMapper
,
HttpServletResponse
response
,
String
msg
,
int
httpStatus
)
throws
IOException
{
Result
result
=
Result
.
succeedWith
(
null
,
httpStatus
,
msg
);
Result
result
=
Result
.
of
(
null
,
httpStatus
,
msg
);
responseWrite
(
objectMapper
,
response
,
result
);
}
...
...
@@ -76,11 +76,28 @@ public class ResponseUtil {
* webflux的response返回json对象
*/
public
static
Mono
<
Void
>
responseWriter
(
ServerWebExchange
exchange
,
int
httpStatus
,
String
msg
)
{
Result
result
=
Result
.
succeedWith
(
null
,
httpStatus
,
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
(
result
.
getResp_code
()
));
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
()));
...
...
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAccessDeniedHandler.java
浏览文件 @
647b54f2
...
...
@@ -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
.
response
Writer
(
exchange
,
HttpStatus
.
FORBIDDEN
.
value
(),
e
.
getMessage
());
return
ResponseUtil
.
response
Failed
(
exchange
,
HttpStatus
.
FORBIDDEN
.
value
(),
e
.
getMessage
());
}
}
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/auth/JsonAuthenticationEntryPoint.java
浏览文件 @
647b54f2
...
...
@@ -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
.
response
Writer
(
exchange
,
HttpStatus
.
UNAUTHORIZED
.
value
(),
e
.
getMessage
());
return
ResponseUtil
.
response
Failed
(
exchange
,
HttpStatus
.
UNAUTHORIZED
.
value
(),
e
.
getMessage
());
}
}
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/error/CustomErrorWebFluxAutoConfiguration.java
0 → 100644
浏览文件 @
647b54f2
package
com.central.gateway.error
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
;
import
org.springframework.boot.autoconfigure.web.ResourceProperties
;
import
org.springframework.boot.autoconfigure.web.ServerProperties
;
import
org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.web.reactive.error.ErrorAttributes
;
import
org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.http.codec.ServerCodecConfigurer
;
import
org.springframework.web.reactive.config.WebFluxConfigurer
;
import
org.springframework.web.reactive.result.view.ViewResolver
;
import
java.util.Collections
;
import
java.util.List
;
/**
* 自定义异常处理
*
* @author zlt
* @date 2020/3/30
* <p>
* Blog: https://blog.csdn.net/zlt2000
* Github: https://github.com/zlt2000
*/
@Configuration
@ConditionalOnWebApplication
(
type
=
ConditionalOnWebApplication
.
Type
.
REACTIVE
)
@ConditionalOnClass
(
WebFluxConfigurer
.
class
)
@AutoConfigureBefore
(
WebFluxAutoConfiguration
.
class
)
@EnableConfigurationProperties
({
ServerProperties
.
class
,
ResourceProperties
.
class
})
public
class
CustomErrorWebFluxAutoConfiguration
{
private
final
ServerProperties
serverProperties
;
private
final
ApplicationContext
applicationContext
;
private
final
ResourceProperties
resourceProperties
;
private
final
List
<
ViewResolver
>
viewResolvers
;
private
final
ServerCodecConfigurer
serverCodecConfigurer
;
public
CustomErrorWebFluxAutoConfiguration
(
ServerProperties
serverProperties
,
ResourceProperties
resourceProperties
,
ObjectProvider
<
List
<
ViewResolver
>>
viewResolversProvider
,
ServerCodecConfigurer
serverCodecConfigurer
,
ApplicationContext
applicationContext
)
{
this
.
serverProperties
=
serverProperties
;
this
.
applicationContext
=
applicationContext
;
this
.
resourceProperties
=
resourceProperties
;
this
.
viewResolvers
=
viewResolversProvider
.
getIfAvailable
(
Collections:
:
emptyList
);
this
.
serverCodecConfigurer
=
serverCodecConfigurer
;
}
@Bean
@Order
(
Ordered
.
HIGHEST_PRECEDENCE
)
public
ErrorWebExceptionHandler
errorWebExceptionHandler
(
ErrorAttributes
errorAttributes
)
{
JsonErrorWebExceptionHandler
exceptionHandler
=
new
JsonErrorWebExceptionHandler
(
errorAttributes
,
this
.
resourceProperties
,
this
.
serverProperties
.
getError
(),
this
.
applicationContext
);
exceptionHandler
.
setViewResolvers
(
this
.
viewResolvers
);
exceptionHandler
.
setMessageWriters
(
this
.
serverCodecConfigurer
.
getWriters
());
exceptionHandler
.
setMessageReaders
(
this
.
serverCodecConfigurer
.
getReaders
());
return
exceptionHandler
;
}
}
zlt-gateway/sc-gateway/src/main/java/com/central/gateway/error/JsonErrorWebExceptionHandler.java
0 → 100644
浏览文件 @
647b54f2
package
com.central.gateway.error
;
import
org.springframework.boot.autoconfigure.web.ErrorProperties
;
import
org.springframework.boot.autoconfigure.web.ResourceProperties
;
import
org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler
;
import
org.springframework.boot.web.reactive.error.ErrorAttributes
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.web.reactive.function.server.*
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 自定义异常处理
*
* @author zlt
* @date 2020/3/30
* <p>
* Blog: https://blog.csdn.net/zlt2000
* Github: https://github.com/zlt2000
*/
public
class
JsonErrorWebExceptionHandler
extends
DefaultErrorWebExceptionHandler
{
public
JsonErrorWebExceptionHandler
(
ErrorAttributes
errorAttributes
,
ResourceProperties
resourceProperties
,
ErrorProperties
errorProperties
,
ApplicationContext
applicationContext
)
{
super
(
errorAttributes
,
resourceProperties
,
errorProperties
,
applicationContext
);
}
/**
* 获取异常属性
*/
@Override
protected
Map
<
String
,
Object
>
getErrorAttributes
(
ServerRequest
request
,
boolean
includeStackTrace
)
{
Throwable
error
=
super
.
getError
(
request
);
return
responseError
(
this
.
buildMessage
(
request
,
error
));
}
/**
* 指定响应处理方法为JSON处理的方法
* @param errorAttributes
*/
@Override
protected
RouterFunction
<
ServerResponse
>
getRoutingFunction
(
ErrorAttributes
errorAttributes
)
{
return
RouterFunctions
.
route
(
RequestPredicates
.
all
(),
this
::
renderErrorResponse
);
}
/**
* 根据code获取对应的HttpStatus
* @param errorAttributes
*/
@Override
protected
HttpStatus
getHttpStatus
(
Map
<
String
,
Object
>
errorAttributes
)
{
int
statusCode
=
(
int
)
errorAttributes
.
get
(
"code"
);
return
HttpStatus
.
valueOf
(
statusCode
);
}
/**
* 构建异常信息
* @param request
* @param ex
* @return
*/
private
String
buildMessage
(
ServerRequest
request
,
Throwable
ex
)
{
StringBuilder
message
=
new
StringBuilder
(
"Failed to handle request ["
);
message
.
append
(
request
.
methodName
());
message
.
append
(
" "
);
message
.
append
(
request
.
uri
());
message
.
append
(
"]"
);
if
(
ex
!=
null
)
{
message
.
append
(
": "
);
message
.
append
(
ex
.
getMessage
());
}
return
message
.
toString
();
}
/**
* 构建返回的JSON数据格式
* @param errorMessage 异常信息
* @return
*/
public
static
Map
<
String
,
Object
>
responseError
(
String
errorMessage
)
{
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"resp_code"
,
1
);
map
.
put
(
"resp_msg"
,
errorMessage
);
map
.
put
(
"datas"
,
null
);
return
map
;
}
}
zlt-uaa/src/main/java/com/central/oauth/filter/OauthTokenAspect.java
浏览文件 @
647b54f2
...
...
@@ -7,7 +7,6 @@ import lombok.extern.slf4j.Slf4j;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.security.authentication.InsufficientAuthenticationException
;
...
...
@@ -65,6 +64,7 @@ public class OauthTokenAspect {
.
body
(
Result
.
succeed
(
body
));
}
}
catch
(
Exception
e
)
{
log
.
error
(
"授权错误"
,
e
);
return
ResponseEntity
.
status
(
HttpStatus
.
BAD_REQUEST
)
.
body
(
Result
.
failed
(
e
.
getMessage
()));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录