|
@@ -18,6 +18,7 @@ import org.springframework.web.servlet.HandlerExecutionChain;
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.io.PrintWriter;
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -37,7 +38,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 +49,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());
|
|
- // 获取加密注解
|
|
|
|
- ApiEncrypt apiEncrypt = this.getApiEncryptAnnotation(servletRequest);
|
|
|
|
|
|
+ } else {
|
|
|
|
+ // 是否有注解,有就报错,没有放行
|
|
if (ObjectUtil.isNotNull(apiEncrypt)) {
|
|
if (ObjectUtil.isNotNull(apiEncrypt)) {
|
|
- // 响应加密标志
|
|
|
|
- encryptFlag = apiEncrypt.response();
|
|
|
|
- } else {
|
|
|
|
- // 是否有注解,有就报错,没有放行
|
|
|
|
- HandlerExceptionResolver exceptionResolver = SpringUtils.getBean(HandlerExceptionResolver.class);
|
|
|
|
|
|
+ HandlerExceptionResolver exceptionResolver = SpringUtils.getBean("handlerExceptionResolver", 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 +77,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(
|