Ejemplo n.º 1
0
  /**
   * 获取access_token
   *
   * @return
   */
  public String getAccessToken() {
    TokenDtoUnionId dtoUnionId = null;
    String access_token = (String) servletContext.getAttribute("access_token");
    Long createDate = (Long) servletContext.getAttribute("createDate");
    Integer expires_in = (Integer) servletContext.getAttribute("expires_in");

    if (createDate == null || expires_in == null || access_token == null) {
      System.out.println("accessToken为空的!");
      dtoUnionId = UserUtils.getAccessToken();
      servletContext.setAttribute("access_token", dtoUnionId.getAccess_token());
      servletContext.setAttribute("createDate", DateUtils.getTimeStamp());
      servletContext.setAttribute("expires_in", dtoUnionId.getExpires_in());
    } else {

      long nowStamp = DateUtils.getTimeStamp();
      long l = nowStamp - createDate;

      System.out.println("时间是:" + l);
      if (l > (expires_in - 1000)) {
        System.out.println("accessToken已过期!");
        dtoUnionId = UserUtils.getAccessToken();
        servletContext.setAttribute("access_token", dtoUnionId.getAccess_token());
        servletContext.setAttribute("createDate", DateUtils.getTimeStamp());
        servletContext.setAttribute("expires_in", dtoUnionId.getExpires_in());
      }
    }

    return dtoUnionId == null ? access_token : dtoUnionId.getAccess_token();
  }
Ejemplo n.º 2
0
  /**
   * 生成签名并放在session里
   *
   * @param url
   * @param state
   * @param accessToken
   */
  public void setSignatureToSession(String url, String state, String accessToken) {

    System.out.println(url);
    String signature = (String) request.getSession().getAttribute("signature" + state);
    String timestamp = (String) request.getSession().getAttribute("timestamp" + state);
    String nonceStr = (String) request.getSession().getAttribute("nonceStr" + state);
    String ticket = (String) request.getSession().getAttribute("ticket" + state);

    if (ticket == null) {
      System.out.println("ticket is null");
      ticket = UserUtils.getTicket(accessToken);
      if (ticket == null) {
        servletContext.removeAttribute("expires_in");
        servletContext.removeAttribute("access_token");
        String access_token = getAccessToken();
        ticket = UserUtils.getTicket(access_token);
      }
      request.getSession().setAttribute("ticket" + state, ticket);
    }

    Map<String, Object> maps = GenerateSignature.getSignature(ticket, url);
    nonceStr = (String) maps.get("nonceStr");
    signature = (String) maps.get("signature");
    timestamp = (String) maps.get("timestamp");

    request.getSession().setAttribute("appId", UrlString.appId);
    request.getSession().setAttribute("timestamp" + state, timestamp);
    request.getSession().setAttribute("signature" + state, signature);
    request.getSession().setAttribute("nonceStr" + state, nonceStr);
  }