SpringBoot开发微信公众号(一)

java开发微信公众号的环境搭建

一、完成个人订阅号的认证

有兴趣的可以在编辑模式进行尝试

二、进入开发者模式下的配置

在左侧的导航栏下方,选择开发下的基本配置

说明:URL为java服务器的地址,此处需要用到内网穿透功能,实现的将内网的tomcat服务的地址映射到公网的80端口,实现的方式有不少,这里推荐的是自己尝试的两种方式,相信大家也有所了解,一种ngrok,推荐使用的国内的服务器,低延时,实现域名固定。第二种,采用花生壳进行穿透。具体实现网上也有挺多,注册了一个,认证时需要6元。token与后台设置的保持一致就行。

三.java后台配置

3.1 springBoot项目搭建

springBoot近段时间流行的微框架,减少了大量的xml配置,简化环境的搭建。介绍一种简单的springboot项目搭建,在http://start.spring.io/配置自己需要的组件,下载到本地,Myeclipse通过Maven导入,即可完成

代码实现


SHA1加密

import java.security.MessageDigest;

/**
 * 
 * 类名称: SHA1
 * 类描述: sha1加密
 * @author yuanjun
 * 创建时间:2017年12月5日上午11:10:01
 */
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.3 application的配置,也可以不配置数据源,在启用项目时,则需要排除数据库的连接

server.port = 8080
server.context-path = /weixin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/websocket
spring.datasource.username=root
spring.datasource.password=123456
server.port = 8080 server.context-path = /weixin spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/websocket spring.datasource.username=root spring.datasource.password=123456

3.4 启动tomcat服务,即运行主方法即可


经验分享 程序员 微信小程序 职场和发展