前言
- 验证服务器需要原样返回
echostr
字符串 - 开发环境
spring boot
解决方案
-
将
contentType
改为text/plain;charset=UTF-8
即可 -
代码贴出
org.apache.commons.codec.digest.DigestUtils;
...
@GetMapping("/sign")
public void getSignInfo(@RequestParam(value = "signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam("echostr") String echostr, HttpServletResponse httpServletResponse) throws Exception {
List<String> array = new ArrayList<>();
array.add(TOKEN);
array.add(nonce);
array.add(timestamp);
Collections.sort(array, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
String arrStr = "";
for (String str : array) {
arrStr += str;
}
String sign = DigestUtils.sha1Hex(arrStr);
if (!sign.equals(signature)) {
RestCodeType.USER_ACCESS_ERROR.write();
}
httpServletResponse.setContentType("text/plain;charset=UTF-8");
httpServletResponse.getOutputStream().write(echostr.getBytes());
httpServletResponse.getOutputStream().close();
}
- 或者
org.apache.commons.codec.digest.DigestUtils;
...
@GetMapping(value = "/sign", produces = "text/plain;charset=UTF-8")
public String getSignInfo(@RequestParam(value = "signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam("echostr") String echostr, HttpServletResponse httpServletResponse) throws Exception {
List<String> array = new ArrayList<>();
array.add(TOKEN);
array.add(nonce);
array.add(timestamp);
Collections.sort(array, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
String arrStr = "";
for (String str : array) {
arrStr += str;
}
String sign = DigestUtils.sha1Hex(arrStr);
if (!sign.equals(signature)) {
RestCodeType.USER_ACCESS_ERROR.write();
}
return echostr;
}
网友评论