好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现二维码扫码授权登陆

假设现在有2个设备,a设备需要扫码授权登陆,b设备是已经登陆了的设备。然后实现如下:

一、a设备生成生成二维码:

a设备向服务器请求getlogincode接口,这个接口根据请求的sessionid进行base64或其他加密方式进行加密,然后以此作为二维码的值,并将这个logincode写到redis里,设置5分钟过期。然后将这个logincode返回给a设备,a设备以此值来生成登陆的二维码。

二、b设备扫码授权

b设备来扫a设备的二维码的时候,携带二维码的值,请求授权登陆的接口scanconfirmlogin,此接口里先校验二维码是否过期,没过期的话进行后面的业务逻辑处理,将用户的基本信息和token写到redis里。

三、a设备轮询获取授权状态

b设备以每秒一次的频率来刷 获取用户授权状态接口,若状态为已授权,拿到用户信息去做后面的逻辑处理。

?

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

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

/**

* 获取扫描登陆的二维码

* @param noncestr随机字符串

* @throws exception

*/

@requestmapping (value = "user/getlogincode.json" )

public void getlogincode(string noncestr,httpservletrequest request,httpservletresponse response) throws exception {

if (stringutil.isblank(noncestr)){

apidata(request, response,reqjson.error(commonerror.params_imperfect));

return ;

}

//参数的有效性校验在拦截器里实现

int expirationtime= 300 ; //时效5分钟

final string sessionid=request.getsession().getid();

string logincode=toolutils.getbase64(sessionid);

jedisutil.set(logincode, logincode, expirationtime);

map<string,object> map= new hashmap<>();

map.put( "logincode" , logincode);

map.put( "expirationtime" , expirationtime);

apidata(request, response, reqjson.ok(map));

}

/**

* 扫码确认登陆

* @param logincode

* @param request

* @param response

* @throws exception

*/

@requestmapping (value = "user/scanconfirmlogin.json" )

@authorizationapi

public void scanconfirmlogin( @currenttoken final token token,string logincode,httpservletrequest request,httpservletresponse response) throws exception {

if (stringutil.isblank(logincode)){

apidata(request, response,reqjson.error(commonerror.params_imperfect));

return ;

}

string userid=token.getuserid();

map<string,string> map= new hashmap<>();

string loginticket=jedisutil.get(logincode);

if (stringutil.isblank(loginticket)){

//二维码过期

apidata(request, response,reqjson.error(commonerror.two_dimensional_code_has_expired));

return ;

}

userinfo userinfo = userinfobiz.getuser( new userinfo(userid));

if (userinfo== null ){

apidata(request, response,reqjson.error(usererror.user_not_found));

return ;

}

//将用户信息放在缓存中

map.put(baseconfig.access_token, token.getaccesstoken());

map.put( "userid" , userinfo.getuserid());

map.put( "rongcloudtoken" , userinfo.getrongcloudtoken());

map.put( "identity" , userinfo.getidentity());

jedisutil.setmap(logincode+ "scanconfirmlogin" , map, 300 );

apidata(request, response, reqjson.ok( new object()));

}

/**

* 获取登陆状态

* @param logincode

* @param request

* @param response

* @throws exception

*/

@requestmapping (value = "user/getscanconfirmloginstatus.json" )

public void getloginstatus( final string logincode,httpservletrequest request,httpservletresponse response) throws exception {

if (stringutil.isblank(logincode)){

apidata(request, response,reqjson.error(commonerror.params_imperfect));

return ;

}

map<string,string> map= jedisutil.getmap(logincode+ "scanconfirmlogin" );

if (map== null ){

apidata(request, response,reqjson.error(commonerror.authorization_has_expired));

return ;

}

apidata(request, response, reqjson.ok(map));

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_33556185/article/details/80060764

查看更多关于java实现二维码扫码授权登陆的详细内容...

  阅读:15次