、登陸流程圖
2、微信小程序端
-
doLogin:
function(callback = () =>{}){
-
-
-
success:function(loginRes){
-
-
-
-
-
success:
function(infoRes){
-
console.log(infoRes,'>>>');
-
-
-
-
-
-
-
signature:infoRes.signature,
-
encrypteData:infoRes.encryptedData,
-
-
-
-
console.log('login success');
-
-
-
that.globalData.userInfo = res.userInfo;
-
wx.setStorageSync(
'userInfo',JSON.stringify(res.userInfo));
-
wx.setStorageSync(
'loginFlag',res.skey);
-
console.log("skey="+res.skey);
-
-
-
that.showInfo(
'res.errmsg');
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
微信小程序端發起登陸請求,攜帶的參數主要有:java
-
-
-
signature:infoRes.signature,
-
encrypteData:infoRes.encryptedData,
-
須要的數據主要有:mysql
result、userInfo和skey
result用來判斷是否登陸成功,userInfo是用戶的一些信息,保存在緩存中,不用每次都從後臺獲取,skey是用戶登陸態標識,也放在緩存中,若是skey存在就直接登陸,維護用戶的登陸狀態,具備時效性redis
3、Java後臺
-
-
@RequestMapping(
"/login")
-
public
Map<String,Object> doLogin(Model model,
-
@RequestParam(value =
"code",required = false) String code,
-
@RequestParam(value =
"rawData",required = false) String rawData,
-
@RequestParam(value =
"signature",required = false) String signature,
-
@RequestParam(value =
"encrypteData",required = false) String encrypteData,
-
@RequestParam(value =
"iv",required = false) String iv){
-
log.info(
"Start get SessionKey" );
-
-
-
Map<String,Object> map = new HashMap<String, Object>( );
-
System.out.println(
"用戶非敏感信息"+rawData);
-
-
JSONObject rawDataJson =
JSON.parseObject( rawData );
-
-
System.out.println(
"簽名"+signature);
-
JSONObject SessionKeyOpenId = getSessionKeyOrOpenId( code );
-
System.out.println(
"post請求獲取的SessionAndopenId="+SessionKeyOpenId);
-
-
String openid = SessionKeyOpenId.getString("openid" );
-
-
String sessionKey = SessionKeyOpenId.getString( "session_key" );
-
-
System.out.println(
"openid="+openid+",session_key="+sessionKey);
-
-
User user = userService.findByOpenid( openid );
-
-
String skey = UUID.randomUUID().toString();
-
-
-
String nickName = rawDataJson.getString( "nickName" );
-
String avatarUrl = rawDataJson.getString( "avatarUrl" );
-
String gender = rawDataJson.getString( "gender" );
-
String city = rawDataJson.getString( "city" );
-
String country = rawDataJson.getString( "country" );
-
String province = rawDataJson.getString( "province" );
-
-
-
-
-
user.setCreateTime(
new Date( ) );
-
user.setSessionkey( sessionKey );
-
-
-
user.setUaddress( country+
" "+province+" "+city );
-
user.setUavatar( avatarUrl );
-
user.setUgender( Integer.parseInt( gender ) );
-
user.setUname( nickName );
-
user.setUpdateTime(
new Date( ) );
-
-
userService.insert( user );
-
-
-
log.info(
"用戶openid已存在,不須要插入" );
-
-
-
String skey_redis = (String) redisTemplate.opsForValue().get( openid );
-
if(StringUtils.isNotBlank( skey_redis )){
-
-
redisTemplate.delete( skey_redis );
-
-
-
-
JSONObject sessionObj =
new JSONObject( );
-
sessionObj.put(
"openId",openid );
-
sessionObj.put(
"sessionKey",sessionKey );
-
redisTemplate.opsForValue().set( skey,sessionObj.toJSONString() );
-
redisTemplate.opsForValue().set( openid,skey );
-
-
-
-
-
-
-
-
-
-
-
JSONObject userInfo = getUserInfo( encrypteData, sessionKey, iv );
-
System.out.println(
"根據解密算法獲取的userInfo="+userInfo);
-
userInfo.put(
"balance",user.getUbalance() );
-
map.put(
"userInfo",userInfo );
-
-
-
獲取openid和sessionKey方法算法
-
public
static JSONObject getSessionKeyOrOpenId(String code){
-
-
-
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
-
Map<String,String> requestUrlParam = new HashMap<String, String>( );
-
requestUrlParam.put(
"appid","你的小程序appId" );
-
requestUrlParam.put(
"secret","你的小程序appSecret" );
-
requestUrlParam.put(
"js_code",wxCode );
-
requestUrlParam.put(
"grant_type","authorization_code" );
-
-
-
JSONObject jsonObject =
JSON.parseObject( UrlUtil.sendPost( requestUrl,requestUrlParam ));
-
-
解密用戶敏感數據獲取用戶信息sql
-
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);
-
-
-
-
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);
-
-
-
-
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);
-
-
-
4、流程
1.小程序端發起請求並攜帶主要參數數據庫
2.java後臺接到/login請求後,根據code去調用微信接口獲取用戶惟一標識openid和sessionKeyjson
3.根據openid查詢mysql數據庫,判斷該用戶是否存在,若是不存在將用戶非敏感信息和其餘初始化數據存入到數據庫中,若是已存在,不操做小程序
4.根據openid查詢redis數據庫,判斷openid對應的skey是否存在,若是存在則刪除原來老的skey以及對應的openid和sessionKey微信小程序
5.經過uuid生成惟一的skey,用openid作鍵,skey作值,存入到redis中api
6.而後把skey作鍵,openid和sessionKey的json串作值也從新存入到redis中
7.根據解密算法,參數有encryptedData、sessionKey和iv,獲取用戶信息userInfo,若是userInfo字段不知足須要,可經過userInfo.put( "balance",user.getUbalance() );添加所須要的字段和值
8.將微信小程序須要的數據封裝到map中,返回給小程序端
-
-
-
-
-
map.put( "userInfo",userInfo );