開發(fā)前準(zhǔn)備
支付寶沙箱環(huán)境申請(qǐng)使用
!!!重點(diǎn) 授權(quán)回調(diào)地址必須要寫全路徑也就是controller最終路徑(下面有具體細(xì)節(jié))
rsa2的密鑰生成: .支付寶提供生成密鑰地址.
獲取用戶授權(quán)
生成喚起支付寶授權(quán)連接
用到appid+回調(diào)路徑 回調(diào)路徑=在上面配置的全路徑 具體路徑:
1
2
|
https: //openauth.alipay.com/oauth2/publicappauthorize.htm? app_id= 2016 ####&scope=auth_user&edirect_uri=http: //ip | 域名 + 接口地址 |
也可以使用自定義參數(shù)的連接:
1
2
|
https: //openauth.alipay.com/oauth2/publicappauthorize.htm?app_id=2016#### &state=自定義參數(shù)(多個(gè)用逗號(hào)拼接)&scope=auth_user&edirect_uri=http: //ip | 域名 + 接口地址 |
具體怎么用??? 在線生成二維碼用支付寶沙箱app掃碼
回調(diào)地址接收支付寶參數(shù)
構(gòu)建請(qǐng)求支付寶客戶端
yml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# 支付寶配置 ali: appid: 2016 #### # 自己的私鑰 merchantprivatekey: 連接生成的私鑰 # 支付寶公鑰 alipaypublickey: 鏈接生成的公鑰配置后支付寶給到的支付寶公鑰 # 簽名方式 signtype: rsa2 # 字符編碼格式 charset: utf- 8 # 字符編碼格式 format: json # 支付寶網(wǎng)關(guān) https: //openapi.alipay.com/gateway.do 是正式的 gatewayurl: https: //openapidev.alipay.com/gateway.do #dev是沙箱 |
property:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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 ; /** * 支付寶公鑰,查看地址:https://openhome.alipay.com 對(duì)應(yīng)appid下的支付寶公鑰。 */ public string alipaypublickey; /** * 接口格式規(guī)范 */ public string format; /** * 簽名方式 */ public string signtype; /** * 字符編碼格式 */ public string charset; /** * 支付寶網(wǎng)關(guān) https://openapi.alipay.com/gateway.do 這是正式地址 */ 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; } } |
業(yè)務(wù)流程代碼
controller:
1
2
3
4
|
@getmapping (value = "/logincallback" ) public string logincallback(httpservletrequest request){ return alipayservice.logincallback(request); } |
service:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public string logincallback(httpservletrequest request){ //獲取用戶掃碼授權(quán)的參數(shù) map<string,string> map = this .getalipayparam(request); //獲取用戶掃碼后的code string code = map.get( "auth_code" ); //構(gòu)建阿里客戶端 alipayclient alipayclient = alipayproperty.getalipayclient(); //獲取阿里用戶token alipaysystemoauthtokenresponse aliusertoken = this .getaliusertoken(code, alipayclient, 0 ); //獲取用戶信息 alipayuserinfoshareresponse infoshareresponse = this .getuserinfo(alipayclient, aliusertoken, 0 ); //?。?!沙箱環(huán)境用戶沒有這些基本信息但是可以看到支付寶接口是成功的 return "sueccss" ; } |
封裝接收參數(shù)方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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] + "," ; } // 亂碼解決,這段代碼在出現(xiàn)亂碼時(shí)使用 // valuestr = new string(valuestr.getbytes("iso-8859-1"), "utf-8"); map.put(name, valuestr); log.info( "接受支付寶回調(diào)參數(shù):{}" ,map); } return map; } |
獲取token方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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; } |
獲取用戶支付寶信息方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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()){ //用戶授權(quán)成功 log.info( "----------------獲得支付寶用戶基本而信息:{}" ,userinforeq); log.info( "成功" ); } else { log.info( "***********失敗,自旋開始第:{}次" ,number); number += 1 ; if (number < 3 ){ log.info( "調(diào)用用戶詳情失敗,嘗試:*******{}*******" ,number); return this .getuserinfo(alipayclient,aliusertoken,number); } return infoshareresponse ; } } |
串業(yè)務(wù)
用戶掃碼后后會(huì)跳到你配置的回調(diào)地址上?。?!但是因?yàn)榇a中返回是success,用戶收到的只是個(gè)字符串。所以此處因該是配置支付寶去回調(diào)前端地址 然后參數(shù)讓前端原封不動(dòng)傳向后端 后端解析成功后,前端引導(dǎo)用戶進(jìn)行下一步操作
總結(jié)
到此這篇關(guān)于java接入支付寶授權(quán)第三方登錄的文章就介紹到這了,更多相關(guān)java接入支付寶授權(quán)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_44440642/article/details/117906000