Java微信开发-微信java开发接入平台实例

微信上接入平台的demo是PHP的,没得java的,所以我自己做接入的时候遇到了一点小问题。用java做带薪开发,首先就要先通过微信的接入配置,大概原理就是微信往服务器发送一些数据,然后在服务器端做一些数据的处理,再返回数据就行了。

微信官方的名称是:

验证服务器地址的有效性

下面是微信官方的文档:

开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带四个参数:

参数描述
signature微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
timestamp时间戳
nonce随机数
echostr随机字符串

开发者通过检验signature对请求进行校验(下面有校验方式)。若确认此次GET请求来自微信服务器,请原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。

加密/校验流程如下:
1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
实例开始

1:写个servlet类,来处理微信发送过来的数据:
package com.levi.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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

import com.levi.utils.SHA1;

/**
 * Servlet implementation class WeiXin
 */
public class WeiXin extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 private static String Token = "leviweixin";  //这个是之前在微信上填写的Token数据,可以自定义
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WeiXin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 System.out.println("get请求,正确");
		System.out.println("获得微信请求!");
		String signature=request.getParameter("signature");
		String timestamp=request.getParameter("timestamp");
		String nonce=request.getParameter("nonce");
		String echostr=request.getParameter("echostr");
		System.out.println("signature="+signature);
		System.out.println("timestamp="+timestamp);
		System.out.println("nonce="+nonce);
		System.out.println("echostr="+echostr);
		System.out.println("Token="+Token);
		
		 List<String> params = new ArrayList<String>();  
         params.add(Token);  
         params.add(timestamp);  
         params.add(nonce);  
         //1. 将token、timestamp、nonce三个参数进行字典序排序  
         Collections.sort(params, new Comparator<String>() {  
             @Override  
             public int compare(String o1, String o2) {  
                 return o1.compareTo(o2);  
             }  
         }); 
         //2. 将三个参数字符串拼接成一个字符串进行sha1加密  
         String temp = SHA1.encode(params.get(0) + params.get(1) + params.get(2));  
         if (temp.equals(signature)) {  
        	 System.out.println("原:"+signature);
        	 System.out.println("测试:"+temp);
        	 System.out.println("匹配正确,传回微信了");
             response.getWriter().write(echostr);  
             System.out.println("传回微信成功");
         }  
		 else {  
        System.out.println("没有传回去数据,");
     } 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 System.out.println("post请求,错误了");
	}

}

2:需要一个SHA1 的util类:
/*
 * 微信公众平台(JAVA) SDK
 *
 * Copyright (c) 2016, Ansitech Network Technology Co.,Ltd All rights reserved.
 * http://www.ansitech.com/weixin/sdk/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.levi.utils;

import java.security.MessageDigest;

/**
 * <p>Title: SHA1算法</p>
 *
 * @author qsyang<yangqisheng274@163.com>
 */
public final class SHA1 {

    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
                           '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /**
     * Takes the raw bytes from the digest and formats them correct.
     *
     * @param bytes the raw bytes from the digest.
     * @return the formatted bytes.
     */
    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        // 把密文转换成十六进制的字符串形式
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }

    public static String encode(String str) {
        if (str == null) {
            return null;
        }
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.update(str.getBytes());
            return getFormattedText(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
3、配置下web.xml就行了
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>wx</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>WeiXin</display-name>
    <servlet-name>WeiXin</servlet-name>
    <servlet-class>com.levi.service.WeiXin</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>WeiXin</servlet-name>
    <url-pattern>/WeiXin</url-pattern>
  </servlet-mapping>
</web-app>



点击提交,如果正确会显示配置成功。

注意事项:
1、一定要用到外网IP映射,可以用花生壳,或者用ngrok。端口要用80端口。用外网映射的目的就是模拟服务器,然后微信才能够给服务器发信息并且验证;
2、要多连续点击几次提交,直到ok为止,我测试的时候就发现,明明是对的,但是微信就说配置失败,后面我多次连续点击提交,非常快的速度连续点击提交。有个前提,要代码没写错哈,其实你把上面的3个代码块的文件复制进去应该是没得问题的。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值