java微信开发者

成为微信开发者,需要通过微信公众平台的验证
-、配置开发者基本信息
![这里写图片描述](https://img-blog.csdn.net/20180502124628217?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2dyb3dpbmdfSVRfYmlyZA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
url是一个可以访问的地址,端口必须是80或者443
token是你自定义的一个内容相当于一个秘钥,用来校验微信签名
appid和秘钥是微信公众号获得
二、校验微信服务信息,实现功能
校验微信,通过request获得微信服务器参数
signature 、timestamp 、nonce 、echostr
校验 signature 无误,原样返回echostr
package com.using.weixin.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpUtils;

import com.using.weixin.common.ApiTools;
import com.using.weixin.wxtools.WeChatUtils;
import com.using.weixin.wxtools.WeiXinTools;
import com.using.weixin.wxtools.vo.recv.WxRecvEventMsg;
import com.using.weixin.wxtools.vo.recv.WxRecvGeoMsg;
import com.using.weixin.wxtools.vo.recv.WxRecvMsg;
import com.using.weixin.wxtools.vo.recv.WxRecvPicMsg;
import com.using.weixin.wxtools.vo.recv.WxRecvTextMsg;
import com.using.weixin.wxtools.vo.recv.WxRecvVoiceMsg;
import com.using.weixin.wxtools.vo.send.WxSendMsg;
import com.using.weixin.wxtools.vo.send.WxSendMusicMsg;
import com.using.weixin.wxtools.vo.send.WxSendNewsMsg;
import com.using.weixin.wxtools.vo.send.WxSendTextMsg;

/**
 * 微信消息处理 请求地址 http://域名/weixin.do
 */
public class IndexServletAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    // token标识
    private static final String TOKEN = "weixin";

    /**
     * post请求接受用户输入的消息,和消息回复
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    }

    /**
     * get请求进行验证服务器是否正常
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        /*
         * 进行接口验证
         */
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        if (null != timestamp && null != nonce && null != echostr
                && null != signature) {
            if (WeiXinTools.access(TOKEN, signature, timestamp, nonce)) {
                response.getWriter().write(echostr);
                return;
            }
            return;
        } else {
            return;
        }
    }

}

签名校验方法

public static boolean access(String token,String signature,String timestamp,String nonce) {
        //组装签名字段
        List<String> ss = new ArrayList<String>();
        ss.add(timestamp);
        ss.add(nonce);
        ss.add(token);
        //排序组装字段
        Collections.sort(ss);
        //拼接字段
        StringBuilder builder = new StringBuilder();
        for(String s : ss) {
            builder.append(s);
        }
        //返回签名校验结果
        return signature.equalsIgnoreCase(HashKit.sha1(builder.toString()));
    }

你可能感兴趣的:(微信开发)