@Override
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);

    String code = request.getParameter("code");
    try {
      response.getWriter().println("<h1>code</h1>");
      response.getWriter().println(code);

      WxMpOAuth2AccessToken wxMpOAuth2AccessToken = this.wxMpService.oauth2getAccessToken(code);
      response.getWriter().println("<h1>access token</h1>");
      response.getWriter().println(wxMpOAuth2AccessToken.toString());

      WxMpUser wxMpUser = this.wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
      response.getWriter().println("<h1>user info</h1>");
      response.getWriter().println(wxMpUser.toString());

      wxMpOAuth2AccessToken =
          this.wxMpService.oauth2refreshAccessToken(wxMpOAuth2AccessToken.getRefreshToken());
      response.getWriter().println("<h1>after refresh</h1>");
      response.getWriter().println(wxMpOAuth2AccessToken.toString());

    } catch (WxErrorException e) {
      e.printStackTrace();
    }

    response.getWriter().flush();
    response.getWriter().close();
  }
  public WxMpOAuth2AccessToken deserialize(
      JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException {
    WxMpOAuth2AccessToken accessToken = new WxMpOAuth2AccessToken();
    JsonObject accessTokenJsonObject = json.getAsJsonObject();

    if (accessTokenJsonObject.get("access_token") != null
        && !accessTokenJsonObject.get("access_token").isJsonNull()) {
      accessToken.setAccessToken(GsonHelper.getAsString(accessTokenJsonObject.get("access_token")));
    }
    if (accessTokenJsonObject.get("expires_in") != null
        && !accessTokenJsonObject.get("expires_in").isJsonNull()) {
      accessToken.setExpiresIn(
          GsonHelper.getAsPrimitiveInt(accessTokenJsonObject.get("expires_in")));
    }
    if (accessTokenJsonObject.get("refresh_token") != null
        && !accessTokenJsonObject.get("refresh_token").isJsonNull()) {
      accessToken.setRefreshToken(
          GsonHelper.getAsString(accessTokenJsonObject.get("refresh_token")));
    }
    if (accessTokenJsonObject.get("openid") != null
        && !accessTokenJsonObject.get("openid").isJsonNull()) {
      accessToken.setOpenId(GsonHelper.getAsString(accessTokenJsonObject.get("openid")));
    }
    if (accessTokenJsonObject.get("scope") != null
        && !accessTokenJsonObject.get("scope").isJsonNull()) {
      accessToken.setScope(GsonHelper.getAsString(accessTokenJsonObject.get("scope")));
    }
    return accessToken;
  }
示例#3
0
  /**
   * 网页授权回调
   *
   * @param request
   * @param response
   */
  @RequestMapping("oauth")
  public void oauth(HttpServletRequest request, HttpServletResponse response) {

    try {
      WxMpService wxService = BuildAndGetWxService.buildAndGetWxService();
      String state = request.getParameter("state");
      String openId = null;
      Owners owner = null;
      LogBuilder.writeToLog("error in oauth");
      if (request.getSession().getAttribute("openId") == null) {
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken =
            wxService.oauth2getAccessToken(request.getParameter("code"));
        openId = wxMpOAuth2AccessToken.getOpenId();
        LogBuilder.writeToLog(openId);
        request.getSession().setAttribute("openId", openId);
      } else {
        openId = (String) request.getSession().getAttribute("openId");
        LogBuilder.writeToLog("error");
      }
      if (state.equals("1")) {
        response.sendRedirect("/owner/ownerBind?openId=" + openId);
        return;
      } else if (state.equals("2")) {
        owner = (Owners) request.getSession().getAttribute("owner");
        if (owner == null) {
          owner = ownerService.getOwnerByOpenId(openId);
        }
        if (owner != null) {
          request.getSession().setAttribute("owner", owner);
          response.sendRedirect("/owner/ownerProfile?openId=" + openId);
          return;
        } else {
          request.getSession().setAttribute("visitor", "visitor");
        }
        response.sendRedirect("/owner/ownerBind?openId=" + openId);
        return;
      } else if (state.equals("3")) {

        owner = (Owners) request.getSession().getAttribute("owner");
        if (owner == null) {
          owner = ownerService.getOwnerByOpenId(openId);
        }
        if (owner != null) {
          request.getSession().setAttribute("owner", owner);

        } else {
          request.getSession().setAttribute("visitor", "visitor");
        }
        response.sendRedirect("/custom/index.jsp");
        return;
      } else if (state.equals("4")) {
        owner = (Owners) request.getSession().getAttribute("owner");
        if (owner == null) {
          owner = ownerService.getOwnerByOpenId(openId);
        }
        if (owner != null) {
          request.getSession().setAttribute("owner", owner);
          response.sendRedirect("/custom/comment.jsp");
          return;
        } else {
          request.getSession().setAttribute("visitor", "visitor");
          response.sendRedirect("/owner/ownerBind?openId=" + openId);
          return;
        }

      } else if (state.equals("5")) {
        owner = (Owners) request.getSession().getAttribute("owner");
        if (owner == null) {
          owner = ownerService.getOwnerByOpenId(openId);
        }
        if (owner != null) {
          request.getSession().setAttribute("owner", owner);
          response.sendRedirect("/custom/repair.jsp");
          return;
        } else {
          request.getSession().setAttribute("visitor", "visitor");
          response.sendRedirect("/owner/ownerBind?openId=" + openId);
          return;
        }

      } else if (state.equals("6")) {
        owner = (Owners) request.getSession().getAttribute("owner");
        if (owner == null) {
          owner = ownerService.getOwnerByOpenId(openId);
        }
        if (owner != null) {
          request.getSession().setAttribute("owner", owner);
          response.sendRedirect("/owner/myChargeAndMessage?openId=" + openId);
          return;
        } else {
          request.getSession().setAttribute("visitor", "visitor");
          response.sendRedirect("/owner/ownerBind?openId=" + openId);
          return;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      LogBuilder.writeToLog(e.getMessage());
    }
  }