我使用下面的代码来验证传入的请求,如果它真的来自 Twilio。
网址是,
我们使用 get & POST 方法。
我们得到期望的签名如下,
String expectedSignature = request.getHeader("X-Twilio-Signature");
请求 URl 是,
String serverUrl = request.getRequestURL().toString()+"/"+request.getQueryString();
// Since we use GET, it will be empty and it is working fine.
Map<String,String> tempParams = new HashMap<String,String>();
TwilioUtils util = new TwilioUtils("AUTH_TOKEN_OF_USER");
boolean validationResult = util.validateRequest(expectedSignature, serverUrl, tempParams);
对于 POST,它不起作用。POST 代码,
- - - - - - 编辑 - - - - - - - - - - -
// Check twilio header ...
String expectedSignature = request.getHeader("X-Twilio-Signature");
// These are the post params twilio sent in its request
Map<String, String> params = null;
String serverUrl = null;
serverUrl = PROTOCOL + "://" + request.getServerName() + request.getRequestURI() + "?" + request.getQueryString();
if (request.getMethod().equalsIgnoreCase("POST")) {
params = new HashMap<String, String>();
Enumeration<String> reqParams = request.getParameterNames();
LOGGER.info("NUMBER OF PARAMS ===>>>> " + request.getParameterMap().size());
int i = 1;
while (reqParams.hasMoreElements()) {
String paramName = reqParams.nextElement();
String paramValue = request.getParameter(paramName);
LOGGER.info("KKKKK KEY is {}, value is {} count {}", paramName, paramValue, i);
params.put(paramName, paramValue);
i = i + 1;
}
String queryString = request.getQueryString();
if (! StringUtils.isEmpty(queryString)) {
String[] parameters = queryString.split("&");
for (String parameter : parameters) {
String[] keyValuePair = parameter.split("=");
params.remove(keyValuePair[0]);
LOGGER.info("===>>>> Removing KEY {} ", keyValuePair[0]);
}
}
LOGGER.info("NUMBER OF PARAMS COUNT FINAL ===>>>> " + params.size());
}
}
TwilioUtils util = new TwilioUtils(authToken);
boolean validationResult = util.validateRequest(expectedSignature, serverUrl, params);
它总是返回假。我做错什么了吗。