diff --git a/README.md b/README.md index a9e6aa23..694cbfde 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ weixin4j 项目说明 ------- -`weixin4j`是一个用java编写针对微信开发的工具包,包含[weixin4j-mp](./weixin4j-mp)(微信公众平台API)[weixin4j-qy](./weixin4j-qy)(微信企业号API)以及[weixin4j-server](./weixin4j-server)(微信被动消息服务器)三个工程 +`weixin4j`是一个用java编写针对微信开发的工具包,包含[weixin4j-mp](./weixin4j-mp)(微信公众平台API)、[weixin4j-qy](./weixin4j-qy)(微信企业号API)以及[weixin4j-server](./weixin4j-server)(微信被动消息服务器)三个工程. 功能列表 ------- diff --git a/weixin4j-server/README.md b/weixin4j-server/README.md index 30a915a4..54880492 100644 --- a/weixin4j-server/README.md +++ b/weixin4j-server/README.md @@ -1,8 +1,8 @@ weixin4j-server =============== -微信消息netty服务器 ------------------- +微信消息服务器 +------------- 功能列表 ------- @@ -11,7 +11,7 @@ weixin4j-server * `消息分发` -* `消息拦截`(待测试) +* `消息拦截` 如何使用 ------- @@ -57,6 +57,37 @@ weixin4j-server DebugMessageHandler.global).startup(); } } +编写一个拦截器 + + public static void main(String[] args) { + // 拦截所有请求 + WeixinMessageInterceptor interceptor = new MessageInterceptorAdapter() { + @Override + public boolean preHandle(ChannelHandlerContext context, + WeixinRequest request, WeixinMessage message, + WeixinMessageHandler handler) throws WeixinException { + context.write(new TextResponse("所有消息被拦截了!")); + return false; + } + + @Override + public void postHandle(ChannelHandlerContext context, + WeixinRequest request, WeixinResponse response, + WeixinMessage message, WeixinMessageHandler handler) + throws WeixinException { + System.err.println("preHandle返回为true,执行handler后"); + } + @Override + public void afterCompletion(ChannelHandlerContext context, + WeixinRequest request, WeixinMessage message, + WeixinMessageHandler handler, WeixinException exception) + throws WeixinException { + System.err.println("请求处理完毕"); + } + }; + new WeixinServerBootstrap("token").addInterceptor(interceptor) + .addHandler(BlankMessageHandler.global).startup(); + } 更多内容将会写在wiki里 @@ -125,6 +156,5 @@ assembly打包(辅助) [spring-webmvc:HandlerInterceptor](https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerInterceptor.java) - [更新LOG](./CHANGE.md) ---------------------- \ No newline at end of file diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/bean/AesToken.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/bean/AesToken.java index 2de56014..bfa8e8dc 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/bean/AesToken.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/bean/AesToken.java @@ -31,7 +31,7 @@ public class AesToken implements Serializable { public AesToken(String token) { this.token = token; } - + public AesToken(String appid, String token, String aesKey) { this.appid = appid; this.token = token; diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/MessageHandlerExecutor.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/MessageHandlerExecutor.java index 76674941..ac45263a 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/MessageHandlerExecutor.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/MessageHandlerExecutor.java @@ -34,10 +34,13 @@ public class MessageHandlerExecutor { */ private final WeixinMessageInterceptor[] messageInterceptors; + private final ChannelHandlerContext context; private int interceptorIndex = -1; - public MessageHandlerExecutor(WeixinMessageHandler messageHandler, + public MessageHandlerExecutor(ChannelHandlerContext context, + WeixinMessageHandler messageHandler, WeixinMessageInterceptor[] messageInterceptors) { + this.context = context; this.messageHandler = messageHandler; this.messageInterceptors = messageInterceptors; } @@ -46,15 +49,14 @@ public class MessageHandlerExecutor { return messageHandler; } - public boolean applyPreHandle(ChannelHandlerContext ctx, - WeixinRequest request, WeixinMessage message) + public boolean applyPreHandle(WeixinRequest request, WeixinMessage message) throws WeixinException { if (messageInterceptors != null) { for (int i = 0; i < messageInterceptors.length; i++) { WeixinMessageInterceptor interceptor = messageInterceptors[i]; - if (!interceptor.preHandle(ctx, request, message, + if (!interceptor.preHandle(context, request, message, messageHandler)) { - triggerAfterCompletion(ctx, request, message, null); + triggerAfterCompletion(request, message, null); return false; } this.interceptorIndex = i; @@ -63,29 +65,27 @@ public class MessageHandlerExecutor { return true; } - public void applyPostHandle(ChannelHandlerContext ctx, - WeixinRequest request, WeixinResponse response, + public void applyPostHandle(WeixinRequest request, WeixinResponse response, WeixinMessage message) throws WeixinException { if (messageInterceptors == null) { return; } for (int i = messageInterceptors.length - 1; i >= 0; i--) { WeixinMessageInterceptor interceptor = messageInterceptors[i]; - interceptor.postHandle(ctx, request, response, message, + interceptor.postHandle(context, request, response, message, messageHandler); } } - public void triggerAfterCompletion(ChannelHandlerContext ctx, - WeixinRequest request, WeixinMessage message, WeixinException ex) - throws WeixinException { + public void triggerAfterCompletion(WeixinRequest request, + WeixinMessage message, WeixinException ex) throws WeixinException { if (messageInterceptors == null) { return; } for (int i = this.interceptorIndex; i >= 0; i--) { WeixinMessageInterceptor interceptor = messageInterceptors[i]; try { - interceptor.afterCompletion(ctx, request, message, + interceptor.afterCompletion(context, request, message, messageHandler, ex); } catch (WeixinException e) { logger.error( diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java index fcb1a90b..485c6c10 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java @@ -1,9 +1,8 @@ package com.foxinmy.weixin4j.dispatcher; +import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.http.DefaultFullHttpResponse; -import io.netty.handler.codec.http.HttpResponseStatus; -import io.netty.handler.codec.http.HttpVersion; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; @@ -17,13 +16,10 @@ import com.foxinmy.weixin4j.bean.BeanFactory; import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.handler.WeixinMessageHandler; import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor; -import com.foxinmy.weixin4j.request.ResponseMessage; import com.foxinmy.weixin4j.request.WeixinMessage; import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.response.WeixinResponse; -import com.foxinmy.weixin4j.type.EncryptType; import com.foxinmy.weixin4j.util.ClassUtil; -import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.HttpUtil; import com.foxinmy.weixin4j.util.ReflectionUtil; @@ -71,46 +67,40 @@ public class WeixinMessageDispatcher { public void doDispatch(final ChannelHandlerContext context, final WeixinRequest request, final WeixinMessage message) throws WeixinException { - MessageHandlerExecutor handlerExecutor = getHandlerExecutor(request, - message); + MessageHandlerExecutor handlerExecutor = getHandlerExecutor(context, + request, message); if (handlerExecutor == null || handlerExecutor.getMessageHandler() == null) { noHandlerFound(context, request, message); return; } - if (!handlerExecutor.applyPreHandle(context, request, message)) { + if (!handlerExecutor.applyPreHandle(request, message)) { return; } WeixinException dispatchException = null; try { - WeixinResponse _response = handlerExecutor.getMessageHandler() + WeixinResponse response = handlerExecutor.getMessageHandler() .doHandle(request, message); log.info( "\n=================message response=================\n{}", - _response); - handlerExecutor.applyPostHandle(context, request, _response, - message); - ResponseMessage response = new ResponseMessage(message, _response); - if (request.getEncryptType() == EncryptType.RAW) { - context.write(HttpUtil.createWeixinMessageResponse( - response.toXml(), Consts.CONTENTTYPE$APPLICATION_XML)); - } else { - context.write(response); - } + response); + handlerExecutor.applyPostHandle(request, response, message); + context.write(response); } catch (WeixinException e) { dispatchException = e; } - handlerExecutor.triggerAfterCompletion(context, request, message, + handlerExecutor.triggerAfterCompletion(request, message, dispatchException); } protected void noHandlerFound(ChannelHandlerContext ctx, WeixinRequest request, WeixinMessage message) { - ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.NOT_FOUND)); + ctx.writeAndFlush(HttpUtil.createHttpResponse(null, NOT_FOUND, null)) + .addListener(ChannelFutureListener.CLOSE); } - protected MessageHandlerExecutor getHandlerExecutor(WeixinRequest request, + protected MessageHandlerExecutor getHandlerExecutor( + ChannelHandlerContext context, WeixinRequest request, WeixinMessage message) throws WeixinException { WeixinMessageHandler messageHandler = null; WeixinMessageHandler[] messageHandlers = getMessageHandlers(); @@ -123,7 +113,8 @@ public class WeixinMessageDispatcher { break; } } - return new MessageHandlerExecutor(messageHandler, messageInterceptors); + return new MessageHandlerExecutor(context, messageHandler, + getMessageInterceptors()); } public WeixinMessageHandler[] getMessageHandlers() throws WeixinException { @@ -173,7 +164,7 @@ public class WeixinMessageDispatcher { public WeixinMessageInterceptor[] getMessageInterceptors() throws WeixinException { if (this.messageInterceptors == null) { - if (this.messageInterceptorList != null) { + if (this.messageInterceptorPackages != null) { List> messageInterceptorClass = new LinkedList>(); for (String packageName : messageInterceptorPackages) { messageInterceptorClass.addAll(ClassUtil diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/MessageInterceptorAdapter.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/MessageInterceptorAdapter.java index 2fd758e2..a803caaf 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/MessageInterceptorAdapter.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/MessageInterceptorAdapter.java @@ -12,20 +12,21 @@ public abstract class MessageInterceptorAdapter implements WeixinMessageInterceptor { @Override - public boolean preHandle(ChannelHandlerContext ctx, WeixinRequest request, - WeixinMessage message, WeixinMessageHandler handler) - throws WeixinException { + public boolean preHandle(ChannelHandlerContext context, + WeixinRequest request, WeixinMessage message, + WeixinMessageHandler handler) throws WeixinException { return true; } @Override - public void postHandle(ChannelHandlerContext ctx, WeixinRequest request, - WeixinResponse response, WeixinMessage message, - WeixinMessageHandler handler) throws WeixinException { + public void postHandle(ChannelHandlerContext context, + WeixinRequest request, WeixinResponse response, + WeixinMessage message, WeixinMessageHandler handler) + throws WeixinException { } @Override - public void afterCompletion(ChannelHandlerContext ctx, + public void afterCompletion(ChannelHandlerContext context, WeixinRequest request, WeixinMessage message, WeixinMessageHandler handler, WeixinException exception) throws WeixinException { diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/WeixinMessageInterceptor.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/WeixinMessageInterceptor.java index b81f654f..565d115e 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/WeixinMessageInterceptor.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/interceptor/WeixinMessageInterceptor.java @@ -30,7 +30,7 @@ public interface WeixinMessageInterceptor { * 微信消息 * @param handler * 消息处理器 - * @return + * @return 返回true执行下一个拦截器 * @throws WeixinException */ boolean preHandle(ChannelHandlerContext ctx, WeixinRequest request, diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageDecoder.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageDecoder.java index e626e8c2..38d3b779 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageDecoder.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageDecoder.java @@ -17,6 +17,7 @@ import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.type.EncryptType; import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.MessageUtil; +import com.foxinmy.weixin4j.util.StringUtil; import com.foxinmy.weixin4j.xml.EncryptMessageHandler; /** @@ -70,10 +71,10 @@ public class WeixinMessageDecoder extends String msgSignature = parameters.containsKey("msg_signature") ? parameters .get("msg_signature").get(0) : ""; message.setMsgSignature(msgSignature); - if (!content.isEmpty()) { if (message.getEncryptType() == EncryptType.AES) { - if (aesToken.getAesKey() == null || aesToken.getAppid() == null) { + if (StringUtil.isBlank(aesToken.getAesKey()) + || StringUtil.isBlank(aesToken.getAppid())) { throw new WeixinException( "AESEncodingKey or AppId not be null in AES mode"); } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageEncoder.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageEncoder.java index c8e046b2..8ce571e7 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageEncoder.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageEncoder.java @@ -1,5 +1,6 @@ package com.foxinmy.weixin4j.socket; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageEncoder; @@ -10,11 +11,14 @@ import org.slf4j.LoggerFactory; import com.foxinmy.weixin4j.bean.AesToken; import com.foxinmy.weixin4j.exception.WeixinException; -import com.foxinmy.weixin4j.request.ResponseMessage; +import com.foxinmy.weixin4j.response.BlankResponse; +import com.foxinmy.weixin4j.response.WeixinResponse; +import com.foxinmy.weixin4j.type.EncryptType; import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.HttpUtil; import com.foxinmy.weixin4j.util.MessageUtil; import com.foxinmy.weixin4j.util.RandomUtil; +import com.foxinmy.weixin4j.util.StringUtil; /** * 微信消息编码类 @@ -27,8 +31,10 @@ import com.foxinmy.weixin4j.util.RandomUtil; * href="http://mp.weixin.qq.com/wiki/0/61c3a8b9d50ac74f18bdf2e54ddfc4e0.html">加密接入指引 */ public class WeixinMessageEncoder extends - MessageToMessageEncoder { + MessageToMessageEncoder { + private final Logger log = LoggerFactory.getLogger(getClass()); + private final AesToken aesToken; public WeixinMessageEncoder(AesToken aesToken) { @@ -36,34 +42,58 @@ public class WeixinMessageEncoder extends } @Override - protected void encode(ChannelHandlerContext ctx, ResponseMessage response, + protected void encode(ChannelHandlerContext ctx, WeixinResponse response, List out) throws WeixinException { - if (aesToken.getAesKey() == null || aesToken.getAppid() == null) { - throw new WeixinException( - "AESEncodingKey or AppId not be null in AES mode"); + EncryptType encryptType = ctx.channel().attr(Consts.ENCRYPTTYPE_KEY) + .get(); + String userOpenId = ctx.channel().attr(Consts.USEROPENID_KEY).get(); + String accountOpenId = ctx.channel().attr(Consts.ACCOUNTOPENID_KEY) + .get(); + if (StringUtil.isBlank(accountOpenId)) { + accountOpenId = aesToken.getAppid(); } - String nonce = RandomUtil.generateString(32); - String timestamp = String.valueOf(System.currentTimeMillis() / 1000l); - String encrtypt = MessageUtil.aesEncrypt(aesToken.getAppid(), - aesToken.getAesKey(), response.toXml()); - String msgSignature = MessageUtil.signature(aesToken.getToken(), nonce, - timestamp, encrtypt); - StringBuilder content = new StringBuilder(); - content.append(""); - content.append(String.format("", nonce)); - content.append(String.format("", - timestamp)); - content.append(String.format( - "", msgSignature)); - content.append(String.format("", - encrtypt)); - content.append(""); - - out.add(HttpUtil.createWeixinMessageResponse(content.toString(), + if (response instanceof BlankResponse) { + content.append(response.toContent()); + } else { + content.append(""); + content.append(String.format( + "", userOpenId)); + content.append(String.format( + "", + accountOpenId)); + content.append(String.format( + "", + System.currentTimeMillis() / 1000l)); + content.append(String.format("", + response.getMsgType())); + content.append(response.toContent()); + content.append(""); + if (encryptType == EncryptType.AES) { + String nonce = RandomUtil.generateString(32); + String timestamp = String + .valueOf(System.currentTimeMillis() / 1000l); + String encrtypt = MessageUtil.aesEncrypt(accountOpenId, + aesToken.getAesKey(), content.toString()); + String msgSignature = MessageUtil.signature( + aesToken.getToken(), nonce, timestamp, encrtypt); + content.delete(0, content.length() - 1); + content.append(""); + content.append(String.format("", + nonce)); + content.append(String.format( + "", timestamp)); + content.append(String.format( + "", + msgSignature)); + content.append(String.format( + "", encrtypt)); + content.append(""); + } + } + ctx.writeAndFlush(HttpUtil.createHttpResponse(content.toString(), OK, Consts.CONTENTTYPE$APPLICATION_XML)); - - log.info("\n=================aes encrtypt out================="); + log.info("\n=================response message================="); log.info("{}", content); } } \ No newline at end of file diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageHandler.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageHandler.java index 2e928056..095af6fc 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageHandler.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/socket/WeixinMessageHandler.java @@ -1,11 +1,12 @@ package com.foxinmy.weixin4j.socket; +import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN; +import static io.netty.handler.codec.http.HttpResponseStatus.METHOD_NOT_ALLOWED; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; -import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.HttpMethod; -import io.netty.handler.codec.http.HttpResponseStatus; -import io.netty.handler.codec.http.HttpVersion; import java.io.ByteArrayInputStream; @@ -26,6 +27,7 @@ import com.foxinmy.weixin4j.util.ClassUtil; import com.foxinmy.weixin4j.util.Consts; import com.foxinmy.weixin4j.util.HttpUtil; import com.foxinmy.weixin4j.util.MessageUtil; +import com.foxinmy.weixin4j.util.StringUtil; /** * 微信被动消息处理类 @@ -74,19 +76,23 @@ public class WeixinMessageHandler extends if (MessageUtil.signature(aesToken.getToken(), request.getTimeStamp(), request.getNonce()).equals( request.getSignature())) { - ctx.write(HttpUtil.createWeixinMessageResponse( - request.getEchoStr(), Consts.CONTENTTYPE$TEXT_PLAIN)); + ctx.writeAndFlush( + HttpUtil.createHttpResponse(request.getEchoStr(), OK, + Consts.CONTENTTYPE$TEXT_PLAIN)).addListener( + ChannelFutureListener.CLOSE); return; } - ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.FORBIDDEN)); + ctx.writeAndFlush( + HttpUtil.createHttpResponse(null, FORBIDDEN, null)) + .addListener(ChannelFutureListener.CLOSE); return; } else if (request.getMethod().equals(HttpMethod.POST.name())) { if (!MessageUtil.signature(aesToken.getToken(), request.getTimeStamp(), request.getNonce()).equals( request.getSignature())) { - ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.FORBIDDEN)); + ctx.writeAndFlush( + HttpUtil.createHttpResponse(null, FORBIDDEN, null)) + .addListener(ChannelFutureListener.CLOSE); return; } if (request.getEncryptType() == EncryptType.AES) { @@ -94,14 +100,16 @@ public class WeixinMessageHandler extends request.getTimeStamp(), request.getNonce(), request.getEncryptContent()).equals( request.getMsgSignature())) { - ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.FORBIDDEN)); + ctx.writeAndFlush( + HttpUtil.createHttpResponse(null, FORBIDDEN, null)) + .addListener(ChannelFutureListener.CLOSE); return; } } } else { - ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, - HttpResponseStatus.METHOD_NOT_ALLOWED)); + ctx.writeAndFlush( + HttpUtil.createHttpResponse(null, METHOD_NOT_ALLOWED, null)) + .addListener(ChannelFutureListener.CLOSE); return; } WeixinMessage weixinMessage = null; @@ -113,6 +121,14 @@ public class WeixinMessageHandler extends } catch (JAXBException e) { throw new WeixinException(e); } + ctx.channel().attr(Consts.ENCRYPTTYPE_KEY) + .set(request.getEncryptType()); + ctx.channel().attr(Consts.USEROPENID_KEY) + .set(weixinMessage.getFromUserName()); + if (StringUtil.isBlank(aesToken.getAppid())) { + ctx.channel().attr(Consts.ACCOUNTOPENID_KEY) + .set(weixinMessage.getToUserName()); + } final WeixinRequest cloneRequest = (WeixinRequest) ClassUtil .deepClone(request); final WeixinMessage cloneMessage = (WeixinMessage) ClassUtil diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java index 459b1ced..77419e93 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java @@ -66,8 +66,8 @@ public final class WeixinServerBootstrap { /** * 明文模式 * - * @param token - * 开发者填写的token + * * @param appid 公众号的唯一ID + * */ public WeixinServerBootstrap(String token) { this(new AesToken(token)); diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/Consts.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/Consts.java index 8a034261..78a76cd3 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/Consts.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/Consts.java @@ -1,7 +1,11 @@ package com.foxinmy.weixin4j.util; +import io.netty.util.AttributeKey; + import java.nio.charset.Charset; +import com.foxinmy.weixin4j.type.EncryptType; + /** * 常量类 * @@ -30,4 +34,11 @@ public final class Consts { public static final String PROTOCOL_JAR = "jar"; public static final String CONTENTTYPE$APPLICATION_XML = "application/xml"; public static final String CONTENTTYPE$TEXT_PLAIN = "text/plain"; + + public static final AttributeKey ENCRYPTTYPE_KEY = AttributeKey + .valueOf("ENCRYPTTYPE"); + public static final AttributeKey ACCOUNTOPENID_KEY = AttributeKey + .valueOf("ACCOUNTOPENID"); + public static final AttributeKey USEROPENID_KEY = AttributeKey + .valueOf("USEROPENID"); } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/HttpUtil.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/HttpUtil.java index ed8aa347..1cfeb710 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/HttpUtil.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/HttpUtil.java @@ -12,6 +12,7 @@ import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; import java.util.Date; @@ -26,10 +27,19 @@ import java.util.Date; */ public class HttpUtil { - public static HttpResponse createWeixinMessageResponse(String content, - String contentType) { + public static HttpResponse createHttpResponse(String content, + HttpResponseStatus status, String contentType) { + if (content == null) { + content = ""; + } + if (StringUtil.isBlank(contentType)) { + contentType = Consts.CONTENTTYPE$TEXT_PLAIN; + } + if (status == null) { + status = OK; + } FullHttpResponse httpResponse = new DefaultFullHttpResponse(HTTP_1_1, - OK, Unpooled.copiedBuffer(content, Consts.UTF_8)); + status, Unpooled.copiedBuffer(content, Consts.UTF_8)); httpResponse.headers().set( CONTENT_TYPE, String.format("%s;encoding=%s", contentType, diff --git a/weixin4j-server/src/test/java/com/foxinmy/weixin4j/server/test/MessageServerStarup.java b/weixin4j-server/src/test/java/com/foxinmy/weixin4j/server/test/MessageServerStarup.java index 248120a4..cc49bcc8 100644 --- a/weixin4j-server/src/test/java/com/foxinmy/weixin4j/server/test/MessageServerStarup.java +++ b/weixin4j-server/src/test/java/com/foxinmy/weixin4j/server/test/MessageServerStarup.java @@ -1,8 +1,13 @@ package com.foxinmy.weixin4j.server.test; +import io.netty.channel.ChannelHandlerContext; + import com.foxinmy.weixin4j.exception.WeixinException; +import com.foxinmy.weixin4j.handler.BlankMessageHandler; import com.foxinmy.weixin4j.handler.DebugMessageHandler; import com.foxinmy.weixin4j.handler.WeixinMessageHandler; +import com.foxinmy.weixin4j.interceptor.MessageInterceptorAdapter; +import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor; import com.foxinmy.weixin4j.request.WeixinMessage; import com.foxinmy.weixin4j.request.WeixinRequest; import com.foxinmy.weixin4j.response.TextResponse; @@ -20,9 +25,9 @@ import com.foxinmy.weixin4j.startup.WeixinServerBootstrap; */ public class MessageServerStarup { - final String appid = "appid"; - final String token = "carsonliu13450438112"; - final String aesKey = "aeskey"; + final String appid = "公众号appid"; + final String token = "开发者token"; + final String aesKey = "aes加密密钥"; /** * 明文模式 @@ -78,7 +83,38 @@ public class MessageServerStarup { .startup(); } + public void test5() throws WeixinException { + // 拦截所有请求 + WeixinMessageInterceptor interceptor = new MessageInterceptorAdapter() { + @Override + public boolean preHandle(ChannelHandlerContext context, + WeixinRequest request, WeixinMessage message, + WeixinMessageHandler handler) throws WeixinException { + context.write(new TextResponse("所有消息被拦截了!")); + return false; + } + + @Override + public void postHandle(ChannelHandlerContext context, + WeixinRequest request, WeixinResponse response, + WeixinMessage message, WeixinMessageHandler handler) + throws WeixinException { + System.err.println("preHandle返回为true,执行handler后"); + } + + @Override + public void afterCompletion(ChannelHandlerContext context, + WeixinRequest request, WeixinMessage message, + WeixinMessageHandler handler, WeixinException exception) + throws WeixinException { + System.err.println("请求处理完毕"); + } + }; + new WeixinServerBootstrap(token).addInterceptor(interceptor) + .addHandler(BlankMessageHandler.global).startup(); + } + public static void main(String[] args) throws WeixinException { - new MessageServerStarup().test4(); + new MessageServerStarup().test5(); } }