/** * 回调请求接收 * * @throws IOException * @throws HttpException */ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws HttpException, IOException { String accessType = request.getParameter(getAccessTypeRequestParameterName(request)); if (StringUtils.isNotBlank(accessType) && accessType.trim().equalsIgnoreCase("update")) { updateAccessToken(request); } Map accessToken = getAccessTokenInCache(request); // 若存在授权信息,检查其是否过期 if (accessToken != null) { Date getDate = (Date) accessToken.get("get_date"); int expiresIn = Integer.parseInt(accessToken.get("expires_in").toString()); // 判断是否过期 if (getDate.getTime() + (expiresIn * 1000) <= System.currentTimeMillis()) { String appId = getAppId(request); String refreshToken = (String) accessToken.get("refresh_token"); Map result = new HashMap(); // 执行刷新缓存操作 result.put("get_date", new Date()); WebAuthApi.refreshAccessToken(appId, refreshToken, result); // 若获取失败,则当无accessToken if (result.containsKey("access_token")) accessToken = result; else accessToken = null; putAccessTokenInCache(request, accessToken); } } if (accessToken != null) handleAccessToken(request, response, accessToken); else if (StringUtils.isNotBlank(accessType) && accessType.trim().equalsIgnoreCase("update")) handleWebAuthDisable(request, response); else handleAccessTokenNotFound(request, response); }
/** * 更新accessToken * * @param request * @throws HttpException * @throws IOException */ private void updateAccessToken(HttpServletRequest request) throws HttpException, IOException { String code = request.getParameter("code"); // 用户不禁止授权 if (StringUtils.isNotBlank(code)) { Map result = new HashMap(); result.put("get_date", new Date()); String appId = getAppId(request); String secret = getSecret(request); WebAuthApi.getAccessToken(appId, secret, code, result); // 成功获取accessToken if (StringUtils.isNotBlank((String) result.get("access_token"))) putAccessTokenInCache(request, result); } }
/** * 当不存在access时,执行的操作(可以根据特性需要,覆盖该函数) * * @param request * @param response * @throws IOException */ protected void handleAccessTokenNotFound(HttpServletRequest request, HttpServletResponse response) throws IOException { String state = request.getParameter("state"); String scope = request.getParameter("scope"); WebAuthScope webAuthScope = WebAuthScope.SNSAPI_BASE.toString().equalsIgnoreCase(scope) ? WebAuthScope.SNSAPI_BASE : WebAuthScope.SNSAPI_USERINFO; String url = getCurrentUrl(request); if (StringUtils.isNotBlank(request.getQueryString())) url += "&"; else url += "?"; url += getAccessTypeRequestParameterName(request) + "=update"; url = WebAuthApi.getAuthUrl(getAppId(request), url, webAuthScope, state); // 从定向至微信 response.sendRedirect(url); }
/** * 处理accessToken数据(可根据实际需要,覆盖该方法) * * @param request * @param response * @param accessToken * @param openid * @param scope * @throws IOException * @throws HttpException */ protected void handleAccessToken( HttpServletRequest request, HttpServletResponse response, String accessToken, String openid, String scope) throws HttpException, IOException { AuthUser authUser = null; // 根据openId,获取用户信息 if (StringUtils.isNotBlank(openid)) { authUser = getAuthUserInCache(request, accessToken, openid, getRequestUserLang(request).toString()); if (authUser == null) { authUser = WebAuthApi.getUser(accessToken, openid, getRequestUserLang(request).toString()); putAuthUserInCache( request, accessToken, openid, getRequestUserLang(request).toString(), authUser); } } handleAuthUser( request, response, accessToken, openid, scope, authUser, request.getParameter("state")); }