资源简介
【核心代码】
package com.gson;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Properties;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.gson.bean.Articles;
import com.gson.bean.InMessage;
import com.gson.bean.OutMessage;
import com.gson.bean.TextOutMessage;
import com.gson.inf.MessageProcessingHandler;
import com.gson.util.Tools;
import com.gson.util.XStreamFactory;
import com.thoughtworks.xstream.XStream;
/**
* 请求拦截
*
* @author GodSon
*
*/
public class WeChatFilter implements Filter {
private final Logger logger = Logger.getLogger(WeChatFilter.class);
private String _token;
private String conf = "classPath:wechat.properties";
private String defaultHandler = "com.gson.inf.DefaultMessageProcessingHandlerImpl";
private Properties p;
@Override
public void destroy() {
logger.info("WeChatFilter已经销毁");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
Boolean isGet = request.getMethod().equals("GET");
String path = request.getServletPath();
String pathInfo = path.substring(path.lastIndexOf("/"));
if (pathInfo == null) {
response.getWriter().write("error");
} else {
_token = pathInfo.substring(1);
if (isGet) {
doGet(request, response);
} else {
doPost(request, response);
}
}
}
private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");
OutMessage oms = new OutMessage();
ServletInputStream in = request.getInputStream();
// 转换微信post过来的xml内容
XStream xs = XStreamFactory.init(false);
xs.alias("xml", InMessage.class);
String xmlMsg = Tools.inputStream2String(in);
logger.debug("输入消息:[" xmlMsg "]");
InMessage msg = (InMessage) xs.fromXML(xmlMsg);
// 获取自定消息处理器,如果自定义处理器则使用默认处理器。
String handler = p.getProperty("MessageProcessingHandlerImpl");
if (handler == null)
handler = defaultHandler;
try {
// 加载处理器
Class<?> clazz = Class.forName(handler);
MessageProcessingHandler processingHandler = (MessageProcessingHandler) clazz.newInstance();
// 取得消息类型
String type = msg.getMsgType();
Method mt = clazz.getMethod(type "TypeMsg", InMessage.class);
oms = (OutMessage) mt.invoke(processingHandler, msg);
if (oms == null) {
oms = new TextOutMessage();
((TextOutMessage) oms).setContent("系统错误!");
}
setMsgInfo(oms,msg);
} catch (Exception e) {
logger.error(e);
oms = new TextOutMessage();
((TextOutMessage) oms).setContent("系统错误!");
try {
setMsgInfo(oms,msg);
} catch (Exception e1) {
logger.error(e);
}
}
// 把发送发送对象转换为xml输出
xs = XStreamFactory.init(true);
xs.alias("xml", oms.getClass());
xs.alias("item", Articles.class);
String xml = xs.toXML(oms);
logger.debug("输出消息:[" xml "]");
response.getWriter().write(xml);
}
private void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
String signature = request.getParameter("signature");// 微信加密签名
String timestamp = request.getParameter("timestamp");// 时间戳
String nonce = request.getParameter("nonce");// 随机数
String echostr = request.getParameter("echostr");//
// 验证
if (Tools.checkSignature(_token, signature, timestamp, nonce)) {
response.getWriter().write(echostr);
}
}
private void setMsgInfo(OutMessage oms,InMessage msg) throws Exception {
// 设置发送信息
Class<?> outMsg = oms.getClass().getSuperclass();
Field CreateTime = outMsg.getDeclaredField("CreateTime");
Field ToUserName = outMsg.getDeclaredField("ToUserName");
Field FromUserName = outMsg.getDeclaredField("FromUserName");
ToUserName.setAccessible(true);
CreateTime.setAccessible(true);
FromUserName.setAccessible(true);
CreateTime.set(oms, new Date().getTime());
ToUserName.set(oms, msg.getFromUserName());
FromUserName.set(oms, msg.getToUserName());
}
/**
* 启动的时候加载wechat.properties配置 可以在过滤器配置wechat.properties路径
*/
@Override
public void init(FilterConfig config) throws ServletException {
String cf = config.getInitParameter("conf");
if (cf != null) {
conf = cf;
}
String classPath = this.getClass().getResource("/").getPath().replaceAll("%20", " ");
conf = conf.replace("classPath:", classPath);
p = new Properties();
File pfile = new File(conf);
if (pfile.exists()) {
try {
p.load(new FileInputStream(pfile));
} catch (FileNotFoundException e) {
logger.error("未找到wechat.properties", e);
} catch (IOException e) {
logger.error("wechat.properties读取异常", e);
}
}
logger.info("WeChatFilter已经启动!");
}
}
代码片段和文件信息
/**
* 微信公众平台开发模式(JAVA) SDK
* (c) 2012-2013 ____′↘夏悸 MIT Licensed
* http://www.jeasyuicn.com/wechat
*/
package com.gson;
import com.gson.bean.InMessage;
import com.gson.bean.OutMessage;
import com.gson.bean.TextOutMessage;
import com.gson.inf.MessageProcessingHandler;
/**
* 自定义实现消息处理
* @author GodSon
*
*/
public class MessageProcessingHandlerImpl implements MessageProcessingHandler {
@Override
public OutMessage textTypeMsg(InMessage msg) {
TextOutMessage oms = new TextOutMessage();
oms.setContent(msg.getContent());
return oms;
}
@Override
public OutMessage locationTypeMsg(InMessage msg) {
// TODO Auto-generated method stub
return null;
}
@Override
public OutMessage imageTypeMsg(InMessage msg) {
// TODO Auto-
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-12-04 02:04 wechat\
文件 851 2013-12-04 02:04 wechat\.classpath
文件 24 2013-12-04 02:04 wechat\.gitignore
文件 1322 2013-12-04 02:04 wechat\.project
文件 1592 2013-12-04 02:04 wechat\README.md
目录 0 2013-12-04 02:04 wechat\WebRoot\
目录 0 2013-12-04 02:04 wechat\WebRoot\me
文件 36 2013-12-04 02:04 wechat\WebRoot\me
目录 0 2013-12-04 02:04 wechat\WebRoot\WEB-INF\
文件 9 2013-12-04 02:04 wechat\WebRoot\WEB-INF\.gitignore
目录 0 2013-12-04 02:04 wechat\WebRoot\WEB-INF\lib\
文件 3490 2013-12-04 02:04 wechat\WebRoot\WEB-INF\lib\log4j-adapter-1.0.0.jar
文件 737 2013-12-04 02:04 wechat\WebRoot\WEB-INF\web.xm
文件 821 2013-12-04 02:04 wechat\WebRoot\index.jsp
目录 0 2013-12-04 02:04 wechat\WebRoot\js\
文件 3630 2013-12-04 02:04 wechat\WebRoot\js\wexin.js
文件 1370 2013-12-04 02:04 wechat\WebRoot\wx_pay.txt
文件 2610 2013-12-04 02:04 wechat\pom.xm
目录 0 2013-12-04 02:04 wechat\src\
目录 0 2013-12-04 02:04 wechat\src\com\
目录 0 2013-12-04 02:04 wechat\src\com\gson\
文件 1259 2013-12-04 02:04 wechat\src\com\gson\MessageProcessingHandlerImpl.java
文件 5885 2013-12-04 02:04 wechat\src\com\gson\WeChatFilter.java
目录 0 2013-12-04 02:04 wechat\src\com\gson\bean\
文件 798 2013-12-04 02:04 wechat\src\com\gson\bean\Articles.java
文件 3219 2013-12-04 02:04 wechat\src\com\gson\bean\InMessage.java
文件 724 2013-12-04 02:04 wechat\src\com\gson\bean\MusicOutMessage.java
文件 1495 2013-12-04 02:04 wechat\src\com\gson\bean\NewsOutMessage.java
文件 643 2013-12-04 02:04 wechat\src\com\gson\bean\OutMessage.java
文件 667 2013-12-04 02:04 wechat\src\com\gson\bean\TextOutMessage.java
目录 0 2013-12-04 02:04 wechat\src\com\gson\inf\
............此处省略17个文件信息
相关资源
- 仿微信头像上传
- 仿微信朋友圈选择图片
- 仿微信 聊天、摇一摇、设置等操作源
- phonggap微信分享
- Android 微信抢红包(抢红包服务-23_2
- Android 仿微信摇一摇(传感器-12_104)
- 微信小程序获取用户openid源码含后端
- 微信支付Demo(android+ios+开发文档)
- 高仿微信UI
- 仿微信密码支付键盘
- android仿微信图片选择器
- android 仿微信图片选择器类库源码(可
- 微信小程序:我的页面含授权获取头
- 微信JSAPI支付(官方)-php版本
- 微信扫码登录网站实现案例(无需授
- android微信登录代码
- Android 完美高仿的微信源码
- 微信二维码支付结果监听工具源码(
- 微信支付V3 Java jsApi 扫码支付
- Java 微信支付官方SDK源码(已修复XX
- java 扫描二维码登录 源码
- java 微信支付平台
- Android应用源码高仿安卓微信UI全套项
- Android--仿微信聊天界面
- 微信公众号H5支付demo(Servlet实现)
- 微信小程序 AES加密 ECB模式js文件
- 2019年最新版微信“已停止访问该网页
- JavaWeb接入微信公众号
- java微信开放平台APP支付代码
- 微信公众号Java后台自定义分享Spring
评论
共有 条评论