Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
holmes
Commits
2eae701f
Commit
2eae701f
authored
Aug 26, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
兰银mock
parent
379ad913
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
349 additions
and
0 deletions
+349
-0
InterceptorConfig.java
src/main/java/cn/qg/holmes/config/InterceptorConfig.java
+7
-0
LanzhouBankEncrypt.java
src/main/java/cn/qg/holmes/encrypt/LanzhouBankEncrypt.java
+193
-0
BaseReq.java
src/main/java/cn/qg/holmes/entity/lanzhou/BaseReq.java
+55
-0
BaseResp.java
src/main/java/cn/qg/holmes/entity/lanzhou/BaseResp.java
+13
-0
LanzhouBankInterceptor.java
...java/cn/qg/holmes/interceptor/LanzhouBankInterceptor.java
+81
-0
No files found.
src/main/java/cn/qg/holmes/config/InterceptorConfig.java
View file @
2eae701f
package
cn
.
qg
.
holmes
.
config
;
import
cn.qg.holmes.interceptor.LanzhouBankInterceptor
;
import
cn.qg.holmes.interceptor.RuleEngineInterceptor
;
import
cn.qg.holmes.interceptor.XinchengInterceptor
;
import
cn.qg.holmes.interceptor.YeebaoInterceptor
;
...
...
@@ -29,10 +30,16 @@ public class InterceptorConfig implements WebMvcConfigurer {
return
new
XinchengInterceptor
();
}
@Bean
public
LanzhouBankInterceptor
lanzhouBankInterceptor
()
{
return
new
LanzhouBankInterceptor
();
}
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
registry
.
addInterceptor
(
yeebaoInterceptor
()).
addPathPatterns
(
"/tzt-api/**"
,
"/balance-board/**"
);
registry
.
addInterceptor
(
ruleEngineInterceptor
()).
addPathPatterns
(
"/rule_engine/**"
,
"/ex/isInWhiteList"
);
registry
.
addInterceptor
(
xinchengInterceptor
()).
addPathPatterns
(
"/webservice/api/lhp/**"
);
registry
.
addInterceptor
(
lanzhouBankInterceptor
()).
addPathPatterns
(
"/api/service/lhp"
);
}
}
src/main/java/cn/qg/holmes/encrypt/LanzhouBankEncrypt.java
0 → 100644
View file @
2eae701f
package
cn
.
qg
.
holmes
.
encrypt
;
import
org.apache.commons.codec.DecoderException
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.codec.binary.Hex
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
javax.crypto.Cipher
;
import
javax.crypto.KeyGenerator
;
import
javax.crypto.SecretKey
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.io.ByteArrayInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.charset.Charset
;
import
java.security.*
;
import
java.security.spec.PKCS8EncodedKeySpec
;
import
java.security.spec.X509EncodedKeySpec
;
import
java.util.UUID
;
/**
* Created by Baiye on 25/01/2019.
*
* @author Baiye
*/
@SuppressWarnings
(
"ALL"
)
public
class
LanzhouBankEncrypt
{
private
static
Charset
DEFAULT_CHARSET
=
Charset
.
forName
(
"UTF-8"
);
/**
* 使用指定的字符串生成秘钥
*
* @param password
* @throws NoSuchAlgorithmException
*/
private
static
String
getRandomKey
(
String
password
)
throws
NoSuchAlgorithmException
{
//生成秘钥
String
s
=
""
;
KeyGenerator
kg
=
KeyGenerator
.
getInstance
(
"AES"
);
// kg.init(128);//要生成多少位,只需要修改这里即可128, 192或256
//SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以生成的秘钥就一样。
kg
.
init
(
128
,
new
SecureRandom
(
password
.
getBytes
()));
SecretKey
sk
=
kg
.
generateKey
();
byte
[]
b
=
sk
.
getEncoded
();
s
=
byteToHexString
(
b
);
return
s
;
}
/**
* 使用指定的字符串生成秘钥
*
* @throws NoSuchAlgorithmException
*/
private
static
String
getRandomKey
()
throws
NoSuchAlgorithmException
{
return
getRandomKey
(
UUID
.
randomUUID
().
toString
());
}
/**
* 十六进制string转二进制byte[]
*/
private
static
byte
[]
hexStringToByte
(
String
s
)
throws
DecoderException
{
return
Hex
.
decodeHex
(
s
);
}
/**
* 二进制byte[]转十六进制string
*/
private
static
String
byteToHexString
(
byte
[]
bytes
)
{
return
Hex
.
encodeHexString
(
bytes
);
}
private
static
String
rsapubKeyEnc
(
String
content
,
String
pubKey
)
throws
IOException
{
InputStream
is
=
null
;
try
{
KeyFactory
keyf
=
KeyFactory
.
getInstance
(
"RSA"
);
//获取公钥
is
=
new
ByteArrayInputStream
(
pubKey
.
getBytes
(
"utf-8"
));
byte
[]
pubbytes
=
new
byte
[
new
Long
(
pubKey
.
length
()).
intValue
()];
is
.
read
(
pubbytes
);
X509EncodedKeySpec
pubX509
=
new
X509EncodedKeySpec
(
Base64
.
decodeBase64
(
pubbytes
));
PublicKey
pkey
=
keyf
.
generatePublic
(
pubX509
);
//公钥加密
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA/ECB/PKCS1Padding"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
pkey
);
byte
[]
cipherText
=
cipher
.
doFinal
(
content
.
getBytes
());
// 将加密结果转换为Base64编码结果;便于internet传送
return
Base64
.
encodeBase64String
(
cipherText
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
RuntimeException
(
e
);
}
finally
{
if
(
is
!=
null
)
{
is
.
close
();
}
}
}
private
static
byte
[]
aesEncrypt
(
byte
[]
key
,
byte
[]
src
)
throws
Exception
{
SecretKeySpec
skeySpec
=
new
SecretKeySpec
(
key
,
"AES"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"AES"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
skeySpec
);
return
cipher
.
doFinal
(
src
);
}
private
static
byte
[]
aesDecrypt
(
byte
[]
key
,
byte
[]
encrypted
)
throws
Exception
{
SecretKeySpec
skeySpec
=
new
SecretKeySpec
(
key
,
"AES"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"AES"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
skeySpec
);
return
cipher
.
doFinal
(
encrypted
);
}
/**
* RSA私钥解密
*
* @param ciphertext 经BASE84编码过的待解密密文
* @param privKey RSA私钥
* @return utf-8编码的明文
*/
private
static
String
rsaprivKeyDec
(
String
ciphertext
,
String
privKey
)
{
try
{
KeyFactory
keyf
=
KeyFactory
.
getInstance
(
"RSA"
);
// 获取私钥
InputStream
key
=
new
ByteArrayInputStream
(
privKey
.
getBytes
(
"utf-8"
));
byte
[]
pribytes
=
new
byte
[
new
Long
(
privKey
.
length
()).
intValue
()];
key
.
read
(
pribytes
);
byte
[]
buffer
=
Base64
.
decodeBase64
(
pribytes
);
PKCS8EncodedKeySpec
priPKCS8
=
new
PKCS8EncodedKeySpec
(
buffer
);
PrivateKey
prikey
=
keyf
.
generatePrivate
(
priPKCS8
);
//私钥解密
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA/ECB/PKCS1Padding"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
prikey
);
byte
[]
text
=
Base64
.
decodeBase64
(
ciphertext
);
byte
[]
content
=
cipher
.
doFinal
(
text
);
return
new
String
(
content
,
"UTF-8"
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
RuntimeException
(
e
);
}
}
/**
* 易贷云申请报文加密
*
* @param requestMessage 申请信息报文
* @param publicKey 加密私钥
* @return String[] 返回数组,数组下表0为加密信息 数组下表1为加密密钥
* @throws Exception
*/
public
static
String
[]
encrypt
(
byte
[]
requestMessage
,
String
publicKey
)
{
try
{
String
[]
byteReturn
=
new
String
[
2
];
//获得随机密钥信息
String
randomKey
=
getRandomKey
();
byte
[]
encodeMessageByte
=
aesEncrypt
(
hexStringToByte
(
randomKey
),
requestMessage
);
String
eMessage
=
byteToHexString
(
encodeMessageByte
);
String
signature
=
rsapubKeyEnc
(
randomKey
,
publicKey
);
byteReturn
[
0
]
=
eMessage
;
byteReturn
[
1
]
=
signature
;
return
byteReturn
;
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"加密异常"
,
e
);
}
}
/**
* 易贷云申请报文解密
*
* @param message 申请信息
* @param signature 密钥
* @return String 返回解密的信息明文
* @throws Exception
*/
public
static
String
decrypt
(
String
message
,
String
signature
,
String
privateKey
)
{
String
dKey
=
rsaprivKeyDec
(
signature
,
privateKey
);
byte
[]
stringByte
;
try
{
stringByte
=
aesDecrypt
(
hexStringToByte
(
dKey
),
hexStringToByte
(
message
));
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"解密失败"
,
e
);
}
return
new
String
(
stringByte
,
DEFAULT_CHARSET
);
}
/***
* MD5加码 生成32位md5码
*/
public
static
String
MD5
(
String
inStr
)
{
return
DigestUtils
.
md5Hex
(
inStr
);
}
}
src/main/java/cn/qg/holmes/entity/lanzhou/BaseReq.java
0 → 100644
View file @
2eae701f
package
cn
.
qg
.
holmes
.
entity
.
lanzhou
;
import
cn.qg.holmes.encrypt.LanzhouBankEncrypt
;
import
com.alibaba.fastjson.annotation.JSONField
;
import
lombok.Data
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.time.DateFormatUtils
;
import
java.util.Date
;
@Slf4j
@Data
public
class
BaseReq
<
T
>
{
/**
* 调用的终端类型
*/
private
String
transcode
;
/**
* 交易代码
*/
private
String
sysid
;
/**
* 交易时间
*/
private
String
transtime
;
private
String
sign
;
/**
* 终端类型
* PC
* WECHAT
* ANDROID
* IOS
*/
private
String
deviceType
=
"PC"
;
@JSONField
(
name
=
"RequestParams"
)
private
T
t
;
public
BaseReq
(
T
t
,
String
transcode
,
String
md5key
,
String
publicKey
,
String
sysId
)
{
this
.
t
=
t
;
this
.
transcode
=
transcode
;
this
.
transtime
=
DateFormatUtils
.
format
(
new
Date
(),
"yyyyMMddHHmmss"
);
this
.
sysid
=
sysId
;
this
.
sign
=
getSign
(
md5key
);
}
public
String
getSign
(
String
md5key
)
{
return
LanzhouBankEncrypt
.
MD5
(
sysid
+
"|"
+
transtime
+
"|"
+
md5key
);
}
}
src/main/java/cn/qg/holmes/entity/lanzhou/BaseResp.java
0 → 100644
View file @
2eae701f
package
cn
.
qg
.
holmes
.
entity
.
lanzhou
;
import
lombok.Data
;
@Data
public
class
BaseResp
<
T
>
{
private
T
result
;
private
boolean
issuccess
;
private
String
rtncode
;
private
String
rtnmessage
;
private
String
solution
;
}
src/main/java/cn/qg/holmes/interceptor/LanzhouBankInterceptor.java
0 → 100644
View file @
2eae701f
package
cn
.
qg
.
holmes
.
interceptor
;
import
cn.qg.holmes.encrypt.LanzhouBankEncrypt
;
import
cn.qg.holmes.entity.lanzhou.BaseReq
;
import
cn.qg.holmes.entity.lanzhou.BaseResp
;
import
com.alibaba.fastjson.JSON
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.lang.Nullable
;
import
org.springframework.web.servlet.HandlerInterceptor
;
import
org.springframework.web.servlet.ModelAndView
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* 兰银mock拦截器
*/
@Slf4j
public
class
LanzhouBankInterceptor
implements
HandlerInterceptor
{
@Value
(
"${lanzhou.fund.public.key}"
)
private
String
lanzhouPublicKey
;
@Value
(
"${lanzhou.lhp.private.key}"
)
private
String
lhpPrivateKey
;
@Override
public
boolean
preHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
)
throws
Exception
{
String
uri
=
request
.
getRequestURI
();
log
.
info
(
"收到兰银{}接口请求:"
,
uri
);
Map
<
String
,
String
[]>
parameterMap
=
request
.
getParameterMap
();
Map
<
String
,
Object
>
requestJson
=
InterceptorUtils
.
convertParameterMap
(
parameterMap
);
String
requestMessage
=
LanzhouBankEncrypt
.
decrypt
(
requestJson
.
get
(
"message"
).
toString
(),
requestJson
.
get
(
"signature"
).
toString
(),
lhpPrivateKey
);
Map
<
String
,
Object
>
responseMap
=
new
HashMap
<>();
if
(
StringUtils
.
isNotBlank
(
requestMessage
))
{
BaseReq
<
Map
<
String
,
String
>>
req
=
JSON
.
parseObject
(
requestMessage
,
BaseReq
.
class
);
if
(
req
.
getTranscode
().
equals
(
"channel.common.queryapplyresult"
))
{
// 资方预审结果查询
Map
<
String
,
String
>
params
=
req
.
getT
();
responseMap
.
put
(
"channelserialno"
,
params
.
get
(
"channelserialno"
));
responseMap
.
put
(
"producttype"
,
params
.
get
(
"producttype"
));
responseMap
.
put
(
"submitflag"
,
"10"
);
// 10-授信成功,04-授信失败,03-处理中
}
else
if
(
req
.
getTranscode
().
equals
(
"channel.common.querybusinessrate"
))
{
// 利率查询接口
responseMap
.
put
(
"businessrate"
,
0.36
);
}
BaseResp
<
Map
<
String
,
Object
>>
resp
=
new
BaseResp
();
resp
.
setResult
(
responseMap
);
resp
.
setIssuccess
(
true
);
resp
.
setRtncode
(
"0000"
);
resp
.
setRtnmessage
(
"请求成功"
);
String
[]
messageArray
=
LanzhouBankEncrypt
.
encrypt
(
JSON
.
toJSONString
(
resp
).
getBytes
(),
lanzhouPublicKey
);
Map
<
String
,
String
>
respStr
=
new
HashMap
<>();
respStr
.
put
(
"message"
,
messageArray
[
0
]);
respStr
.
put
(
"signature"
,
messageArray
[
1
]);
InterceptorUtils
.
constructResponse
(
response
,
JSON
.
toJSONString
(
respStr
),
"application/json; charset=utf-8"
);
return
false
;
}
else
{
log
.
info
(
"接口{} 的mock暂不支持!"
,
uri
);
Map
<
String
,
String
>
respStr
=
new
HashMap
<>();
respStr
.
put
(
"message"
,
"Mock暂不支持!"
);
InterceptorUtils
.
constructResponse
(
response
,
JSON
.
toJSONString
(
respStr
),
"application/json; charset=utf-8"
);
return
false
;
}
}
@Override
public
void
postHandle
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
@Nullable
ModelAndView
modelAndView
)
{
}
@Override
public
void
afterCompletion
(
HttpServletRequest
request
,
HttpServletResponse
response
,
Object
handler
,
@Nullable
Exception
ex
)
{
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment