
org.weixin4j.spring.web.WeixinJieruController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weixin4j-spring Show documentation
Show all versions of weixin4j-spring Show documentation
一个让Weixin4j可以在Spring中集成的框架。
The newest version!
/*
* 微信公众平台(JAVA) SDK
*
* Copyright (c) 2014, Ansitech Network Technology Co.,Ltd All rights reserved.
*
* http://www.weixin4j.org/spring/
*
* 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 org.weixin4j.spring.web;
import java.io.IOException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.weixin4j.WeixinException;
import org.weixin4j.spi.IMessageHandler;
import org.weixin4j.util.TokenUtil;
/**
* 微信开发者接入控制器
*
* @author yangqisheng
*/
public class WeixinJieruController {
@Autowired
private IMessageHandler messageHandler;
//开发者接入验证
@RequestMapping(method = RequestMethod.GET)
public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
//消息来源可靠性验证
String signature = request.getParameter("signature");// 微信加密签名
String timestamp = request.getParameter("timestamp");// 时间戳
String nonce = request.getParameter("nonce"); // 随机数
//Token为weixin4j.properties中配置的Token
String token = TokenUtil.get();
//1.验证消息真实性
//http://mp.weixin.qq.com/wiki/index.php?title=验证消息真实性
//成为开发者验证
String echostr = request.getParameter("echostr");
//确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
if (TokenUtil.checkSignature(token, signature, timestamp, nonce)) {
response.getWriter().write(echostr);
}
}
//接收微信消息
@RequestMapping(method = RequestMethod.POST)
public void post(HttpServletRequest request, HttpServletResponse response) throws IOException {
//消息来源可靠性验证
String signature = request.getParameter("signature");// 微信加密签名
String timestamp = request.getParameter("timestamp");// 时间戳
String nonce = request.getParameter("nonce"); // 随机数
//Token为weixin4j.properties中配置的Token
String token = TokenUtil.get();
//确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败
if (!TokenUtil.checkSignature(token, signature, timestamp, nonce)) {
//消息不可靠,直接返回
response.getWriter().write("");
return;
}
//用户每次向公众号发送消息、或者产生自定义菜单点击事件时,响应URL将得到推送
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");
//获取POST流
ServletInputStream in = request.getInputStream();
//处理输入消息,返回结果
String xml = messageHandler.invoke(in);
//返回结果
response.getWriter().write(xml);
} catch (IOException e) {
response.getWriter().write("");
} catch (WeixinException ex) {
response.getWriter().write("");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy