摘要:本文将带你了解微信在成为开发者时的校验 ,希望本文对大家学微信有所帮助。
当我们点击提交后,微信服务器将发送GET请求到填写的服务器地址URL上,GET请求携带四个参数,调用url的地址
[java] view plain copy 1. import java.io.InputStream; 2. import java.io.PrintWriter; 3. import java.util.Map; 4. 5. import javax.servlet.http.HttpServletRequest; 6. import javax.servlet.http.HttpServletResponse; 7. 8. import org.apache.log4j.Logger; 9. import org.springframework.stereotype.Controller; 10. import org.springframework.web.bind.annotation.RequestMapping; 11. import org.springframework.web.bind.annotation.RequestMethod; 12. import org.springframework.web.bind.annotation.RequestParam; 13. 14. import com.weixin.util.MessageUtil; 15. import com.weixin.util.SignUtil; 16. import com.weixin.wechat.dispatcher.EventDispatcher; 17. import com.weixin.wechat.dispatcher.MsgDispatcher; 18. 19. @Controller 20. @RequestMapping("/wechat") 21. public class WechatSecurity { 22. private static Logger logger = Logger.getLogger(WechatSecurity.class); 23. 24. /** 25. * 这是验证成为开发者时调到的,如果是推送信息使用的是post 26. * @Description: 用于接收 get 参数,返回验证参数 27. * @param @param request 28. * @param @param response 29. * @param @param signature 30. * @param @param timestamp 31. * @param @param nonce 32. * @param @param echostr 33. * @author dapengniao 34. * @date 2016 年 3 月 4 日 下午 6:20:00 35. */ 36. @RequestMapping(value = "/security", method = RequestMethod.GET) 37. public void doGet( 38. HttpServletRequest request, 39. HttpServletResponse response, 40. @RequestParam(value = "signature", required = true) String signature, 41. @RequestParam(value = "timestamp", required = true) String timestamp, 42. @RequestParam(value = "nonce", required = true) String nonce, 43. @RequestParam(value = "echostr", required = true) String echostr) { 44. try { 45. System.out.println("get"); 46. if (SignUtil.checkSignature(signature, timestamp, nonce)) { 47. PrintWriter out = response.getWriter(); 48. out.print(echostr);//签名正确,成功接连 49. out.close(); 50. } else { 51. logger.info("这里存在非法请求!"); 52. } 53. } catch (Exception e) { 54. logger.error(e, e); 55. } 56. } 验证通过就成为开发者,之后的信息推送通过post请求 [java] view plain copy 1. /** 2. * post 方法用于接收微信服务端消息,文本,图片,url等 3. * @param request 4. * @param response 5. */ 6. @RequestMapping(value = "/security", method = RequestMethod.POST) 7. public void DoPost(HttpServletRequest request, HttpServletResponse response) { 8. System.out.println("这是 post 方法!"); 9. String result = ""; 10. try { 11. //从 request 中取得输入流 12. InputStream inputStream = request.getInputStream(); 13. //把xml文档解释成map 14. Map<String, String> map = MessageUtil.parseXml(inputStream); 15. String msgtype = map.get("MsgType"); 16. if (MessageUtil.REQ_MESSAGE_TYPE_EVENT.equals(msgtype)) { 17. result = EventDispatcher.processEvent(map); // 进入事件处理 18. } else { 19. result = MsgDispatcher.processMessage(map); // 进入消息处理 20. } 21. //结果回复 22. PrintWriter out = response.getWriter(); 23. out.print(result); 24. out.close(); 25. out = null; 26. } catch (Exception e) { 27. logger.error(e, e); 28. } 29. }
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号