package com.dxg.wechatdemo.weixin.pm.controller;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dxg.wechatdemo.weixin.pm.config.WechatMpProperties;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author Binary Wang(https://github.com/binarywang)
*/
@Controller
@RequestMapping("/wechat/portal")
public class WechatController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private WxMpService wxService;
@Autowired
private WxMpMessageRouter router;
@Resource
WechatMpProperties wmProperties;
@GetMapping(produces = "text/plain;charset=utf-8")
public void authGet(@RequestParam(name = "signature", required = false) String signature,
@RequestParam(name = "timestamp", required = false) String timestamp,
@RequestParam(name = "nonce", required = false) String nonce,
@RequestParam(name = "echostr", required = false) String echostr, HttpServletResponse response) {
this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
throw new IllegalArgumentException("请求参数非法,请核实!");
}
if (this.wxService.checkSignature(timestamp, nonce, signature)) {
try {
response.getWriter().write(echostr);
response.getWriter().flush();
response.getWriter().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return echostr;
}
// return "非法请求";
}
@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/xml;charset=UTF-8");
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String encType = request.getParameter("encrypt_type");
String msgSignature = request.getParameter("msg_signature");
StringBuilder xmlMsg = new StringBuilder();
// 处理接收消息
try {
ServletInputStream in = request.getInputStream();
// 将流转换为字符串
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1; ) {
xmlMsg.append(new String(b, 0, n, "UTF-8"));
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String requestBody = xmlMsg.toString();
this.logger.info(
"\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}],"
+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
signature, encType, msgSignature, timestamp, nonce, requestBody);
if (!this.wxService.checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
}
String out = null;
if (encType == null) {
// 明文传输的消息
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
WxMpXmlOutMessage outMessage = this.route(inMessage);
if (outMessage == null) {
return "";
}
out = outMessage.toXml();
} else if ("aes".equals(encType)) {
// aes加密的消息
WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody,
this.wxService.getWxMpConfigStorage(), timestamp, nonce, msgSignature);
this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
WxMpXmlOutMessage outMessage = this.route(inMessage);
if (outMessage == null) {
return "";
}
out = outMessage.toEncryptedXml(this.wxService.getWxMpConfigStorage());
}
this.logger.debug("\n组装回复信息:{}", out);
try {
response.getWriter().write(out);
response.getWriter().flush();
response.getWriter().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public void weixinRedirect(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("application/json;charset=UTF-8");
String path = request.getParameter("path");
if (path == null) {
path = "?";
} else {
path = "%3fpath%3d" + path + "&";
}
this.logger.info("\n path = {} \n", path);
String redirectUrl = new String("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wmProperties.getAppId() + "&redirect_uri=http%3a%2f%2f" + wmProperties.getHost() + "%2fwechat%2fportal%2fgetUserOpenId" + path + "response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect");
logger.info("redirectUrl:" + redirectUrl);
response.sendRedirect(redirectUrl);
}
/* @GetMapping(value = "/getUserOpenId")
public void getUserOpenId(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("application/json;charset=UTF-8");
//参数
String code = request.getParameter("code");
String path = request.getParameter("path");
if (path == null) {
path = "";
}
WxMpOAuth2AccessToken accessToken = this.wxService.oauth2getAccessToken(code);
this.logger.info("\n code = {} \n openId = {} \n path = {} \n",code,accessToken.getOpenId(),path);
FcConsumerLoginInfo consumerLoginInfo = this.wechatMpService.getConsumerLoginInfo(accessToken.getOpenId());
String tokenStr = URLEncoder.encode(consumerLoginInfo.getToken(),"UTF-8");
String redirectUrl = new String(path+"?openId="+accessToken.getOpenId()+"&token="+tokenStr);
logger.info("公众号重定向url是:"+redirectUrl);
response.sendRedirect(redirectUrl);
}*/
//
// @RequestMapping(value = "/getWeixinOpenId", method = RequestMethod.GET)
// public void getWeixinOpenIdRedirect(HttpServletRequest request, HttpServletResponse response) throws Exception {
// response.setContentType("application/json;charset=UTF-8");
// this.logger.info("接口 getWeixinOpenId url = [{}] , \n ",request.getRequestURL());
// String redirectUrl = new String("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx07472b5042a0e627&redirect_uri=http%3a%2f%2f"+WxConstant.WX_HOST+"%2fwechat%2fportal
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
spring boot接入微信最完整demo (162个子文件)
WechatController.class 7KB
WechatMpConfiguration.class 6KB
WxJSUtil.class 5KB
WxMenuController.class 4KB
ScanHandler.class 4KB
MsgHandler.class 4KB
HttpRequestUtil.class 3KB
LocationHandler.class 3KB
SubscribeHandler.class 3KB
MenuHandler.class 2KB
WxQcCodeController.class 2KB
WechatMpProperties.class 2KB
NewsBuilder.class 2KB
UnsubscribeHandler.class 2KB
LogHandler.class 2KB
ImageBuilder.class 1KB
TextBuilder.class 1KB
StoreCheckNotifyHandler.class 1KB
KfSessionHandler.class 1KB
NullHandler.class 1KB
WechatdemoApplication.class 725B
AbstractBuilder.class 721B
WechatdemoApplicationTests.class 644B
JsonUtils.class 597B
AbstractHandler.class 592B
mvnw.cmd 6KB
.gitignore 268B
wechatdemo.iml 12KB
maven-wrapper.jar 47KB
WechatController.java 8KB
WxMenuController.java 8KB
WechatMpConfiguration.java 6KB
WxJSUtil.java 4KB
HttpRequestUtil.java 3KB
ScanHandler.java 2KB
MsgHandler.java 2KB
WxQcCodeController.java 2KB
LocationHandler.java 2KB
WechatMpProperties.java 2KB
SubscribeHandler.java 2KB
MenuHandler.java 1KB
UnsubscribeHandler.java 990B
LogHandler.java 904B
StoreCheckNotifyHandler.java 825B
KfSessionHandler.java 776B
NewsBuilder.java 762B
ImageBuilder.java 745B
NullHandler.java 742B
TextBuilder.java 737B
AbstractBuilder.java 608B
AbstractHandler.java 368B
WechatdemoApplicationTests.java 348B
WechatdemoApplication.java 330B
JsonUtils.java 299B
mvnw 9KB
maven-wrapper.properties 116B
workspace.xml 52KB
pom.xml 4KB
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_1_0_RELEASE.xml 769B
Maven__org_springframework_boot_spring_boot_starter_thymeleaf_2_1_0_RELEASE.xml 762B
Maven__org_springframework_boot_spring_boot_starter_logging_2_1_0_RELEASE.xml 748B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_1_0_RELEASE.xml 741B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_1_0_RELEASE.xml 734B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_9_7.xml 729B
Maven__org_springframework_boot_spring_boot_starter_test_2_1_0_RELEASE.xml 727B
Maven__org_springframework_boot_spring_boot_starter_json_2_1_0_RELEASE.xml 727B
Maven__org_thymeleaf_extras_thymeleaf_extras_java8time_3_0_1_RELEASE.xml 725B
Maven__org_springframework_plugin_spring_plugin_metadata_1_2_0_RELEASE.xml 721B
Maven__org_springframework_boot_spring_boot_starter_web_2_1_0_RELEASE.xml 720B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 700B
Maven__org_springframework_plugin_spring_plugin_core_1_2_0_RELEASE.xml 693B
Maven__org_springframework_boot_spring_boot_starter_2_1_0_RELEASE.xml 692B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_9_7.xml 688B
Maven__org_hibernate_validator_hibernate_validator_6_0_13_Final.xml 681B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_9_7.xml 674B
Maven__org_springframework_boot_spring_boot_test_2_1_0_RELEASE.xml 671B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_12.xml 660B
Maven__org_springframework_spring_expression_5_1_2_RELEASE.xml 658B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_9_0.xml 644B
compiler.xml 643B
Maven__org_thymeleaf_thymeleaf_spring5_3_0_11_RELEASE.xml 641B
Maven__org_apache_tomcat_embed_tomcat_embed_jasper_9_0_12.xml 639B
Maven__org_springframework_spring_context_5_1_2_RELEASE.xml 637B
Maven__org_springframework_boot_spring_boot_2_1_0_RELEASE.xml 636B
Maven__org_apache_tomcat_tomcat_annotations_api_9_0_12.xml 636B
misc.xml 632B
Maven__org_springframework_spring_webmvc_5_1_2_RELEASE.xml 630B
Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_12.xml 625B
Maven__com_fasterxml_jackson_core_jackson_databind_2_9_7.xml 623B
Maven__org_springframework_spring_beans_5_1_2_RELEASE.xml 623B
Maven__io_springfox_springfox_swagger_common_2_6_1.xml 623B
Maven__com_github_binarywang_weixin_java_common_2_9_0.xml 617B
Maven__org_springframework_spring_core_5_1_2_RELEASE.xml 616B
Maven__org_springframework_spring_test_5_1_2_RELEASE.xml 616B
Maven__javax_annotation_javax_annotation_api_1_3_2.xml 611B
Maven__org_apache_tomcat_embed_tomcat_embed_el_9_0_12.xml 611B
Maven__javax_validation_validation_api_2_0_1_Final.xml 611B
Maven__org_springframework_spring_jcl_5_1_2_RELEASE.xml 609B
Maven__org_springframework_spring_web_5_1_2_RELEASE.xml 609B
Maven__org_springframework_spring_aop_5_1_2_RELEASE.xml 609B
共 162 条
- 1
- 2
0老船长0
- 粉丝: 12
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0