一、微信小程序
第一步:调用 wx.login获取code 文档地址
第二步:判断用户是否授权读取用户信息 文档地址
第三步:调用wx.getUserInfo读取用户数据 文档地址
第四步:由于小程序后台授权域名无法授权微信的域名,所以我们只能通过我们自己的服务器去调用微信服务器去获取用户信息,故我们将wx.login获取code 和 wx.getuserinfo 获取的encrypteddata与iv 通过wx.request 请求传入后台
服务器返回的数据:
小程序代码:
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
|
//调用登录接口,获取 code
wx.login({
success: function (res) {
wx.getsetting({
success(setres) {
// 判断是否已授权
if (!setres.authsetting[ 'scope.userinfo' ]) {
// 授权访问
wx.authorize({
scope: 'scope.userinfo' ,
success() {
//获取用户信息
wx.getuserinfo({
lang: "zh_cn" ,
success: function (userres) {
//发起网络请求
wx.request({
url: config.loginwxurl,
data: {
code: res.code,
encrypteddata: userres.encrypteddata,
iv: userres.iv
},
header: {
"content-type" : "application/x-www-form-urlencoded"
},
method: 'post' ,
//服务端的回掉
success: function (result) {
var data = result.data.result;
data.expiretime = nowdate + expiretime;
wx.setstoragesync( "userinfo" , data);
userinfo = data;
}
})
}
})
}
})
} else {
//获取用户信息
wx.getuserinfo({
lang: "zh_cn" ,
success: function (userres) {
//发起网络请求
wx.request({
url: config.loginwxurl,
data: {
code: res.code,
encrypteddata: userres.encrypteddata,
iv: userres.iv
},
header: {
"content-type" : "application/x-www-form-urlencoded"
},
method: 'post' ,
success: function (result) {
var data = result.data.result;
data.expiretime = nowdate + expiretime;
wx.setstoragesync( "userinfo" , data);
userinfo = data;
}
})
}
})
}
}
})
}
})
|
二、java服务端
根据code获取openid与解码用户信息 代码
所需要的jar包
1
2
3
4
5
6
7
8
9
10
|
<dependency>
<groupid>org.codehaus.xfire</groupid>
<artifactid>xfire-core</artifactid>
<version> 1.2 . 6 </version>
</dependency>
<dependency>
<groupid>org.bouncycastle</groupid>
<artifactid>bcprov-jdk16</artifactid>
<version> 1.46 </version>
</dependency>
|
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/**
* 微信小程序信息获取
*
* @author zhy
*/
public class wxappletuserinfo {
private static logger log = logger.getlogger(wxappletuserinfo. class );
/**
* 获取微信小程序 session_key 和 openid
*
* @author zhy
* @param code 调用微信登陆返回的code
* @return
*/
public static jsonobject getsessionkeyoropenid(string code){
//微信端登录code值
string wxcode = code;
resourcebundle resource = resourcebundle.getbundle( "weixin" ); //读取属性文件
string requesturl = resource.getstring( "url" ); //请求地址 https://api.weixin.qq.com/sns/jscode2session
map<string,string> requesturlparam = new hashmap<string,string>();
requesturlparam.put( "appid" , resource.getstring( "appid" )); //开发者设置中的appid
requesturlparam.put( "secret" , resource.getstring( "appsecret" )); //开发者设置中的appsecret
requesturlparam.put( "js_code" , wxcode); //小程序调用wx.login返回的code
requesturlparam.put( "grant_type" , "authorization_code" ); //默认参数
//发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识
jsonobject jsonobject = json.parseobject(urlutil.sendpost(requesturl, requesturlparam));
return jsonobject;
}
/**
* 解密用户敏感数据获取用户信息
*
* @author zhy
* @param sessionkey 数据进行加密签名的密钥
* @param encrypteddata 包括敏感数据在内的完整用户信息的加密数据
* @param iv 加密算法的初始向量
* @return
*/
public static jsonobject getuserinfo(string encrypteddata,string sessionkey,string iv){
// 被加密的数据
byte [] databyte = base64.decode(encrypteddata);
// 加密秘钥
byte [] keybyte = base64.decode(sessionkey);
// 偏移量
byte [] ivbyte = base64.decode(iv);
try {
// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
int base = 16 ;
if (keybyte.length % base != 0 ) {
int groups = keybyte.length / base + (keybyte.length % base != 0 ? 1 : 0 );
byte [] temp = new byte [groups * base];
arrays.fill(temp, ( byte ) 0 );
system.arraycopy(keybyte, 0 , temp, 0 , keybyte.length);
keybyte = temp;
}
// 初始化
security.addprovider( new bouncycastleprovider());
cipher cipher = cipher.getinstance( "aes/cbc/pkcs7padding" , "bc" );
secretkeyspec spec = new secretkeyspec(keybyte, "aes" );
algorithmparameters parameters = algorithmparameters.getinstance( "aes" );
parameters.init( new ivparameterspec(ivbyte));
cipher.init(cipher.decrypt_mode, spec, parameters); // 初始化
byte [] resultbyte = cipher.dofinal(databyte);
if ( null != resultbyte && resultbyte.length > 0 ) {
string result = new string(resultbyte, "utf-8" );
return json.parseobject(result);
}
} catch (nosuchalgorithmexception e) {
log.error(e.getmessage(), e);
} catch (nosuchpaddingexception e) {
log.error(e.getmessage(), e);
} catch (invalidparameterspecexception e) {
log.error(e.getmessage(), e);
} catch (illegalblocksizeexception e) {
log.error(e.getmessage(), e);
} catch (badpaddingexception e) {
log.error(e.getmessage(), e);
} catch (unsupportedencodingexception e) {
log.error(e.getmessage(), e);
} catch (invalidkeyexception e) {
log.error(e.getmessage(), e);
} catch (invalidalgorithmparameterexception e) {
log.error(e.getmessage(), e);
} catch (nosuchproviderexception e) {
log.error(e.getmessage(), e);
}
return null ;
}
}
|
发送请求的代码
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
|
/**
* 向指定 url 发送post方法的请求
*
* @param url 发送请求的 url
* @param param 请求参数
* @return 所代表远程资源的响应结果
*/
ublic static string sendpost(string url, map<string, ?> parammap) {
printwriter out = null ;
bufferedreader in = null ;
string result = "" ;
string param = "" ;
iterator<string> it = parammap.keyset().iterator();
while (it.hasnext()) {
string key = it.next();
param += key + "=" + parammap.get(key) + "&" ;
}
try {
url realurl = new url(url);
// 打开和url之间的连接
urlconnection conn = realurl.openconnection();
// 设置通用的请求属性
conn.setrequestproperty( "accept" , "*/*" );
conn.setrequestproperty( "connection" , "keep-alive" );
conn.setrequestproperty( "accept-charset" , "utf-8" );
conn.setrequestproperty( "user-agent" , "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)" );
// 发送post请求必须设置如下两行
conn.setdooutput( true );
conn.setdoinput( true );
// 获取urlconnection对象对应的输出流
out = new printwriter(conn.getoutputstream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义bufferedreader输入流来读取url的响应
in = new bufferedreader( new inputstreamreader(conn.getinputstream(), "utf-8" ));
string line;
while ((line = in.readline()) != null ) {
result += line;
}
} catch (exception e) {
log.error(e.getmessage(), e);
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out!= null ){
out.close();
}
if (in!= null ){
in.close();
}
}
catch (ioexception ex){
ex.printstacktrace();
}
}
return result;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/weilai_zhilu/article/details/77932630