微信小程序登录,获取用户手机号
基础类
1 2 3 4 5
| <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.57</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10
| @Data @AllArgsConstructor @NoArgsConstructor public class WeiXinLoginVo { private String openId; private String session_key; private String unionid; private Integer errcode; private String errmsg; }
|
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
|
public static String sendGet(String url, String param) { StringBuilder result = new StringBuilder(); BufferedReader in = null; try { String urlNameString = url; if (param != null) { urlNameString = url + "?" + param; } URL realUrl = new URL(urlNameString); URLConnection connection = realUrl.openConnection(); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect(); in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (Exception ex) { ex.printStackTrace(); } } return result.toString(); }
|
获取前端传入的code调用接口获取session_key
1 2 3 4 5 6 7 8 9 10 11 12
| @Value("${weixin.appid}") private String appid;
@Value("${weixin.secret}") private String secret;
public WeiXinLoginVo goLogin(String code) { String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + jsCode + "&grant_type=authorization_code"; String result = HttpUtils.sendGet(url, null); return JSON.parseObject(result, WeiXinLoginVo.class); }
|
解密前端传入的encryptedData和iv
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
| weiXinCommon.decryptOfDiyIV(userLoginVo.getEncryptedData(), sessionKey, userLoginVo.getIv());
private static final String KEY_ALGORITHM = "AES"; private static final String ALGORITHM_STR = "AES/CBC/PKCS7Padding"; private static Key key; private static Cipher cipher;
public byte[] decryptOfDiyIV(String encryptedData, String sessionKeyB64, String ivs) { byte[] encryptedDataByte = Base64.decode(encryptedData); byte[] sessionKeyB64Byte = Base64.decode(sessionKeyB64); byte[] ivsByte = Base64.decode(ivs); byte[] encryptedText = null; init(sessionKeyB64Byte); try { cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivsByte)); encryptedText = cipher.doFinal(encryptedDataByte); } catch (Exception e) { e.printStackTrace(); } return encryptedText; }
private void init(byte[] keyBytes) { int base = 16; if (keyBytes.length % base != 0) { int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0); byte[] temp = new byte[groups * base]; Arrays.fill(temp, (byte) 0); System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length); keyBytes = temp; } Security.addProvider(new BouncyCastleProvider()); key = new SecretKeySpec(keyBytes, KEY_ALGORITHM); try { cipher = Cipher.getInstance(ALGORITHM_STR, "BC"); } catch (Exception e) { e.printStackTrace(); } }
|
解密后的数据
1
| {phoneNumber=手机号, watermark={"appid":"xxx","timestamp":1592388528}, purePhoneNumber=手机号, countryCode=86}
|
相关文章
数据库连接池
SpringIOC
Junit和Spring
Tomcat
Servlet
Request,Response和ServletContext
Cookie和Session
JSP和EL和Jstl
Filter和Listener
Mybatis