Search in sources :

Example 1 with UrlInfoBean

use of com.github.liuweijw.business.wechat.beans.UrlInfoBean in project fw-cloud-framework by liuweijw.

the class WxAuthorizeController method authorize.

// https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
@RequestMapping(value = "/authorize", method = RequestMethod.GET)
public String authorize(HttpServletRequest request, @RequestParam("_backUrl") String _backUrl, Integer from, @RequestParam("wechatId") String wechatId, Long t) {
    long start = System.currentTimeMillis();
    log.info("【wxauth.authorize】_backUrl:" + _backUrl);
    from = null == from || from < 0 ? 0 : from;
    t = null == t || t < 0 ? 0 : t;
    String returnUrl = properties.getAuthUrl() + "/wechat/auth/openId?wechatId=" + wechatId + "&from=" + from + "&t=" + t;
    // from == 1 已经关注(静默登录) from == 0 通过网页(用户授权)
    String state = WebUtils.buildURLEncoder(_backUrl);
    log.info("【wxauth.authorize】state:" + state);
    if (state.length() > 112) {
        // 微信端最长允许128-16(#wechat_redirect),长度超过改为项目段链接标识替换
        state = RandomHelper.randomStringUpper();
        UrlInfoBean urlInfoBean = new UrlInfoBean(state, _backUrl);
        urlInfoService.cacheUrlInfo(urlInfoBean);
    }
    String redirectURL = wxService.oauth2buildAuthorizationUrl(returnUrl, from.intValue() == 1 ? WxConsts.OAuth2Scope.SNSAPI_BASE : WxConsts.OAuth2Scope.SNSAPI_USERINFO, state);
    // 微信默认会发送两次回调请求问题处理 -设置之后还是一样问题暂未解决,目前调用服务端采用其它方式规避此问题
    // https://blog.csdn.net/jiangguilong2000/article/details/79416615
    // https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=https&response_type=code&scope=snsapi_base&state=xxx#wechat_redirect
    // if (!redirectURL.contains("connect_redirect=")) {
    // redirectURL = redirectURL.replace("#wechat_redirect",
    // "&connect_redirect=1#wechat_redirect");
    // }
    log.info("【wxauth.authorize】redirect:" + redirectURL);
    long end = System.currentTimeMillis();
    log.info("【wxauth.authorize】耗时:" + (end - start));
    log.info("【wxauth.authorize】请求从第三方应用到跳转授权开始[" + t + "],耗时:" + (end - t));
    return "redirect:" + redirectURL;
}
Also used : UrlInfoBean(com.github.liuweijw.business.wechat.beans.UrlInfoBean) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UrlInfoBean

use of com.github.liuweijw.business.wechat.beans.UrlInfoBean in project fw-cloud-framework by liuweijw.

the class WxAuthorizeController method openId.

// 如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE
@RequestMapping(value = "/openId", method = RequestMethod.GET)
public String openId(HttpServletRequest request, @RequestParam("code") String code, @RequestParam("state") String state, @RequestParam("from") Integer from, @RequestParam("wechatId") String wechatId, @RequestParam("t") Long t) {
    long start = System.currentTimeMillis();
    String openId = "";
    try {
        boolean isSopeBase = from.intValue() == 1;
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxService.oauth2getAccessToken(code);
        openId = wxMpOAuth2AccessToken.getOpenId();
        log.info("【wxauth.openId】:state|" + state);
        // 采用异步方式拉取用户信息
        // taskExecutor.execute(() -> {})
        long mqStart = System.currentTimeMillis();
        log.info("【wxauth.openId】发送MQ:" + mqStart);
        WechatNotifyBean wechatNotifyBean = new WechatNotifyBean();
        wechatNotifyBean.setSopeBase(isSopeBase);
        wechatNotifyBean.setWechatId(wechatId);
        wechatNotifyBean.setWxMpOAuth2AccessToken(wxMpOAuth2AccessToken);
        rabbitTemplate.convertAndSend(MqQueueConstant.WECHAT_QUEUE, wechatNotifyBean);
        log.info("【wxauth.openId】发送MQ耗时:" + (System.currentTimeMillis() - mqStart));
        log.info("【wxauth.openId】:openId|" + openId);
    } catch (WxErrorException ex) {
        ex.printStackTrace();
        log.info("【wxauth.openId】exception:" + ex.getError().getErrorMsg());
    }
    String returnUrl = "";
    if (state.length() == 32 && !state.startsWith("http")) {
        // key
        UrlInfoBean urlInfoBean = urlInfoService.findFromCacheByUuid(state);
        returnUrl = urlInfoBean.getUrl();
    } else {
        returnUrl = state;
    }
    String redirectUrl = RequestUtil.buildAppendURLParams(RequestUtil.buildURLParams(returnUrl, OPENID), OPENID + "=" + openId, "t=" + t);
    log.info("【wxauth.openId】:redirect|" + redirectUrl);
    long end = System.currentTimeMillis();
    log.info("【wxauth.openId】耗时:" + (end - start));
    log.info("【wxauth.authorize】请求从第三方应用到跳转授权开始[" + t + "],耗时:" + (end - t));
    return "redirect:" + redirectUrl;
}
Also used : WxMpOAuth2AccessToken(me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken) WechatNotifyBean(com.github.liuweijw.business.wechat.beans.WechatNotifyBean) UrlInfoBean(com.github.liuweijw.business.wechat.beans.UrlInfoBean) WxErrorException(me.chanjar.weixin.common.error.WxErrorException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UrlInfoBean

use of com.github.liuweijw.business.wechat.beans.UrlInfoBean in project fw-cloud-framework by liuweijw.

the class UrlInfoServiceImpl method findFromCacheByUuid.

@Override
public UrlInfoBean findFromCacheByUuid(String uuid) {
    log.info("从缓存查询微信UrlInfo[uuid]:" + uuid);
    Cache cache = cacheManager.getCache(WECHAT_URL_INFO);
    Optional<UrlInfoBean> optional = Optional.ofNullable(cache.get(uuid, UrlInfoBean.class));
    if (optional.isPresent()) {
        log.info("从缓存查询微信UrlInfo[uuid]:" + uuid + "查询成功");
        cache.evict(uuid);
        return optional.get();
    }
    log.error("从缓存查询微信UrlInfo[uuid]:" + uuid + "未查询到");
    UrlInfo dbUserinfo = findByUuid(uuid);
    if (null == dbUserinfo) {
        log.error("从db查询微信UrlInfo[uuid]:" + uuid + "未查询到");
        return new UrlInfoBean();
    }
    log.info("从db查询微信UrlInfo[uuid]:" + uuid + "查询成功");
    return new UrlInfoBean(dbUserinfo.getUuid(), dbUserinfo.getUrl());
}
Also used : UrlInfoBean(com.github.liuweijw.business.wechat.beans.UrlInfoBean) QUrlInfo(com.github.liuweijw.business.wechat.domain.QUrlInfo) UrlInfo(com.github.liuweijw.business.wechat.domain.UrlInfo) Cache(org.springframework.cache.Cache)

Aggregations

UrlInfoBean (com.github.liuweijw.business.wechat.beans.UrlInfoBean)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 WechatNotifyBean (com.github.liuweijw.business.wechat.beans.WechatNotifyBean)1 QUrlInfo (com.github.liuweijw.business.wechat.domain.QUrlInfo)1 UrlInfo (com.github.liuweijw.business.wechat.domain.UrlInfo)1 WxErrorException (me.chanjar.weixin.common.error.WxErrorException)1 WxMpOAuth2AccessToken (me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken)1 Cache (org.springframework.cache.Cache)1