微信企业号接入(使用SpringMVC) .

转:http://blog.csdn.net/omsvip/article/details/39397059

 

企业号简介:

企业号是公众平台为企业客户提供的微信移动应用入口。它帮助企业建立与员工、上下游供应链及企业应用间的连接。利用企业号,企业或第三方合作伙伴可以帮助企业快速、低成本的实现移动轻应用的部署与应用,实现生产、管理、协作、运营的移动化。

当你成功申领一个企业号后,你可以登录企业号的管理页面,导入通讯录,配置应用,邀请成员关注该企业号,也可以通过应用向成员发送文本、图文、文件、视频、音频等多媒体消息。通过简单的配置,你就可以自动并回复成员发送的消息,实现公告通知、知识管理、企业文化建设、手机企业通讯录等基本的企业应用。

你还可以通过本接口文档所描述的接口,建立企业号同企业应用间的连接,实现更多丰富且个性化的企业移动应用。

建立连接:


企业应用同微信企业号间的连接有以下三种方式:

1、企业应用调用企业号提供的接口,管理或查询企业号后台所管理的资源、或给成员发送消息等,以下称主动调用模式。

2、企业号把用户发送的消息或用户触发的事件推送给企业应用,由企业应用处理,以下称回调模式。

3、用户在微信中阅读企业应用下发的H5页面,该页面可以调用微信提供的原生接口,使用微信开放的终端能力,以下称JSAPI模式。

通过这三种连接方式的结合,企业可以在微信企业号中建立功能强大的移动轻应用,并依托微信数亿活跃用户,帮助企业方便、快捷地实现应用的部署,并确保应用的活跃度。

每一种连接的实现机制不同,以下各节分别描述三种连接机制。

接入(回调模式):




具体实现方法(Java):

首先下载腾讯提供的加密解密包,地址是:微信加密解密地址

Java:http://qydev.weixin.qq.com/java.zip

php:http://qydev.weixin.qq.com/php.zip

C#:http://qydev.weixin.qq.com/csharp.zip

把下次好的文件解压并添加到项目中:

记得添加:commons-codec-1.9.jar



编写核心接入类代码如下:

  1. <span style="font-size:14px;">package org.oms.qiye.web;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import org.oms.qiye.aes.AesException;  
  9. import org.oms.qiye.aes.WXBizMsgCrypt;  
  10. import org.oms.qiye.service.CoreService;  
  11. import org.springframework.stereotype.Controller;  
  12. import org.springframework.web.bind.annotation.RequestMapping;  
  13. import org.springframework.web.bind.annotation.RequestMethod;  
  14.   
  15. /** 
  16.  * 注解方式打开链接 
  17.  *  
  18.  * @author Sunlight 
  19.  * 
  20.  */  
  21. @Controller  
  22. public class CoreController {  
  23.     private String token = "sunlight";  
  24.     private String encodingAESKey = "s8vFF4f6AWay3uAdJh79WD6imaam4BV6Kl4eL4UzgfM";  
  25.     private String corpId = "xxxxxxxxxx"//你的企业号ID   
  26.   
  27.     @RequestMapping(value = { "/coreJoin.do" }, method = RequestMethod.GET)  
  28.     public void coreJoinGet(HttpServletRequest request,  
  29.             HttpServletResponse response) throws IOException {  
  30.         // 微信加密签名   
  31.         String msg_signature = request.getParameter("msg_signature");  
  32.         // 时间戳   
  33.         String timestamp = request.getParameter("timestamp");  
  34.         // 随机数   
  35.         String nonce = request.getParameter("nonce");  
  36.         // 随机字符串   
  37.         String echostr = request.getParameter("echostr");  
  38.   
  39.         System.out.println("request=" + request.getRequestURL());  
  40.   
  41.         PrintWriter out = response.getWriter();  
  42.         // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败   
  43.         String result = null;  
  44.         try {  
  45.             WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey,  
  46.                     corpId);  
  47.             result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);  
  48.         } catch (AesException e) {  
  49.             e.printStackTrace();  
  50.         }  
  51.         if (result == null) {  
  52.             result = token;  
  53.         }  
  54.         out.print(result);  
  55.         out.close();  
  56.         out = null;  
  57.     }  
  58.   
  59.     @RequestMapping(value = { "/coreJoin.do" }, method = RequestMethod.POST)  
  60.     public void coreJoinPost(HttpServletRequest request,  
  61.             HttpServletResponse response) throws IOException {  
  62.         // 调用核心业务类接收消息、处理消息   
  63.         String respMessage = CoreService.processRequest(request);  
  64.         System.out.println("respMessage=" + respMessage);  
  65.         // 响应消息   
  66.         PrintWriter out = response.getWriter();  
  67.         out.print(respMessage);  
  68.         out.close();  
  69.   
  70.     }  
  71.   
  72. }  
  73. </span>  
<span style="font-size:14px;">package org.oms.qiye.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.oms.qiye.aes.AesException;
import org.oms.qiye.aes.WXBizMsgCrypt;
import org.oms.qiye.service.CoreService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 注解方式打开链接
 * 
 * @author Sunlight
 *
 */
@Controller
public class CoreController {
	private String token = "sunlight";
	private String encodingAESKey = "s8vFF4f6AWay3uAdJh79WD6imaam4BV6Kl4eL4UzgfM";
	private String corpId = "xxxxxxxxxx"; //你的企业号ID

	@RequestMapping(value = { "/coreJoin.do" }, method = RequestMethod.GET)
	public void coreJoinGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// 微信加密签名
		String msg_signature = request.getParameter("msg_signature");
		// 时间戳
		String timestamp = request.getParameter("timestamp");
		// 随机数
		String nonce = request.getParameter("nonce");
		// 随机字符串
		String echostr = request.getParameter("echostr");

		System.out.println("request=" + request.getRequestURL());

		PrintWriter out = response.getWriter();
		// 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
		String result = null;
		try {
			WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, encodingAESKey,
					corpId);
			result = wxcpt.VerifyURL(msg_signature, timestamp, nonce, echostr);
		} catch (AesException e) {
			e.printStackTrace();
		}
		if (result == null) {
			result = token;
		}
		out.print(result);
		out.close();
		out = null;
	}

	@RequestMapping(value = { "/coreJoin.do" }, method = RequestMethod.POST)
	public void coreJoinPost(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// 调用核心业务类接收消息、处理消息
		String respMessage = CoreService.processRequest(request);
		System.out.println("respMessage=" + respMessage);
		// 响应消息
		PrintWriter out = response.getWriter();
		out.print(respMessage);
		out.close();

	}

}
</span>

CoreService.java 此类目前没有内容,后面编写消息回复功能!

web.xml文件配置:

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <display-name>QiyeProject</display-name>  
  4.   <welcome-file-list>  
  5.         <welcome-file>index.jsp</welcome-file>  
  6.     </welcome-file-list>  
  7.   
  8.     <servlet>  
  9.         <servlet-name>mvc</servlet-name>  
  10.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  11.         <init-param>  
  12.             <param-name>contextConfigLocation</param-name>  
  13.             <!-- 不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml -->  
  14.             <param-value>/WEB-INF/mvc-servlet.xml</param-value>  
  15.         </init-param>  
  16.         <load-on-startup>1</load-on-startup>  
  17.     </servlet>  
  18.   
  19.     <servlet-mapping>  
  20.         <servlet-name>mvc</servlet-name>  
  21.         <url-pattern>/</url-pattern>  
  22.     </servlet-mapping>  
  23.   
  24.     <!-- 表单处理乱码 -->  
  25.     <filter>  
  26.         <filter-name>CharacterFilter</filter-name>  
  27.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  28.         <init-param>  
  29.             <param-name>encoding</param-name>  
  30.             <param-value>UTF-8</param-value>  
  31.         </init-param>  
  32.     </filter>  
  33.     <filter-mapping>  
  34.         <filter-name>CharacterFilter</filter-name>  
  35.         <url-pattern>/*</url-pattern>  
  36.     </filter-mapping>  
  37. </web-app></span>  
<span style="font-size:14px;"><?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>QiyeProject</display-name>
  <welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- 不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml -->
			<param-value>/WEB-INF/mvc-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>mvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 表单处理乱码 -->
	<filter>
		<filter-name>CharacterFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app></span>

mvc-servlet.xml 文件配置:
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd   
  8.     http://www.springframework.org/schema/tx   
  9.     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
  10.     http://www.springframework.org/schema/context  
  11.     http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  12.     http://www.springframework.org/schema/mvc  
  13.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  14.   
  15.     <context:component-scan base-package="org.oms.qiye.web"></context:component-scan>  
  16.     <mvc:annotation-driven />  
  17.   
  18.     <bean  
  19.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  20.         <property name="prefix" value="/WEB-INF/jsp/"></property>  
  21.         <property name="suffix" value=".jsp"></property>  
  22.     </bean>  
  23.       
  24.     <!-- 静态文件处理 -->  
  25.     <mvc:resources mapping="/resource/images/**" location="/resource/images/" />  
  26.     <mvc:resources mapping="/resource/js/**" location="/resource/js/" />  
  27.     <mvc:resources mapping="/resource/css/**" location="/resource/css/" />  
  28.       
  29.       
  30. </beans></span>  
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

	<context:component-scan base-package="org.oms.qiye.web"></context:component-scan>
	<mvc:annotation-driven />

	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 静态文件处理 -->
	<mvc:resources mapping="/resource/images/**" location="/resource/images/" />
	<mvc:resources mapping="/resource/js/**" location="/resource/js/" />
	<mvc:resources mapping="/resource/css/**" location="/resource/css/" />
	
	
</beans></span>

此处接入需要注意:

1.com\qq\weixin\mp\aes目录下是用户需要用到的接入企业微信的接口,其中WXBizMsgCrypt.java文件提供的WXBizMsgCrypt类封装了用户接入企业微信的三个接口,其它的类文件用户用于实现加解密,用户无须关心。sample.java文件提供了接口的使用示例。

2.WXBizMsgCrypt封装了VerifyURL, DecryptMsg, EncryptMsg三个接口,分别用于开发者验证回调url、接收消息的解密以及开发者回复消息的加密过程。使用方法可以参考Sample.java文件。

3.请开发者使用jdk1.5以上的版本。针对org.apache.commons.codec.binary.Base64,需要导入jar包commons-codec-1.9(或comm ons-codec-1.8等其他版本),我们有提供,官方下载地址:

http://commons.apache.org/proper/commons-codec/download_codec.cgi


****请特别注意******

4.异常java.security.InvalidKeyException:illegal Key Size的解决方案:

在官方网站下载JCE无限制权限策略文件(JDK7的下载地址:

http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt。如果安装了JRE,将两个jar文件放到%JRE_HOME% \lib\security目录下覆盖原来的文件,如果安装了JDK,将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件。


接入结果:


微信企业号开发交流加QQ群:89714226

转载请注明出处

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值