Java接入支付宝授权第三方登录的完整步骤

2025-05-29 0 57

开发前准备

支付宝开发平台.

支付宝沙箱环境申请使用

Java接入支付宝授权第三方登录的完整步骤

Java接入支付宝授权第三方登录的完整步骤

!!!重点 授权回调地址必须要写全路径也就是controller最终路径(下面有具体细节)

rsa2的密钥生成: .支付宝提供生成密钥地址.

获取用户授权

生成唤起支付宝授权连接

用到appid+回调路径 回调路径=在上面配置的全路径 具体路径:

?

1

2
https://openauth.alipay.com/oauth2/publicappauthorize.htm?

app_id=2016####&scope=auth_user&edirect_uri=http://ip | 域名 + 接口地址

也可以使用自定义参数的连接:

?

1

2
https://openauth.alipay.com/oauth2/publicappauthorize.htm?app_id=2016####

&state=自定义参数(多个用逗号拼接)&scope=auth_user&edirect_uri=http://ip | 域名 + 接口地址

具体怎么用??? 在线生成二维码用支付宝沙箱app扫码

回调地址接收支付宝参数

构建请求支付宝客户端

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

# 支付宝网关 https://openapi.alipay.com/gateway.do 是正式的

gatewayurl: https://openapidev.alipay.com/gateway.do #dev是沙箱

Java接入支付宝授权第三方登录的完整步骤

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 对应appid下的支付宝公钥。

*/

public string alipaypublickey;

/**

* 接口格式规范

*/

public string format;

/**

* 签名方式

*/

public string signtype;

/**

* 字符编码格式

*/

public string charset;

/**

* 支付宝网关 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;

}

}

业务流程代码

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){

//获取用户扫码授权的参数

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";

}

封装接收参数方法:

?

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

}

// 乱码解决,这段代码在出现乱码时使用

// valuestr = new string(valuestr.getbytes("iso-8859-1"), "utf-8");

map.put(name, valuestr);

log.info("接受支付宝回调参数:{}",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()){

//用户授权成功

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 ;

}

}

串业务

用户扫码后后会跳到你配置的回调地址上!!!但是因为代码中返回是success,用户收到的只是个字符串。所以此处因该是配置支付宝去回调前端地址 然后参数让前端原封不动传向后端 后端解析成功后,前端引导用户进行下一步操作

总结

到此这篇关于java接入支付宝授权第三方登录的文章就介绍到这了,更多相关java接入支付宝授权内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/weixin_44440642/article/details/117906000

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java接入支付宝授权第三方登录的完整步骤 https://www.kuaiidc.com/105351.html

相关文章

发表评论
暂无评论