JAVA接入支付寶授權第三方登入

專注的阿熊發表於2021-07-07

Property:

import com.alipay.api.AlipayClient;

import com.alipay.api.DefaultAlipayClient;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.stereotype.Component;

/**

  * 支付寶配置

  */

@Data

@Component

@ConfigurationProperties(prefix = "ali")

public class AliPayProperty {

     /**

      * 支付寶 APPID

      */

     public String appId;

     /**

      * 商戶私鑰,您的 PKCS8 格式 RSA2 私鑰

      */

     public String merchantPrivateKey ;

     /**

      * 支付寶公鑰 , 檢視地址: 對應 APPID 下的支付寶公鑰。

      */

     public String alipayPublicKey;

     /**

      * 介面格式規範

      */

     public String format;

     /**

      * 簽名方式

      */

     public String signType;

     /**

      * 字元編碼格式

      */

     public String charset;

     /**

      * 支付寶閘道器   這是正式地址

      */

     public String gatewayUrl;

 

     /**

      * 支付寶客戶端

      * @return

      */

     public AlipayClient getAlipayClient(){

         AlipayClient alipayClient = new DefaultAlipayClient(

                 this.gatewayUrl,

                 this.appId,

                 this.merchantPrivateKey,

                 this.format,

                 this.charset,

                 this.alipayPublicKey,

                 this.signType);

         return alipayClient;

     }

}

業務流程程式碼

controller

@GetMapping(value = "/loginCallBack")

public String loginCallBack(HttpServletRequest request){

return aliPayService.loginCallBack(request);

}

1

2

3

4

service

public String loginCallBack(HttpServletRequest request){

// 獲取使用者掃碼授權的引數

Map<String,String> map = this.getAliPayParam(request);

// 獲取使用者掃碼後的 code

String code = map.get("auth_code");

// 構建阿里客戶端

     AlipayClient alipayClient = aliPayProperty.getAlipayClient();

// 獲取阿里使用者 token

     AlipaySystemOauthTokenResponse aliUserToken =

       this.getAliUserToken(code, alipayClient,0);

     // 獲取使用者資訊

     AlipayUserInfoShareResponse infoShareResponse =

       this.getUserInfo(alipayClient, aliUserToken, 0);

     // !!!沙箱環境使用者沒有這些基本資訊但是可以看到支付寶介面是成功的

     return "SUECCSS";

}

封裝接收引數方法:

     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] + ",";

             }

             // 亂碼解決,外匯跟單gendan5.com這段程式碼在出現亂碼時使用

//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");

             map.put(name, valueStr);

             log.info(" 接受支付寶回撥引數: {}",map);

         }

         return map;

     }

獲取 token 方法:

     private 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());

         if(oauthTokenResponse.isSuccess()){

             log.info(" 成功 ");

         } else {

         log.info("*********** 失敗,自旋開始第: {} ",number);

             number += 1;

             if(number < 3){

                 log.info(" 獲取 token 失敗,嘗試: *******{}*******",number);

                 return this.getAliUserToken(apiPayLoginReq, alipayClient, number);

             }

         }

         return oauthTokenResponse;

     }

獲取使用者支付寶資訊方法:

private 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());

         UserInfoReq userInfoReq = new UserInfoReq();

         if(infoShareResponse.isSuccess()){

             // 使用者授權成功

             log.info("---------------- 獲得支付寶使用者基本而資訊: {}",userInfoReq);

             log.info(" 成功 ");

         } else {

             log.info("*********** 失敗,自旋開始第: {} ",number);

             number += 1;

             if(number < 3){

                 log.info(" 呼叫使用者詳情失敗,嘗試: *******{}*******",number);

                 return this.getUserInfo(alipayClient,aliUserToken,number);

             }

             return infoShareResponse ;

         }

     }


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2780188/,如需轉載,請註明出處,否則將追究法律責任。

相關文章