Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jiwei-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
马天浩
jiwei-api
Commits
7fe79a6f
Commit
7fe79a6f
authored
Nov 25, 2020
by
matianhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[农民建房接口] <add> 接入农民建房接口
parent
bd96fd3e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
247 additions
and
4 deletions
+247
-4
FarmersBuildHouseController.java
...requestsecret/controller/FarmersBuildHouseController.java
+40
-0
RestTemplateService.java
...va/com/mth/requestsecret/service/RestTemplateService.java
+35
-4
RasUtil.java
src/main/java/com/mth/requestsecret/util/RasUtil.java
+172
-0
No files found.
src/main/java/com/mth/requestsecret/controller/FarmersBuildHouseController.java
0 → 100644
View file @
7fe79a6f
package
com
.
mth
.
requestsecret
.
controller
;
import
com.google.common.collect.Maps
;
import
com.mth.requestsecret.service.RestTemplateService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.Map
;
/**
* @author MaTianHao
* @date 2020/11/23
*/
@RestController
@Slf4j
public
class
FarmersBuildHouseController
{
@Autowired
private
RestTemplateService
restTemplateService
;
/**
* 农村建房接口
* 开始时间与结束时间间隔最多三个月
*
* @param beginDate 开始时间
* @param endDate 结束时间
* @return 响应实体
*/
@PostMapping
(
"/hourseInfo"
)
public
ResponseEntity
findHourseInfo
(
String
beginDate
,
String
endDate
)
{
Map
<
String
,
String
>
param
=
Maps
.
newHashMap
();
param
.
put
(
"beginDate"
,
beginDate
);
param
.
put
(
"endDate"
,
endDate
);
return
restTemplateService
.
buildHouseSendRequest
(
param
);
}
}
src/main/java/com/mth/requestsecret/service/RestTemplateService.java
View file @
7fe79a6f
...
...
@@ -2,16 +2,14 @@ package com.mth.requestsecret.service;
import
com.alibaba.fastjson.JSONObject
;
import
com.mth.requestsecret.scheduler.RequestSecretSchedulerTask
;
import
com.mth.requestsecret.util.AESUtils
;
import
com.mth.requestsecret.util.DSLUtils
;
import
com.mth.requestsecret.util.MD5Utils
;
import
com.mth.requestsecret.util.RedisUtils
;
import
com.mth.requestsecret.util.*
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang.StringEscapeUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.*
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.client.RestTemplate
;
...
...
@@ -19,6 +17,7 @@ import java.time.Instant;
import
java.time.LocalDateTime
;
import
java.time.ZoneOffset
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.Objects
;
import
static
com
.
mth
.
requestsecret
.
constant
.
Constants
.*;
...
...
@@ -63,6 +62,9 @@ public class RestTemplateService {
@Value
(
"${sgb.address}"
)
private
String
sgbAddress
;
@Value
(
"${buildhouse.address}"
)
private
String
buildHouseAddress
;
/**
* 瑞成平台接口发送请求
*
...
...
@@ -210,4 +212,33 @@ public class RestTemplateService {
return
StringEscapeUtils
.
unescapeXml
(
resultStr
);
}
/**
* 农民建房接口 发送请求
*/
public
ResponseEntity
buildHouseSendRequest
(
Map
<
String
,
String
>
param
)
{
// 获取签名
String
sign
=
RasUtil
.
getSign
(
param
);
MultiValueMap
<
String
,
String
>
paramMap
=
new
LinkedMultiValueMap
<>();
paramMap
.
add
(
"beginDate"
,
param
.
get
(
"beginDate"
));
paramMap
.
add
(
"endDate"
,
param
.
get
(
"endDate"
));
paramMap
.
add
(
"sign"
,
sign
);
// 1. 构造http请求头
HttpHeaders
headers
=
new
HttpHeaders
();
MediaType
mediaType
=
MediaType
.
parseMediaType
(
"application/x-www-form-urlencoded;charset=utf-8"
);
headers
.
setContentType
(
mediaType
);
HttpEntity
formEntity
=
new
HttpEntity
<>(
paramMap
,
headers
);
// 2. 返回结果
ResponseEntity
responseEntity
=
restTemplate
.
postForEntity
(
buildHouseAddress
,
formEntity
,
String
.
class
);
// 3. 日志记录
log
.
info
(
"农民建房url:{}"
,
sgbAddress
);
log
.
info
(
"api params:{}"
,
paramMap
);
log
.
info
(
"api response:{}"
,
responseEntity
);
return
responseEntity
;
}
}
\ No newline at end of file
src/main/java/com/mth/requestsecret/util/RasUtil.java
0 → 100644
View file @
7fe79a6f
package
com
.
mth
.
requestsecret
.
util
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.lang.StringUtils
;
import
java.nio.charset.StandardCharsets
;
import
java.security.KeyFactory
;
import
java.security.PrivateKey
;
import
java.security.PublicKey
;
import
java.security.spec.PKCS8EncodedKeySpec
;
import
java.security.spec.X509EncodedKeySpec
;
import
java.util.*
;
/**
* @author MaTianHao
* @date 2020/11/23
*/
public
class
RasUtil
{
//私钥
public
static
String
RAS_PRIVATE_KEY
=
"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOR8qd2JvHIf+t8v\n"
+
"m8HdiIhvdk2v8/jCvRKf8RTXoUnkdwZwMTg9uQgvkZOfBd75utckmzQRtS1JkEt8\n"
+
"vI0BzqxFJREeUa+76PijXPhf547z1aqfTJcxGsZH+iZHiOBSEyeQXzXzn72FxtGJ\n"
+
"Rcj58JC2ScGPpHRgS2k8t+XrnmkXAgMBAAECgYEAyb/6DY/tQahTUHctRUVjpXUm\n"
+
"NPrEpkbtxGBN82VjGWgOYTT3gP2fQEcgeATWnkhMXmQIVzW7kL2AF+eQZcHOxkra\n"
+
"mDL6mMkEeH2l5KrmQhmGoJcpwa9QwCOvR2VJ8GLg9/uH/6uE/ryeHs7Z5GX8j7Gq\n"
+
"5X7+9PLasrVDplRWQQECQQD8QlgOMJtQwF9ClEyMdxMmcgiYLPHioQZaJemznWwy\n"
+
"m534d8Ktw416TeUMSbUr5VvRYbfD5dhmKrWCZYGFhiA3AkEA5+ASoWD2G6W8Ecfm\n"
+
"aXvbe4bfdV+g8t+JTR8guQDkoH8CjqZDiF/U8QgVHNecQUWzSGoD14KRoolvOpp3\n"
+
"r9jOIQJAf9Q2vuoOqnrxStHPgJaOk7rRdBvCby9eAqyXorcxLuwAvvohIuCnfsSe\n"
+
"Pv3S2u2c+5ti/dgpMoyN8gfx/HGdPQJAHCjbOlwDJ3JmN6aWNeND0Wmcw61cZ4J8\n"
+
"MzB/kdgo69Dg56ALfYld2/PGVJ1erZKgZj/xvX9u3lVge7Z8qI9k4QJAE9byj4mS\n"
+
"pp/URG6XD/7tlRtqcPBdXYOkhL0R3XlqvaMTSN3R3b6expI9uufFb72kTXJRc8oA\n"
+
"3nl4+juWBljdSQ=="
;
//公钥
public
static
String
RAS_PUBLIC_KEY
=
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkfKndibxyH/rfL5vB3YiIb3ZN\n"
+
"r/P4wr0Sn/EU16FJ5HcGcDE4PbkIL5GTnwXe+brXJJs0EbUtSZBLfLyNAc6sRSUR\n"
+
"HlGvu+j4o1z4X+eO89Wqn0yXMRrGR/omR4jgUhMnkF8185+9hcbRiUXI+fCQtknB\n"
+
"j6R0YEtpPLfl655pFwIDAQAB"
;
/**
* 排列签名字段
*
* @param sortedParams
* @return
*/
public
static
String
getSignContent
(
Map
sortedParams
)
{
StringBuffer
content
=
new
StringBuffer
();
List
<
String
>
keys
=
new
ArrayList
<
String
>(
sortedParams
.
keySet
());
Collections
.
sort
(
keys
);
int
index
=
0
;
for
(
int
i
=
0
;
i
<
keys
.
size
();
i
++)
{
String
key
=
keys
.
get
(
i
);
if
(
"sign"
.
endsWith
(
key
))
{
continue
;
}
Object
valueObj
=
sortedParams
.
get
(
key
);
if
(
sortedParams
.
get
(
key
)
==
null
)
{
continue
;
}
String
value
=
String
.
valueOf
(
valueObj
);
if
(
StringUtils
.
isNotEmpty
(
key
)
&&
StringUtils
.
isNotEmpty
(
value
))
{
content
.
append
((
index
==
0
?
""
:
"&"
)
+
key
+
"="
+
value
);
index
++;
}
}
return
content
.
toString
();
}
/**
* 生成签名
*
* @param content
* @param privateKey
* @return
*/
public
static
String
sign
(
String
content
,
String
privateKey
)
{
try
{
PKCS8EncodedKeySpec
priPKCS8
=
new
PKCS8EncodedKeySpec
(
Base64
.
decodeBase64
(
privateKey
));
KeyFactory
keyf
=
KeyFactory
.
getInstance
(
"RSA"
);
PrivateKey
priKey
=
keyf
.
generatePrivate
(
priPKCS8
);
java
.
security
.
Signature
signature
=
java
.
security
.
Signature
.
getInstance
(
"SHA1WithRSA"
);
signature
.
initSign
(
priKey
);
signature
.
update
(
content
.
getBytes
());
byte
[]
signed
=
signature
.
sign
();
return
new
String
(
org
.
apache
.
commons
.
codec
.
binary
.
Base64
.
encodeBase64
(
signed
));
}
catch
(
Exception
e
)
{
throw
new
IllegalArgumentException
(
"加密失败"
);
}
}
/**
* 签名验证
*
* @param content
* @param sign
* @param lakala_public_key
* @return
*/
public
static
boolean
verify
(
String
content
,
String
sign
,
String
lakala_public_key
)
{
try
{
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
byte
[]
encodedKey
=
Base64
.
decodeBase64
(
lakala_public_key
);
PublicKey
pubKey
=
keyFactory
.
generatePublic
(
new
X509EncodedKeySpec
(
encodedKey
));
java
.
security
.
Signature
signature
=
java
.
security
.
Signature
.
getInstance
(
"SHA1WithRSA"
);
signature
.
initVerify
(
pubKey
);
signature
.
update
(
content
.
getBytes
(
StandardCharsets
.
UTF_8
));
boolean
bverify
=
signature
.
verify
(
Base64
.
decodeBase64
(
sign
));
return
bverify
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
public
static
boolean
checkSign
(
Map
<
String
,
String
>
param
)
{
if
(!
param
.
containsKey
(
"sign"
))
{
return
false
;
}
try
{
String
sign
=
param
.
get
(
"sign"
);
param
.
remove
(
"sign"
);
String
content
=
getSignContent
(
param
);
return
verify
(
content
,
sign
,
RAS_PUBLIC_KEY
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 根据参数封装签名
*
* @param param 参数
* @return sign签名
*/
public
static
String
getSign
(
Map
param
)
{
// 获取入参串
String
content
=
getSignContent
(
param
);
// 获取签名字符串
return
sign
(
content
,
RAS_PRIVATE_KEY
);
}
//演示说明
public
static
void
main
(
String
[]
args
)
{
Map
param
=
new
HashMap
();
param
.
put
(
"beginDate"
,
"2020-09"
);
param
.
put
(
"endDate"
,
"2020-11"
);
//获取入参串
String
content
=
getSignContent
(
param
);
//进行签名
String
sign
=
sign
(
content
,
RAS_PRIVATE_KEY
);
//到此已经生成签名了,将签名和参数一同传递即可
System
.
out
.
println
(
"生成签名:"
+
sign
);
boolean
check
=
verify
(
content
,
sign
,
RAS_PUBLIC_KEY
);
if
(
check
)
{
System
.
out
.
println
(
"验证签名成功"
);
}
else
{
System
.
out
.
println
(
"验证签名失败"
);
}
}
}
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