问题描述
- 关于用java开发微信公众平台开发 token验证失败的问题
-
大家好,最近在学习用java开发微信公众平台,第一次写的代码token验证通过了,用的是阿里云的服务器换系统(server2008->2003)后就怎么也通过不过验证。直接输入域名www.zhenggufang.com/aip/longin网页显示404.域名也已经备过案。服务器上的tomcat也启动正常tomcat/conf/server也修改了。多谢提交还是token验证失败。请各位帮忙看看代码如下:package service;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import message.resp.TextMessage;
import util.ValidationUtil;
public class LoginServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {System.out.println(get 请求); String signature = request.getParameter(signature); String timestamp = request.getParameter(timestamp); String nonce= request.getParameter(nonce); String echostr=request.getParameter(echostr); PrintWriter out=response.getWriter(); System.out.println(signature+ signature); System.out.println(timestamp+ timestamp); System.out.println(nonce+ nonce); System.out.println(echostr+ echostr); 验证请求确认成功原样返回echostr参数内容,则接入生效,成为开发者成功,否则 if(ValidationUtil.chechSignature(signature, timestamp, nonce)) { out.print(echostr); } out.close(); }
接受微信服务器发过来的xml数据包(通过post)形式发过来的
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String respXml=;响应的xml串 request.setCharacterEncoding(utf-8); response.setCharacterEncoding(utf-8); String signature = request.getParameter(signature); String timestamp = request.getParameter(timestamp); String nonce= request.getParameter(nonce); PrintWriter out=response.getWriter(); if(ValidationUtil.chechSignature(signature, timestamp, nonce)) { 接收并解析来自用户的xml数据包中的内容 MapString,String reqMap=MessageUtil.parseXml(request); String ToUserName=reqMap.get(ToUserName); String FromUserName=reqMap.get(FromUserName); String MsgType=reqMap.get(MsgType); String Content=reqMap.get(Content); System.out.println(用户给公众号发的消息+Content); 开始响应消息给用户 String respContent=;要响应的文本内容 构建一条文本消息 TextMessage textMsg=new TextMessage(); textMsg.setFromUserName(FromUserName); textMsg.setFromUserName(ToUserName); textMsg.setCreateTime(new Date().getTime()); textMsg.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT); if(MsgType.equals(MessageUtil.RESP_MESSAGE_TYPE_TEXT)) { respContent=大家好,来自工作公众平台的测试消息!!; } textMsg.setContent(respContent); respXml=MessageUtil.messageToXml(textMsg); System.out.println(respXml+respXml); 接受打印的字符 out.println(respXml); } out.close(); out=null; }
}
package util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;/**
- 微信请求校验工具类
- @author y
*
*/
public class ValidationUtil
{
private static String token="wlghr0479";
public static boolean chechSignature(String signature,String timestamp,String nonce)
{
//1.将token,timestamp,nonce三个参数排序
String[] str= new String[]{token,timestamp,nonce};
Arrays.sort(str);
//2.将三个参数字符串接成一个字符串
StringBuilder buff=new StringBuilder();
for (int i=0;i<str.length;i++)
{
buff.append(str[i]);
}
//3.进行sha1加密
MessageDigest md=null;
String result="";
try {
md= MessageDigest.getInstance("SHA-1");
byte[]date =md.digest(buff.toString().getBytes());
//将字节数组转换成字符串
result=bytesToStr(date);
System.out.println("加密后的"+result);} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result!=null ?(result.equals(signature.toUpperCase())) :false;
}
//将直接数组转换成十六进制字符串
private static String bytesToStr(byte[] byteArray)
{
String strDigest="";
for (int i=0;i<byteArray.length;i++)
{
strDigest += byteToHexStr(byteArray[i]);
}
return strDigest;
}
//将一个字节转换成16进制字符串
private static String byteToHexStr(byte mByte)
{
char[]Digit ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[]temp1=new char[2];
temp1[0]=Digit[mByte>>>4 & 0X0F];
temp1[1]=Digit[mByte & 0X0F];
String str=new String(temp1);
return str;
}}
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>service.LoginServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/api/login</url-pattern> </servlet-mapping>
index.jsp
解决方案
http://www.vzhushou.com/helpView_106.html
http://www.cnblogs.com/txw1958/p/token-verify.html
解决方案二:
看下你服务器上的日志,doGet方法中的日志有没有打印呢?如果没有的话,说明你微信配置的公众号服务器地址配置错误了。
直接访问域名报404错误,说明你的项目部署到服务器上都没有正确部署。建议找个阿里云部署项目的教程看看你。
解决方案三:
应该是这个问题,但是就查不出来啊,都困扰我好几天了,毕小宝老师可以给我远程指导一下吗?不胜感激!!
解决方案四:
你上面的地址是www.zhenggufang.com/aip/longin,但是你配置的地址是http://www.zhenggufang.com/api/login,你是打字太快打错了还是微信那边回调地址写错了?