首先java 后端依赖两个jar
org.codehaus.xfire
xfire-core
1.2.6
org.bouncycastle
bcprov-jdk16
1.46
流程:1.需要微信前端调用wx.login接口获取code。 然后再调用wx.getuserInfo接口获取用户的信息。
获取到的信息包含有用户基本信息(这里面没有openid),以及encryptedData,这个encryptedData含有完整用户信息(含openid),但是数据是加密的,需要服务器端来解析。
2. 前端调用服务器接口,将获取到的code,以及encryptedData,和iv一起发送到后端。
3. 服务器在解密encryptedData之前,需要调用微信接口获取sessionkey. 有了encryptedData才能解密。
前端调用代码:wx.login({
success: function (res_login) {
if (res_login.code){
wx.getUserInfo({
success:function(res){
console.log(res)
var jsonData = {
code: res_login.code,
encryptedData: res.encryptedData,
iv: res.iv
};
wx.request({
url: 'http://domain/miniapp/login/userinfo',
header: { 'Content-Type': 'application/json' },
method:'POST',
data: jsonData,
success: res => {
console.log(res.data)
wx.hideLoading()
this.setData({
projectlist: res.data.content
})
}
})
}
})
}
}
})
服务器端代码:import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.channels.ScatteringByteChannel;
import java.security.AlgorithmParameters;
impo