转:支付宝授权第三方登陆(笔记)

转:支付宝授权第三方登陆(笔记)

转载链接:转载

自己写一下避免找不到

一、maven

  <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
  </dependency>
  <dependency>
      <groupId>com.alipay.sdk</groupId>
      <artifactId>alipay-sdk-java</artifactId>
      <version>3.4.49.ALL</version>
  </dependency>

二、工具

@Slf4j
@Component
public class AliUtils {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取支付宝参数
     *
     * @param request
     * @return
     */
    public Map<String, String> getAliPayParam(HttpServletRequest request) {
        Map<String, String> map = new HashMap();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决,这段代码在出现乱码时使用
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            map.put(name, valueStr);
            log.info("接受支付宝回调参数:{}", map);
        }
        return map;
    }


    public AlipaySystemOauthTokenResponse getAliUserToken(String code, AlipayClient alipayClient) throws AlipayApiException {
        return getAliUserToken(code, alipayClient, 0);
    }

    /**
     * 获取token
     *
     * @param code         auth_code
     * @param alipayClient 支付宝构建的客户端
     * @param number
     * @return
     * @throws AlipayApiException
     */
    public AlipaySystemOauthTokenResponse getAliUserToken(String code, AlipayClient alipayClient, int number) throws AlipayApiException {
        AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
        alipaySystemOauthTokenRequest.setGrantType("authorization_code");
        alipaySystemOauthTokenRequest.setCode(code);
        AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(alipaySystemOauthTokenRequest);
        log.info("获得用户+++++++++++++++token:{}+++++++++++++++", oauthTokenResponse.getAccessToken());
        log.info("获得用户+++++++++++++++uuid:{}+++++++++++++++", oauthTokenResponse.getUserId());
        return oauthTokenResponse;
    }

    public AlipayUserInfoShareResponse getUserInfo(AlipayClient alipayClient, AlipaySystemOauthTokenResponse aliUserToken) throws AlipayApiException {
        return getUserInfo(alipayClient, aliUserToken, 0);
    }


    //获取支付宝信息
    public AlipayUserInfoShareResponse getUserInfo(AlipayClient alipayClient, AlipaySystemOauthTokenResponse aliUserToken, int number) throws AlipayApiException {
        AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse infoShareResponse = alipayClient.execute(alipayUserInfoShareRequest, aliUserToken.getAccessToken());
        log.info("----------------获得支付宝用户详情:{}", infoShareResponse.getBody());
        return infoShareResponse;
    }

    /**
     * 支付宝获取token
     *
     * @param code
     * @param alipayClient
     * @return
     */
    public AlipaySystemOauthTokenResponse getToken(String code, AlipayClient alipayClient) throws AlipayApiException {
        if (redisTemplate.hasKey(CacheKeyEnum.ALI_TOKEN_SN.key())) {
            Object aliTokenObj = redisTemplate.opsForValue().get(CacheKeyEnum.ALI_TOKEN_SN.key());
            return JSONObject.parseObject((String) aliTokenObj, AlipaySystemOauthTokenResponse.class);
        }
        AlipaySystemOauthTokenResponse aliUserToken = this.getAliUserToken(code, alipayClient);
        log.info("微信token返回:{}", aliUserToken.toString());
        if (aliUserToken.isSuccess()) {
            redisTemplate.opsForValue().set(CacheKeyEnum.ALI_TOKEN_SN.key(), JSON.toJSONString(aliUserToken), CacheKeyEnum.ALI_TOKEN_SN.timeOut(), TimeUnit.SECONDS);
            return aliUserToken;
        } else {
            throw new BusinessException("微信Token获取失败:" + aliUserToken.getMsg());
        }
    }
}

三、调用


 		//构建阿里客户端
        AlipayClient alipayClient = aliPayProperty.getByFlag(flag);
        //获取阿里用户token  这里面只包含用户ID
        AlipaySystemOauthTokenResponse aliUserToken = aliUtils.getToken(code, alipayClient);
        //获取用户信息
        AlipayUserInfoShareResponse infoShareResponse = aliUtils.getUserInfo(alipayClient, aliUserToken);
        if (null == aliUserToken) {
            throw new BusinessException("用户信息获取失败");
        } else if (!aliUserToken.isSuccess()) {
            throw new BusinessException(aliUserToken.getSubMsg());
        }
        log.info("token信息:{}", JSONUtil.toJsonStr(aliUserToken));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值