|
@@ -4,17 +4,18 @@ package com.shkpr.service.customgateway.core.filter;
|
|
|
import com.global.base.log.LogLevelFlag;
|
|
import com.global.base.log.LogLevelFlag;
|
|
|
import com.global.base.log.LogPrintMgr;
|
|
import com.global.base.log.LogPrintMgr;
|
|
|
import com.global.base.tools.FastJsonUtil;
|
|
import com.global.base.tools.FastJsonUtil;
|
|
|
-import com.shkpr.service.customgateway.core.utils.CommTool;
|
|
|
|
|
-import com.shkpr.service.customgateway.core.utils.HttpTool;
|
|
|
|
|
-import com.shkpr.service.customgateway.core.utils.TokenAuthenticationService;
|
|
|
|
|
import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
|
|
import com.shkpr.service.customgateway.core.constants.LogFlagBusiType;
|
|
|
import com.shkpr.service.customgateway.core.constants.ResponseCode;
|
|
import com.shkpr.service.customgateway.core.constants.ResponseCode;
|
|
|
import com.shkpr.service.customgateway.core.storage.GlobalData;
|
|
import com.shkpr.service.customgateway.core.storage.GlobalData;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.utils.CommTool;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.utils.RequestUtil;
|
|
|
|
|
+import com.shkpr.service.customgateway.core.utils.ResponseUtil;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.validation.ConstraintViolation;
|
|
import javax.validation.ConstraintViolation;
|
|
|
import javax.validation.Validator;
|
|
import javax.validation.Validator;
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.util.Enumeration;
|
|
import java.util.Enumeration;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -25,17 +26,18 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
* 该类主要用来校验Controller的request头和request参数
|
|
* 该类主要用来校验Controller的request头和request参数
|
|
|
*/
|
|
*/
|
|
|
public class JWTControllerCheck {
|
|
public class JWTControllerCheck {
|
|
|
|
|
+ private final AtomicInteger mTmpIndex = new AtomicInteger(0);
|
|
|
protected Validator mValidator = null;//由于该类不是@Component或@Bean,所以无法对Validator msValidator使用@Autowired
|
|
protected Validator mValidator = null;//由于该类不是@Component或@Bean,所以无法对Validator msValidator使用@Autowired
|
|
|
protected Map<String, String> mMapURI2Method;
|
|
protected Map<String, String> mMapURI2Method;
|
|
|
protected String mStrThisSampleName = null;
|
|
protected String mStrThisSampleName = null;
|
|
|
- private AtomicInteger mTmpIndex = new AtomicInteger(0);
|
|
|
|
|
- public JWTControllerCheck(Map<String, String> mapURI2Method){
|
|
|
|
|
|
|
+
|
|
|
|
|
+ public JWTControllerCheck(Map<String, String> mapURI2Method) {
|
|
|
this.mMapURI2Method = mapURI2Method;
|
|
this.mMapURI2Method = mapURI2Method;
|
|
|
this.mValidator = GlobalData.getInstance().getValidForParam();
|
|
this.mValidator = GlobalData.getInstance().getValidForParam();
|
|
|
this.mStrThisSampleName = this.getClass().getSimpleName();
|
|
this.mStrThisSampleName = this.getClass().getSimpleName();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public boolean checkRequest(HttpServletRequest request, HttpServletResponse response){
|
|
|
|
|
|
|
+ public boolean checkRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
//解决跨域请求问题
|
|
//解决跨域请求问题
|
|
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
|
response.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
response.setHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT");
|
|
response.setHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT");
|
|
@@ -52,14 +54,11 @@ public class JWTControllerCheck {
|
|
|
return false;
|
|
return false;
|
|
|
if (!checkRequestParam(request, response))
|
|
if (!checkRequestParam(request, response))
|
|
|
return false;
|
|
return false;
|
|
|
- if (!checkRequestBody(request, response))
|
|
|
|
|
- return false;
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ return checkRequestBody(request, response);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- protected boolean checkServerInternet(HttpServletRequest request, HttpServletResponse response){
|
|
|
|
|
|
|
+ protected boolean checkServerInternet(HttpServletRequest request, HttpServletResponse response) {
|
|
|
/*if (!DataTransferMgr.getInstance().isConnected()){
|
|
/*if (!DataTransferMgr.getInstance().isConnected()){
|
|
|
response.setStatus(ResponseCode.STATUS_SERVER_NET_DISCONN_OR_BUSY.toInt());//服务器端网络异常
|
|
response.setStatus(ResponseCode.STATUS_SERVER_NET_DISCONN_OR_BUSY.toInt());//服务器端网络异常
|
|
|
return false;
|
|
return false;
|
|
@@ -67,7 +66,7 @@ public class JWTControllerCheck {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected boolean checkMaxConcurrentRequest(HttpServletRequest request, HttpServletResponse response){
|
|
|
|
|
|
|
+ protected boolean checkMaxConcurrentRequest(HttpServletRequest request, HttpServletResponse response) {
|
|
|
/*if (DataTransferMgr.getInstance().getLiveUnitClient() > GlobalData.getInstance().getMaxConcurrentRequest()){
|
|
/*if (DataTransferMgr.getInstance().getLiveUnitClient() > GlobalData.getInstance().getMaxConcurrentRequest()){
|
|
|
response.setStatus(ResponseCode.STATUS_MAX_CONCURRENT_REQUEST_COUNT.toInt());//超过了最大并发请求数
|
|
response.setStatus(ResponseCode.STATUS_MAX_CONCURRENT_REQUEST_COUNT.toInt());//超过了最大并发请求数
|
|
|
return false;
|
|
return false;
|
|
@@ -75,77 +74,77 @@ public class JWTControllerCheck {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected boolean checkRequestHead(HttpServletRequest request, HttpServletResponse response){
|
|
|
|
|
|
|
+ protected boolean checkRequestHead(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
boolean bResult = true;
|
|
boolean bResult = true;
|
|
|
if (mMapURI2Method == null)
|
|
if (mMapURI2Method == null)
|
|
|
return bResult;
|
|
return bResult;
|
|
|
|
|
|
|
|
String strOldMethod = mMapURI2Method.get(request.getRequestURI());
|
|
String strOldMethod = mMapURI2Method.get(request.getRequestURI());
|
|
|
- if (strOldMethod == null){
|
|
|
|
|
- for (Map.Entry<String,String> entry:mMapURI2Method.entrySet()){
|
|
|
|
|
|
|
+ if (strOldMethod == null) {
|
|
|
|
|
+ for (Map.Entry<String, String> entry : mMapURI2Method.entrySet()) {
|
|
|
String uri = entry.getKey();
|
|
String uri = entry.getKey();
|
|
|
String method = entry.getValue();
|
|
String method = entry.getValue();
|
|
|
- if (CommTool.isTheThisFormatMatch(uri, request.getRequestURI())){
|
|
|
|
|
|
|
+ if (CommTool.isTheThisFormatMatch(uri, request.getRequestURI())) {
|
|
|
strOldMethod = method;
|
|
strOldMethod = method;
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- if (strOldMethod != null){
|
|
|
|
|
|
|
+ if (strOldMethod != null) {
|
|
|
String submitMehtod = request.getMethod();
|
|
String submitMehtod = request.getMethod();
|
|
|
- submitMehtod = submitMehtod!=null ? submitMehtod.toLowerCase():"xxx";
|
|
|
|
|
|
|
+ submitMehtod = submitMehtod != null ? submitMehtod.toLowerCase() : "xxx";
|
|
|
|
|
|
|
|
- if (submitMehtod.equalsIgnoreCase(strOldMethod)){
|
|
|
|
|
|
|
+ if (submitMehtod.equalsIgnoreCase(strOldMethod)) {
|
|
|
String strContentType = request.getHeader("Content-Type");
|
|
String strContentType = request.getHeader("Content-Type");
|
|
|
- strContentType = strContentType!=null ? strContentType.toLowerCase():"xxx";
|
|
|
|
|
|
|
+ strContentType = strContentType != null ? strContentType.toLowerCase() : "xxx";
|
|
|
String strContent = request.getHeader("Content"); //经测试发现ios只能设置Content
|
|
String strContent = request.getHeader("Content"); //经测试发现ios只能设置Content
|
|
|
- strContent = strContent!=null ? strContent.toLowerCase():"xxx";
|
|
|
|
|
|
|
+ strContent = strContent != null ? strContent.toLowerCase() : "xxx";
|
|
|
|
|
|
|
|
String strAccept = request.getHeader("Accept");
|
|
String strAccept = request.getHeader("Accept");
|
|
|
- strAccept = strAccept!=null ? strAccept.toLowerCase():"xxx";
|
|
|
|
|
|
|
+ strAccept = strAccept != null ? strAccept.toLowerCase() : "xxx";
|
|
|
|
|
|
|
|
- String strCompare = TokenAuthenticationService.HEADER_CONTENT_TYPE;
|
|
|
|
|
|
|
+ String strCompare = "application/json;charset=UTF-8";
|
|
|
strCompare = strCompare.toLowerCase();
|
|
strCompare = strCompare.toLowerCase();
|
|
|
- String strShortCompare = TokenAuthenticationService.HEADER_SHORT_CONTENT_TYPE;
|
|
|
|
|
|
|
+ String strShortCompare = "application/json";
|
|
|
strShortCompare = strShortCompare.toLowerCase();
|
|
strShortCompare = strShortCompare.toLowerCase();
|
|
|
|
|
|
|
|
if (submitMehtod.equals("post")
|
|
if (submitMehtod.equals("post")
|
|
|
|| submitMehtod.equals("delete")
|
|
|| submitMehtod.equals("delete")
|
|
|
- || submitMehtod.equals("put")){
|
|
|
|
|
|
|
+ || submitMehtod.equals("put")) {
|
|
|
//boolean b = strAccept.contains(strCompare);
|
|
//boolean b = strAccept.contains(strCompare);
|
|
|
if (!strContentType.contains(strCompare)
|
|
if (!strContentType.contains(strCompare)
|
|
|
&& !strContentType.contains(strShortCompare)
|
|
&& !strContentType.contains(strShortCompare)
|
|
|
&& !strContent.contains(strCompare)
|
|
&& !strContent.contains(strCompare)
|
|
|
&& !strContent.contains(strShortCompare)
|
|
&& !strContent.contains(strShortCompare)
|
|
|
&& !strAccept.contains(strCompare)
|
|
&& !strAccept.contains(strCompare)
|
|
|
- && !strAccept.contains(strShortCompare)){
|
|
|
|
|
- HttpTool.handlerHttpErrorStatus(response, ResponseCode.STATUS_INVALID_CONTENT_TYPE);//POST/DELETE/PUT时Content-Type不对
|
|
|
|
|
|
|
+ && !strAccept.contains(strShortCompare)) {
|
|
|
|
|
+ ResponseUtil.writeResponseCode(response, ResponseCode.STATUS_INVALID_CONTENT_TYPE, true);//POST/DELETE/PUT时Content-Type不对
|
|
|
bResult = false;
|
|
bResult = false;
|
|
|
}
|
|
}
|
|
|
- }else if (submitMehtod.equals("get")){
|
|
|
|
|
|
|
+ } else if (submitMehtod.equals("get")) {
|
|
|
/*String strAuthToken = request.getHeader("authortoken");
|
|
/*String strAuthToken = request.getHeader("authortoken");
|
|
|
String strUserAgent = request.getHeader(ApiURI.HEADER_USER_AGENT);
|
|
String strUserAgent = request.getHeader(ApiURI.HEADER_USER_AGENT);
|
|
|
if (!StringUtils.isEmpty(strAuthToken)
|
|
if (!StringUtils.isEmpty(strAuthToken)
|
|
|
|| ("apk".equals(CommTool.getPlatformByAgent(strUserAgent)) && !strContentType.contains(strCompare))){
|
|
|| ("apk".equals(CommTool.getPlatformByAgent(strUserAgent)) && !strContentType.contains(strCompare))){
|
|
|
- HttpTool.handlerHttpErrorStatus(response, ResponseCode.STATUS_ACCESS_DENY);
|
|
|
|
|
|
|
+ ResponseUtil.writeResponseCode();(response, ResponseCode.STATUS_ACCESS_DENY);
|
|
|
bResult = false;
|
|
bResult = false;
|
|
|
}*/
|
|
}*/
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (!bResult && mTmpIndex.incrementAndGet()%10==0){
|
|
|
|
|
|
|
+ if (!bResult && mTmpIndex.incrementAndGet() % 10 == 0) {
|
|
|
Map<String, String> mapHeads = new HashMap<String, String>();
|
|
Map<String, String> mapHeads = new HashMap<String, String>();
|
|
|
Enumeration<String> headerNames = request.getHeaderNames();
|
|
Enumeration<String> headerNames = request.getHeaderNames();
|
|
|
while (headerNames.hasMoreElements()) {
|
|
while (headerNames.hasMoreElements()) {
|
|
|
- String key = (String)headerNames.nextElement();
|
|
|
|
|
|
|
+ String key = headerNames.nextElement();
|
|
|
String value = "";
|
|
String value = "";
|
|
|
Enumeration<String> headerValues = request.getHeaders(key);
|
|
Enumeration<String> headerValues = request.getHeaders(key);
|
|
|
- while (headerValues.hasMoreElements()){
|
|
|
|
|
- value = value + (String)headerValues.nextElement();
|
|
|
|
|
|
|
+ while (headerValues.hasMoreElements()) {
|
|
|
|
|
+ value = value + headerValues.nextElement();
|
|
|
}
|
|
}
|
|
|
mapHeads.put(key, value);
|
|
mapHeads.put(key, value);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- String strAddress = HttpTool.getIpAddress(request);
|
|
|
|
|
|
|
+ String strAddress = RequestUtil.getIpAddress(request);
|
|
|
LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_WARN, LogFlagBusiType.BUSI_AUTH.toStrValue(), mStrThisSampleName, "Inner"
|
|
LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_WARN, LogFlagBusiType.BUSI_AUTH.toStrValue(), mStrThisSampleName, "Inner"
|
|
|
, String.format("uri:{%s %s} remote_address:{%s} illegal request for printing... %s"
|
|
, String.format("uri:{%s %s} remote_address:{%s} illegal request for printing... %s"
|
|
|
, submitMehtod
|
|
, submitMehtod
|
|
@@ -153,31 +152,34 @@ public class JWTControllerCheck {
|
|
|
, strAddress
|
|
, strAddress
|
|
|
, FastJsonUtil.toJSON(mapHeads)));
|
|
, FastJsonUtil.toJSON(mapHeads)));
|
|
|
}
|
|
}
|
|
|
- } else{
|
|
|
|
|
- HttpTool.handlerHttpErrorStatus(response, ResponseCode.STATUS_ERROR_REQUEST_METHOD);//URI对应的Method不对
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ResponseUtil.writeResponseCode(response, ResponseCode.STATUS_ERROR_REQUEST_METHOD, true);//URI对应的Method不对
|
|
|
bResult = false;
|
|
bResult = false;
|
|
|
}
|
|
}
|
|
|
- } else{
|
|
|
|
|
- HttpTool.handlerHttpErrorStatus(response, ResponseCode.STATUS_NOT_MATCHED_URI);//找不到对应的URI
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ ResponseUtil.writeResponseCode(response, ResponseCode.STATUS_NOT_MATCHED_URI, true);//找不到对应的URI
|
|
|
bResult = false;
|
|
bResult = false;
|
|
|
}
|
|
}
|
|
|
return bResult;
|
|
return bResult;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- protected boolean checkRequestParam(HttpServletRequest request, HttpServletResponse response){ return true; }
|
|
|
|
|
|
|
+ protected boolean checkRequestParam(HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- protected boolean checkRequestBody(HttpServletRequest request, HttpServletResponse response){
|
|
|
|
|
|
|
+ protected boolean checkRequestBody(HttpServletRequest request, HttpServletResponse response) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public <T> boolean checkObjectByValidator(T object, Class<?>... classForObject){
|
|
|
|
|
- if (mValidator != null){
|
|
|
|
|
|
|
+ public <T> boolean checkObjectByValidator(T object, Class<?>... classForObject) {
|
|
|
|
|
+ if (mValidator != null) {
|
|
|
Set<ConstraintViolation<T>> constraintViolations = mValidator.validate(object, classForObject);
|
|
Set<ConstraintViolation<T>> constraintViolations = mValidator.validate(object, classForObject);
|
|
|
- if (!(constraintViolations == null || constraintViolations.size() <= 0))
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ return constraintViolations == null || constraintViolations.size() <= 0;
|
|
|
}
|
|
}
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public boolean skipCheckAuthToPermit(HttpServletRequest request, HttpServletResponse response){return false;}
|
|
|
|
|
|
|
+ public boolean skipCheckAuthToPermit(HttpServletRequest request, HttpServletResponse response) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|