Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
zcq
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
注册表
注册表
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
svn
zcq
Commits
240c1f15
提交
240c1f15
authored
11月 27, 2019
作者:
yucaiwei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
--no commit message
上级
ad86b21b
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
219 行增加
和
8 行删除
+219
-8
WeChatLoginXcxController.java
.../com/zrqx/third/wechatlogin/WeChatLoginXcxController.java
+25
-7
AES.java
.../src/main/java/com/zrqx/third/wechatlogin/config/AES.java
+77
-0
WXCore.java
...c/main/java/com/zrqx/third/wechatlogin/config/WXCore.java
+53
-0
WeChatLoginXcxConfig.java
...m/zrqx/third/wechatlogin/config/WeChatLoginXcxConfig.java
+2
-1
WxPKCS7Encoder.java
...ava/com/zrqx/third/wechatlogin/config/WxPKCS7Encoder.java
+62
-0
没有找到文件。
com.zrqx.third/src/main/java/com/zrqx/third/wechatlogin/WeChatLoginXcxController.java
浏览文件 @
240c1f15
...
@@ -9,7 +9,10 @@ import io.swagger.annotations.ApiOperation;
...
@@ -9,7 +9,10 @@ import io.swagger.annotations.ApiOperation;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
...
@@ -17,10 +20,12 @@ import com.alibaba.fastjson.JSON;
...
@@ -17,10 +20,12 @@ import com.alibaba.fastjson.JSON;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.zrqx.core.enums.ResponseCodeEnum
;
import
com.zrqx.core.enums.ResponseCodeEnum
;
import
com.zrqx.core.exception.BaseException
;
import
com.zrqx.core.exception.BaseException
;
import
com.zrqx.core.form.third.wecharlogin.WecharXcxGetPhoneForm
;
import
com.zrqx.core.model.third.wechartlogin.WeChartThirdReturn
;
import
com.zrqx.core.model.third.wechartlogin.WeChartThirdReturn
;
import
com.zrqx.core.util.response.CallBack
;
import
com.zrqx.core.util.response.CallBack
;
import
com.zrqx.third.constant.RequestPath
;
import
com.zrqx.third.constant.RequestPath
;
import
com.zrqx.third.util.HttpsUtils
;
import
com.zrqx.third.util.HttpsUtils
;
import
com.zrqx.third.wechatlogin.config.WXCore
;
import
com.zrqx.third.wechatlogin.config.WeChatLoginXcxConfig
;
import
com.zrqx.third.wechatlogin.config.WeChatLoginXcxConfig
;
/** 微信登录
/** 微信登录
...
@@ -38,7 +43,7 @@ public class WeChatLoginXcxController {
...
@@ -38,7 +43,7 @@ public class WeChatLoginXcxController {
public
CallBack
<
WeChartThirdReturn
>
query
(
String
code
,
HttpServletRequest
request
)
throws
Exception
{
public
CallBack
<
WeChartThirdReturn
>
query
(
String
code
,
HttpServletRequest
request
)
throws
Exception
{
String
appid
=
"appid="
+
WeChatLoginXcxConfig
.
APPID
;
String
appid
=
"appid="
+
WeChatLoginXcxConfig
.
APPID
;
String
secret
=
"&secret="
+
WeChatLoginXcxConfig
.
SECRET
;
String
secret
=
"&secret="
+
WeChatLoginXcxConfig
.
SECRET
;
String
code1
=
"&code="
+
code
;
String
code1
=
"&
js_
code="
+
code
;
String
grant_type
=
"&grant_type="
+
WeChatLoginXcxConfig
.
GRANT_TYPE
;
String
grant_type
=
"&grant_type="
+
WeChatLoginXcxConfig
.
GRANT_TYPE
;
//调用微信接口获得openid和access_token
//调用微信接口获得openid和access_token
String
param
=
appid
+
secret
+
code1
+
grant_type
;
String
param
=
appid
+
secret
+
code1
+
grant_type
;
...
@@ -50,16 +55,29 @@ public class WeChatLoginXcxController {
...
@@ -50,16 +55,29 @@ public class WeChatLoginXcxController {
}
}
//唯一标识用户的 openId
//唯一标识用户的 openId
String
openId
=
object
.
get
(
"openid"
).
toString
();
String
openId
=
object
.
get
(
"openid"
).
toString
();
//用户昵称
String
session_key
=
object
.
get
(
"session_key"
).
toString
();
String
nickname
=
object
.
get
(
"nickname"
).
toString
();
//用户头像
String
headimgurl
=
object
.
get
(
"headimgurl"
).
toString
();
WeChartThirdReturn
user
=
new
WeChartThirdReturn
();
WeChartThirdReturn
user
=
new
WeChartThirdReturn
();
user
.
setOpenid
(
openId
);
user
.
setOpenid
(
openId
);
user
.
setNickname
(
nickname
);
user
.
setSession_key
(
session_key
);
user
.
setHeadImgUrl
(
headimgurl
);
return
CallBack
.
success
(
user
);
return
CallBack
.
success
(
user
);
}
}
//获取手机号,解密
@ApiOperation
(
value
=
"获取手机号"
,
notes
=
"获取手机号"
)
@PostMapping
(
RequestPath
.
GET
+
RequestPath
.
PHONE
)
private
CallBack
<
String
>
getPhoneNumber
(
@RequestBody
WecharXcxGetPhoneForm
form
){
String
appId
=
WeChatLoginXcxConfig
.
APPID
;
String
sessionKey
=
form
.
getSession_key
();
String
encryptedData
=
form
.
getEncryptedData
();
String
iv
=
form
.
getIv
();
String
str
=
WXCore
.
decrypt
(
appId
,
encryptedData
,
sessionKey
,
iv
);
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
str
);
String
phone
=
jsonObject
.
getString
(
"phoneNumber"
);
if
(
StringUtils
.
isBlank
(
phone
)){
return
CallBack
.
fail
(
"获取失败"
);
}
return
CallBack
.
success
(
phone
);
}
}
}
com.zrqx.third/src/main/java/com/zrqx/third/wechatlogin/config/AES.java
0 → 100644
浏览文件 @
240c1f15
package
com
.
zrqx
.
third
.
wechatlogin
.
config
;
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
javax.crypto.BadPaddingException
;
import
javax.crypto.Cipher
;
import
javax.crypto.IllegalBlockSizeException
;
import
javax.crypto.NoSuchPaddingException
;
import
javax.crypto.spec.IvParameterSpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
org.bouncycastle.jce.provider.BouncyCastleProvider
;
/**
* AES加密
*
*/
public
class
AES
{
public
static
boolean
initialized
=
false
;
/**
* AES解密
*
* @param content
* 密文
* @return
* @throws InvalidAlgorithmParameterException
* @throws NoSuchProviderException
*/
public
byte
[]
decrypt
(
byte
[]
content
,
byte
[]
keyByte
,
byte
[]
ivByte
)
throws
InvalidAlgorithmParameterException
{
initialize
();
try
{
Cipher
cipher
=
Cipher
.
getInstance
(
"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
=
AlgorithmParameters
.
getInstance
(
"AES"
);
params
.
init
(
new
IvParameterSpec
(
iv
));
return
params
;
}
}
com.zrqx.third/src/main/java/com/zrqx/third/wechatlogin/config/WXCore.java
0 → 100644
浏览文件 @
240c1f15
package
com
.
zrqx
.
third
.
wechatlogin
.
config
;
import
org.apache.commons.codec.binary.Base64
;
import
com.alibaba.fastjson.JSONObject
;
/**
* 封装对外访问方法
*
*/
public
class
WXCore
{
private
static
final
String
WATERMARK
=
"watermark"
;
private
static
final
String
APPID
=
"appid"
;
/**
* 解密数据
* @return
* @throws Exception
*/
public
static
String
decrypt
(
String
appId
,
String
encryptedData
,
String
sessionKey
,
String
iv
){
String
result
=
""
;
try
{
AES
aes
=
new
AES
();
byte
[]
resultByte
=
aes
.
decrypt
(
Base64
.
decodeBase64
(
encryptedData
),
Base64
.
decodeBase64
(
sessionKey
),
Base64
.
decodeBase64
(
iv
));
if
(
null
!=
resultByte
&&
resultByte
.
length
>
0
){
result
=
new
String
(
WxPKCS7Encoder
.
decode
(
resultByte
));
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
result
);
String
decryptAppid
=
jsonObject
.
getJSONObject
(
WATERMARK
).
getString
(
APPID
);
if
(!
appId
.
equals
(
decryptAppid
)){
result
=
""
;
}
}
}
catch
(
Exception
e
)
{
result
=
""
;
e
.
printStackTrace
();
}
return
result
;
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
String
appId
=
"wx8be2cb2a503c6bad"
;
String
encryptedData
=
"rxOMA/+mOM+O1TL4ami24Ek4oh1HVKaDWhrLdHRHKqldw0M8HK46VD1YcOGJPBedH2D8zyKR4dx5gmNmcF1W42tHwhd+Q7jFxZ0HuIPdZcKaQ8LVl1ADLemoqHVeErheoICBySxiEHEuwVwV4BfiBpzDnprDrXfqJAUsRpN6TqdCqzmBJyZhO5sV13wRCGXXe8QuFbPomMuj75OqY5NC/w=="
;
String
sessionKey
=
"G/VGyQV+yt7kE8fCtuBsMA=="
;
String
iv
=
"OZ8lmwEyzueW92osjXGJgA=="
;
String
str
=
decrypt
(
appId
,
encryptedData
,
sessionKey
,
iv
);
System
.
out
.
println
(
str
);
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
str
);
String
phoneNumber
=
jsonObject
.
getString
(
"phoneNumber"
);
System
.
out
.
println
(
phoneNumber
);
}
}
com.zrqx.third/src/main/java/com/zrqx/third/wechatlogin/config/WeChatLoginXcxConfig.java
浏览文件 @
240c1f15
...
@@ -5,7 +5,7 @@ public class WeChatLoginXcxConfig {
...
@@ -5,7 +5,7 @@ public class WeChatLoginXcxConfig {
public
final
static
String
APPID
=
"wx8be2cb2a503c6bad"
;
public
final
static
String
APPID
=
"wx8be2cb2a503c6bad"
;
//密钥AppSecret
//密钥AppSecret
public
final
static
String
SECRET
=
"
ea31181b8ee6714a3fc9f2462eef7bb8
"
;
public
final
static
String
SECRET
=
"
0688f11dcf73931de7354cddefd78894
"
;
//固定值 填写 authorization_code
//固定值 填写 authorization_code
public
final
static
String
GRANT_TYPE
=
"authorization_code"
;
public
final
static
String
GRANT_TYPE
=
"authorization_code"
;
...
@@ -23,4 +23,5 @@ public class WeChatLoginXcxConfig {
...
@@ -23,4 +23,5 @@ public class WeChatLoginXcxConfig {
String
param
=
appid
+
secret
+
code1
+
grant_type
;
String
param
=
appid
+
secret
+
code1
+
grant_type
;
return
param
;
return
param
;
}
}
}
}
com.zrqx.third/src/main/java/com/zrqx/third/wechatlogin/config/WxPKCS7Encoder.java
0 → 100644
浏览文件 @
240c1f15
package
com
.
zrqx
.
third
.
wechatlogin
.
config
;
import
java.nio.charset.Charset
;
import
java.util.Arrays
;
/**
* 微信小程序加解密
*
*/
public
class
WxPKCS7Encoder
{
private
static
final
Charset
CHARSET
=
Charset
.
forName
(
"utf-8"
);
private
static
final
int
BLOCK_SIZE
=
32
;
/**
* 获得对明文进行补位填充的字节.
*
* @param count
* 需要进行填充补位操作的明文字节个数
* @return 补齐用的字节数组
*/
public
static
byte
[]
encode
(
int
count
)
{
// 计算需要填充的位数
int
amountToPad
=
BLOCK_SIZE
-
(
count
%
BLOCK_SIZE
);
if
(
amountToPad
==
0
)
{
amountToPad
=
BLOCK_SIZE
;
}
// 获得补位所用的字符
char
padChr
=
chr
(
amountToPad
);
String
tmp
=
new
String
();
for
(
int
index
=
0
;
index
<
amountToPad
;
index
++)
{
tmp
+=
padChr
;
}
return
tmp
.
getBytes
(
CHARSET
);
}
/**
* 删除解密后明文的补位字符
*
* @param decrypted
* 解密后的明文
* @return 删除补位字符后的明文
*/
public
static
byte
[]
decode
(
byte
[]
decrypted
)
{
int
pad
=
decrypted
[
decrypted
.
length
-
1
];
if
(
pad
<
1
||
pad
>
32
)
{
pad
=
0
;
}
return
Arrays
.
copyOfRange
(
decrypted
,
0
,
decrypted
.
length
-
pad
);
}
/**
* 将数字转化成ASCII码对应的字符,用于对明文进行补码
*
* @param a
* 需要转化的数字
* @return 转化得到的字符
*/
public
static
char
chr
(
int
a
)
{
byte
target
=
(
byte
)
(
a
&
0xFF
);
return
(
char
)
target
;
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论