java⼩程序登录验证登录并获取⽤户信息⼩程序API
public class WeChatAppLoginReq
{
private String code;
private String rawData;
private String encryptedData;
private String iv;
private String signature;
public String getCode() {
return code;
}
public void setCode(String code) {
}
public String getRawData() {
return rawData;
}
public void setRawData(String rawData) {
this.rawData = rawData;
}
public String getEncryptedData() {
return encryptedData;
}
public void setEncryptedData(String encryptedData) {
}
public String getIv() {
return iv;
}
public void setIv(String iv) {
this.iv = iv;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
}
import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Date;
import java.util.Map;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
pto.NoSuchPaddingException;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jose4j.base64url.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaoyi.sns.q.WeChatAppLoginReq;
import com.xiaoyi.sns.bean.po.spo.UserInfoPo;
import com.xiaoyi.stant.Constants.UserType;
import com.xiaoyi.sns.business.impl.spo.UserInfoBiz;
import com.stant.ResponseMsg;
import com.ption.SystemException;
import com.xiaoyi.snsmon.util.HmacUtil;
/**
* Project Name: sns-business
* File Name: WeAppLoginBiz.java
* Date: 2017年1⽉13⽇下午1:06:17
* Author: zhao.zhibo@xiaoyi
* Explain: ⼩程序登录
*/
@Component
public class WeChatAppLoginBiz
{
private static final Logger logger = Logger(WeChatAppLoginBiz.class);
@Autowired
private UserInfoBiz userInfoBiz;
public static boolean initialized = false;
private static final String APPID = "wx3c************b8";
private static final String APPID = "wx3c************b8";
private static final String SECRET = "75324***************500ae89726";
public Map<String,Object> login(WeChatAppLoginReq req)
{
//获取 session_key 和 openId
String url = "api.weixin.qq/sns/jscode2session?appid="+APPID+"&secret="+SECRET+"&js_code="+Code()+"&grant_type=authorization_code" RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = hange(url, HttpMethod.GET, null, String.class);
if(responseEntity != null && StatusCode() == HttpStatus.OK)
{
String sessionData = Body();
logger.info("sessionData = "+ sessionData);
JSONObject jsonObj = JSON.parseObject(sessionData);
String openId = String("openid");
String sessionKey = String("session_key");
String signature = HmacUtil.RawData()+sessionKey);
if(!signature.Signature()))
{
logger.info(" req signature="+Signature());
logger.info(" java signature="+Signature());
throw new SystemException(ResponseMsg.WECHAT_LOGIN_SIGNATURE_ERROR);
}
byte[] resultByte = null;
try {
resultByte = decrypt(Base64.EncryptedData()), Base64.decode(sessionKey), Base64.Iv()));
} catch (Exception e) {
throw new SystemException(ResponseMsg.WECHAT_LOGIN_USER_ERROR);
}
if(null != resultByte && resultByte.length > 0)
{
String userInfoStr = "";
try {
userInfoStr = new String(resultByte, "UTF-8");
} catch (UnsupportedEncodingException e)
{
<(e.getMessage());
}
logger.info("userInfo = "+ userInfoStr);
JSONObject userInfoObj = JSON.parseObject(userInfoStr);
UserInfoPo userPo = new UserInfoPo();
userPo.String("nickName"));
userPo.setCreatedTime(new Date());
userPo.String("gender"));
userPo.String("avatarUrl"));
userPo.String("unionId"));
userPo.setType((short)UserType.WeiXin);
userPo.setLoginType(UserType.WeChatApp);
//userPo.String("city"));
//String("city");
//String("province");
//String("country");
Map<String,Object> data = userInfoBiz.insertOrUpdate(userPo);
return data;
}else
{
throw new SystemException(ResponseMsg.WECHAT_LOGIN_USER_ERROR);
}
}else
{
throw new SystemException(ResponseMsg.WECHAT_LOGIN_CODE_ERROR);
throw new SystemException(ResponseMsg.WECHAT_LOGIN_CODE_ERROR);
}
}
private byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException { initialize();
try {
Cipher cipher = Instance("AES/CBC/PKCS7Padding");
Key sKeySpec = new SecretKeySpec(keyByte, "AES");
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化
byte[] result = cipher.doFinal(content);
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void initialize(){
if (initialized) return;
Security.addProvider(new BouncyCastleProvider());
initialized = true;
}
//⽣成iv
public static AlgorithmParameters generateIV(byte[] iv) throws Exception{
AlgorithmParameters params = Instance("AES");
有趣的java小程序
params.init(new IvParameterSpec(iv));
return params;
}
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HmacUtil {
public static String SHA1(String str){
try {
//指定sha1算法
MessageDigest digest = Instance("SHA-1");
digest.Bytes());
//获取字节数组
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
// 字节数组转换为⼗六进制数
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = HexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
String().toLowerCase();
} catch (NoSuchAlgorithmException e) {
return "";
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论