完善API加解密
This commit is contained in:
parent
2868e20c86
commit
8f1ce9a5df
@ -37,7 +37,7 @@ public class CryptoFilter implements Filter {
|
|||||||
HttpServletRequest servletRequest = (HttpServletRequest) request;
|
HttpServletRequest servletRequest = (HttpServletRequest) request;
|
||||||
HttpServletResponse servletResponse = (HttpServletResponse) response;
|
HttpServletResponse servletResponse = (HttpServletResponse) response;
|
||||||
|
|
||||||
boolean encryptFlag = false;
|
boolean responseFlag = false;
|
||||||
ServletRequest requestWrapper = null;
|
ServletRequest requestWrapper = null;
|
||||||
ServletResponse responseWrapper = null;
|
ServletResponse responseWrapper = null;
|
||||||
EncryptResponseBodyWrapper responseBodyWrapper = null;
|
EncryptResponseBodyWrapper responseBodyWrapper = null;
|
||||||
@ -48,24 +48,24 @@ public class CryptoFilter implements Filter {
|
|||||||
if (HttpMethod.PUT.matches(servletRequest.getMethod()) || HttpMethod.POST.matches(servletRequest.getMethod())) {
|
if (HttpMethod.PUT.matches(servletRequest.getMethod()) || HttpMethod.POST.matches(servletRequest.getMethod())) {
|
||||||
// 是否存在加密标头
|
// 是否存在加密标头
|
||||||
String headerValue = servletRequest.getHeader(properties.getHeaderFlag());
|
String headerValue = servletRequest.getHeader(properties.getHeaderFlag());
|
||||||
|
// 获取加密注解
|
||||||
|
ApiEncrypt apiEncrypt = this.getApiEncryptAnnotation(servletRequest);
|
||||||
|
responseFlag = apiEncrypt != null && apiEncrypt.response();
|
||||||
if (StringUtils.isNotBlank(headerValue)) {
|
if (StringUtils.isNotBlank(headerValue)) {
|
||||||
// 请求解密
|
// 请求解密
|
||||||
requestWrapper = new DecryptRequestBodyWrapper(servletRequest, properties.getPrivateKey(), properties.getHeaderFlag());
|
requestWrapper = new DecryptRequestBodyWrapper(servletRequest, properties.getPrivateKey(), properties.getHeaderFlag());
|
||||||
// 获取加密注解
|
} else {
|
||||||
ApiEncrypt apiEncrypt = this.getApiEncryptAnnotation(servletRequest);
|
// 是否有注解,有就报错,没有放行
|
||||||
if (ObjectUtil.isNotNull(apiEncrypt)) {
|
if (ObjectUtil.isNotNull(apiEncrypt)) {
|
||||||
// 响应加密标志
|
HandlerExceptionResolver exceptionResolver = SpringUtils.getBean("handlerExceptionResolver", HandlerExceptionResolver.class);
|
||||||
encryptFlag = apiEncrypt.response();
|
|
||||||
} else {
|
|
||||||
// 是否有注解,有就报错,没有放行
|
|
||||||
HandlerExceptionResolver exceptionResolver = SpringUtils.getBean(HandlerExceptionResolver.class);
|
|
||||||
exceptionResolver.resolveException(
|
exceptionResolver.resolveException(
|
||||||
servletRequest, servletResponse, null,
|
servletRequest, servletResponse, null,
|
||||||
new ServiceException("没有访问权限,请联系管理员授权", HttpStatus.FORBIDDEN));
|
new ServiceException("没有访问权限,请联系管理员授权", HttpStatus.FORBIDDEN));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 判断是否响应加密
|
// 判断是否响应加密
|
||||||
if (encryptFlag) {
|
if (responseFlag) {
|
||||||
responseBodyWrapper = new EncryptResponseBodyWrapper(servletResponse);
|
responseBodyWrapper = new EncryptResponseBodyWrapper(servletResponse);
|
||||||
responseWrapper = responseBodyWrapper;
|
responseWrapper = responseBodyWrapper;
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ public class CryptoFilter implements Filter {
|
|||||||
ObjectUtil.defaultIfNull(requestWrapper, request),
|
ObjectUtil.defaultIfNull(requestWrapper, request),
|
||||||
ObjectUtil.defaultIfNull(responseWrapper, response));
|
ObjectUtil.defaultIfNull(responseWrapper, response));
|
||||||
|
|
||||||
if (encryptFlag) {
|
if (responseFlag) {
|
||||||
servletResponse.reset();
|
servletResponse.reset();
|
||||||
// 对原始内容加密
|
// 对原始内容加密
|
||||||
String encryptContent = responseBodyWrapper.getEncryptContent(
|
String encryptContent = responseBodyWrapper.getEncryptContent(
|
||||||
|
Loading…
Reference in New Issue
Block a user