支付宝授权登录免费源码奉献

废话不多说,直接上代码

首先,在授权登录前要获取 待签名授权信息,所需参数

除了app_id,pid,支付宝公钥和私钥,其他的都是定值,直接照搬:

 

    #支付宝网关(固定)
  url: https://openapi.alipay.com/gateway.do
  #APPID 即创建应用后生成
  app_id: 
    #商户签约拿到的pid
  pid: 
  #APPID 服务接口名称
  apiname: com.alipay.account.auth
    #服务接口名称
  method: alipay.open.auth.sdk.code.get
    #商户类型标识
  app_name: mc
    #业务类型
  biz_type: openservice
    #产品码
  product_id: APP_FAST_LOGIN
    #商户唯一标识
  target_id: 123456
    #授权类型
  auth_type: AUTHACCOUNT
  #开发者私钥,由开发者自己生成
  app_private_key:
  #参数返回格式,只支持json
  format: json
  #编码集,支持GBK/UTF-8
  charset: UTF-8
  #支付宝公钥,由支付宝生成
  alipay_public_key:
  #商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2
  sign_type: RSA2
  grant_type: authorization_code
  #接口权限值,目前只支持auth_user和auth_base两个值
  scope: kuaijie

 

获取sign给前端,前端授权支付宝授权后会获取到授权code

private static String buildKeyValue(String key, String value, boolean isEncode) {
        StringBuilder sb = new StringBuilder();
        sb.append(key);
        sb.append("=");
        if (isEncode) {
            try {
                sb.append(URLEncoder.encode(value, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                sb.append(value);
            }
        } else {
            sb.append(value);
        }
        return sb.toString();
    }

    @Override
    public String getSign() throws AlipayApiException {
        List<String> keys = new ArrayList<String>(map.keySet());
        // key排序
        Collections.sort(keys);

        StringBuilder authInfo = new StringBuilder();
        for (int i = 0; i < keys.size() - 1; i++) {
            String key = keys.get(i);
            String value = map.get(key);
            authInfo.append(buildKeyValue(key, value, false));
            authInfo.append("&");
        }

        String tailKey = keys.get(keys.size() - 1);
        String tailValue = map.get(tailKey);
        authInfo.append(buildKeyValue(tailKey, tailValue, false));

        String oriSign = AlipaySignature.rsaSign("content", rsaKey, charset, signType);
        String encodedSign = "";
        try {
            encodedSign = URLEncoder.encode(oriSign, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return authInfo.toString()+"&sign=" + encodedSign;
    }

 

拿到code后获取用户的基本信息接口:

@Override
    public String getAliPayUserInfo(String code) {

        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode(code);
        request.setGrantType(grantType);
        String accessToken = null;
        try {
            accessToken = getAccessToken(request);
        } catch (AlipayApiException e) {
            log.error("{支付宝sdk根据用户code获取accessToken失败}", e);
            throw new RuntimeException("未获取到accessToken");
        }
        AlipayUserInfoShareRequest userRequest = new AlipayUserInfoShareRequest();
        try {
            AlipayUserInfoShareResponse userinfoShareResponse = alipayClient.execute(userRequest, accessToken);
            return userinfoShareResponse.getBody();
        } catch (AlipayApiException e) {
            log.error("{支付宝sdk获取用户信息失败}", e);
            throw new RuntimeException("用户信息获取异常");
        }
    }

    /***
     * 根据用户code获取accessToken
     * 
     * @param request
     * @param code
     * @return
     * @throws AlipayApiException
     */
    public String getAccessToken(AlipaySystemOauthTokenRequest request) throws AlipayApiException {
        AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
        return oauthTokenResponse.getAccessToken();
    }


用户基本信息如下:

{

    "alipay_user_info_share_response": {

        "msg": "Success",

        "code": "10000",

        "gender": "性别",

        "province": "",

        "city": "",

        "user_id": "用户id",

        "nick_name": "昵称",

        "avatar": "头像"

    },

    "sign": ""

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值