Ejemplo n.º 1
0
  /**
   * 功能:授权后的回调请求处理
   *
   * @throws Exception
   */
  @RequestMapping(value = "/wechat/oAuth")
  public String oAuth(Signer signer, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    // 防止中文乱码
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");

    // 用户同意授权后,能获取到code
    String code = signer.getCode();

    Signer signInfo = SignUtil.getSignInfo(signer);
    String appId = signInfo.getAppid();
    String appSecret = signInfo.getAppsecret();

    // 用户同意授权
    if (!"authdeny".equals(code)) {
      // 获取网页授权acess_token
      WeixinOauth2Token weixinOauth2Token =
          AdvancedUtil.getOauth2AccessToken(appId, appSecret, code);
      // 网页授权接口访问凭证
      String accessToken = weixinOauth2Token.getAccessToken();
      // 用户标识openId
      String openId = weixinOauth2Token.getOpenId();
      // 获取用户信息
      SNSUserInfo snsUserInfo = AdvancedUtil.getSNSUserInfo(accessToken, openId);
      // 设置要传递参数
      request.setAttribute("snsUserInfo", snsUserInfo);
    }
    // 跳转到oAuth.jsp
    return "oAuth";
  }
Ejemplo n.º 2
0
  /**
   * 功能:微信首页
   *
   * @throws Exception
   */
  @RequestMapping(value = "/wechat/home")
  public void home(Signer signer, HttpServletRequest request, HttpServletResponse response)
      throws Exception {

    // 防止中文乱码
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    // 随机字符串
    String echostr = signer.getEchostr();
    // 初始化用于判断微信请求方法GET/POST
    String method = request.getMethod();

    PrintWriter out = null;
    try {
      out = response.getWriter();
      if (SignUtil.checkSignature(signer)) {
        if ("GET".equals(method)) {
          out.print(echostr);
        } else {
          // 调用核心服务类接收处理请求
          String respXml = wechatService.processRequest(request);
          out.print(respXml);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      out.close();
      out = null;
    }
  }