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
e5722c66
Commit
e5722c66
authored
Aug 13, 2020
by
matianhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[纪委接口] <update> 请求秘钥存入redis;接口返回签名错误时重新获取秘钥
parent
4dea01bd
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
929 additions
and
6 deletions
+929
-6
pom.xml
pom.xml
+7
-0
Constants.java
src/main/java/com/mth/requestsecret/constant/Constants.java
+31
-0
RequestController.java
...a/com/mth/requestsecret/controller/RequestController.java
+1
-1
RequestSecretSchedulerTask.java
...h/requestsecret/scheduler/RequestSecretSchedulerTask.java
+8
-1
RestTemlateService.java
...ava/com/mth/requestsecret/service/RestTemlateService.java
+27
-4
RedisUtils.java
src/main/java/com/mth/requestsecret/util/RedisUtils.java
+855
-0
No files found.
pom.xml
View file @
e5722c66
...
...
@@ -45,6 +45,13 @@
<artifactId>
lombok
</artifactId>
<version>
1.18.12
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
<version>
2.3.0.RELEASE
</version>
</dependency>
<dependency>
<groupId>
com.alibaba.cloud
</groupId>
<artifactId>
spring-cloud-starter-alibaba-nacos-discovery
</artifactId>
...
...
src/main/java/com/mth/requestsecret/constant/Constants.java
0 → 100644
View file @
e5722c66
package
com
.
mth
.
requestsecret
.
constant
;
/**
* @author MaTianHao
* @date 2020/8/13
*/
public
class
Constants
{
// redis存储请求秘钥key值
//-----------------------------------
/**
* 数据局接口
*/
public
static
final
String
SJJ_REQUEST_SECRET_PREFIX
=
"sjj.request.secret"
;
/**
* 招必得接口
*/
public
static
final
String
ZBD_REQUEST_SECRET_PREFIX
=
"zbd.request.secret"
;
// 数据局接口返回响应码
//-----------------------------------
/**
* 成功:00
*/
public
static
final
String
RESPONSE_CODE_00
=
"00"
;
/**
* sign参数错误:14
*/
public
static
final
String
RESPONSE_CODE_14
=
"14"
;
}
src/main/java/com/mth/requestsecret/controller/RequestController.java
View file @
e5722c66
...
...
@@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
* @date 2020/7/13
*/
@RestController
public
class
RequestController
extends
BaseController
{
public
class
RequestController
{
@Autowired
RestTemlateService
restTemlateService
;
...
...
src/main/java/com/mth/requestsecret/scheduler/RequestSecretSchedulerTask.java
View file @
e5722c66
package
com
.
mth
.
requestsecret
.
scheduler
;
import
com.alibaba.fastjson.JSONObject
;
import
com.mth.requestsecret.constant.Constants
;
import
com.mth.requestsecret.util.DSLUtils
;
import
com.mth.requestsecret.util.MD5Utils
;
import
com.mth.requestsecret.util.RedisUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -33,6 +35,9 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
@Autowired
private
RestTemplate
restTemplate
;
@Autowired
RedisUtils
redisUtil
;
@Value
(
"${DATA_API_ADDRESS}"
)
private
String
pathUrl
;
...
...
@@ -75,7 +80,7 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
/**
* 获取请求秘钥
*/
p
rivate
void
getRequestSecret
()
{
p
ublic
void
getRequestSecret
()
{
String
url
=
pathUrl
+
"/interface/public/service/risen-inte/reTokenByKey.action"
+
"?appKey={appKey}&sign={sign}&requestTime={requestTime}"
;
...
...
@@ -100,6 +105,8 @@ public class RequestSecretSchedulerTask implements SchedulingConfigurer {
requestSecretEndTime
=
Long
.
parseLong
(
datas
.
getString
(
"requestSecretEndTime"
));
refreshSecret
=
datas
.
getString
(
"refreshSecret"
);
refreshSecretEndTime
=
Long
.
parseLong
(
datas
.
getString
(
"refreshSecretEndTime"
));
// 保存请求秘钥至redis
redisUtil
.
set
(
Constants
.
SJJ_REQUEST_SECRET_PREFIX
,
requestSecret
);
}
}
...
...
src/main/java/com/mth/requestsecret/service/RestTemlateService.java
View file @
e5722c66
package
com
.
mth
.
requestsecret
.
service
;
import
com.alibaba.fastjson.JSONObject
;
import
com.mth.requestsecret.scheduler.RequestSecretSchedulerTask
;
import
com.mth.requestsecret.util.DSLUtils
;
import
com.mth.requestsecret.util.MD5Utils
;
import
com.mth.requestsecret.util.RedisUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
...
...
@@ -13,6 +15,9 @@ import org.springframework.web.client.RestTemplate;
import
java.util.Date
;
import
static
com
.
mth
.
requestsecret
.
constant
.
Constants
.
RESPONSE_CODE_14
;
import
static
com
.
mth
.
requestsecret
.
constant
.
Constants
.
SJJ_REQUEST_SECRET_PREFIX
;
/**
* @author fyl
* @version v1.0
...
...
@@ -27,6 +32,12 @@ public class RestTemlateService {
@Autowired
private
RestTemplate
restTemplate
;
@Autowired
private
RedisUtils
redisUtil
;
@Autowired
private
RequestSecretSchedulerTask
schedulerTask
;
@Value
(
"${DATA_API_ADDRESS}"
)
private
String
pathUrl
;
...
...
@@ -42,12 +53,15 @@ public class RestTemlateService {
// 请求时间
String
requestTime
=
DSLUtils
.
dateToLong
(
new
Date
())
+
""
;
// redis中获取秘钥
String
requestSecret
=
redisUtil
.
get
(
SJJ_REQUEST_SECRET_PREFIX
);
log
.
info
(
"redis中请求秘钥:{}"
,
requestSecret
);
// 签名字符串
String
signStr
=
appKey
+
RequestSecretSchedulerTask
.
requestSecret
+
requestTime
;
String
signStr
=
appKey
+
requestSecret
+
requestTime
;
// 组装请求参数
paramMap
.
add
(
"appKey"
,
appKey
);
paramMap
.
add
(
"sign"
,
MD5Utils
.
encoderByMd5
(
signStr
));
paramMap
.
add
(
"requestTime"
,
requestTime
);
paramMap
.
set
(
"appKey"
,
appKey
);
paramMap
.
set
(
"sign"
,
MD5Utils
.
encoderByMd5
(
signStr
));
paramMap
.
set
(
"requestTime"
,
requestTime
);
// 日志记录
log
.
info
(
"纪委api:{}"
,
apiMethod
);
...
...
@@ -60,6 +74,15 @@ public class RestTemlateService {
HttpEntity
<
MultiValueMap
<
String
,
Object
>>
request
=
new
HttpEntity
<>(
paramMap
,
headers
);
ResponseEntity
<
String
>
responseEntity
=
restTemplate
.
exchange
(
url
.
toString
(),
HttpMethod
.
POST
,
request
,
String
.
class
);
log
.
info
(
"api response:{}"
,
responseEntity
);
// 接口返回码
String
code
=
JSONObject
.
parseObject
(
responseEntity
.
getBody
()).
getString
(
"code"
);
// 如果返回签名错误,则重新获取秘钥,重新发送请求
if
(
RESPONSE_CODE_14
.
equals
(
code
))
{
schedulerTask
.
getRequestSecret
();
return
commonSendRequest
(
paramMap
,
apiMethod
);
}
return
responseEntity
;
}
}
\ No newline at end of file
src/main/java/com/mth/requestsecret/util/RedisUtils.java
0 → 100644
View file @
e5722c66
package
com
.
mth
.
requestsecret
.
util
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.CollectionUtils
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.TimeUnit
;
/**
* Redis工具类
*
* @author MaTianHao
* @date 2020/8/13
*/
@Component
public
class
RedisUtils
{
@Autowired
private
RedisTemplate
<
String
,
String
>
redisTemplate
;
// =============================common============================
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public
boolean
expire
(
String
key
,
long
time
)
{
try
{
if
(
time
>
0
)
{
redisTemplate
.
expire
(
key
,
time
,
TimeUnit
.
SECONDS
);
}
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public
long
getExpire
(
String
key
)
{
return
redisTemplate
.
getExpire
(
key
,
TimeUnit
.
SECONDS
);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public
boolean
hasKey
(
String
key
)
{
try
{
return
redisTemplate
.
hasKey
(
key
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@SuppressWarnings
(
"unchecked"
)
public
void
del
(
String
...
key
)
{
if
(
key
!=
null
&&
key
.
length
>
0
)
{
if
(
key
.
length
==
1
)
{
redisTemplate
.
delete
(
key
[
0
]);
}
else
{
redisTemplate
.
delete
(
CollectionUtils
.
arrayToList
(
key
));
}
}
}
// ============================String=============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public
String
get
(
String
key
)
{
return
key
==
null
?
null
:
redisTemplate
.
opsForValue
().
get
(
key
);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public
boolean
set
(
String
key
,
String
value
)
{
try
{
redisTemplate
.
opsForValue
().
set
(
key
,
value
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public
boolean
set
(
String
key
,
String
value
,
long
time
)
{
try
{
if
(
time
>
0
)
{
redisTemplate
.
opsForValue
().
set
(
key
,
value
,
time
,
TimeUnit
.
SECONDS
);
}
else
{
set
(
key
,
value
);
}
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
// ================================Map=================================
/**
* 157
* HashGet
* 158
*
* @param key 键 不能为null
* 159
* @param item 项 不能为null
* 160
* @return 值
* 161
*/
public
Object
hget
(
String
key
,
String
item
)
{
return
redisTemplate
.
opsForHash
().
get
(
key
,
item
);
}
/**
* 167
* 获取hashKey对应的所有键值
* 168
*
* @param key 键
* 169
* @return 对应的多个键值
* 170
*/
public
Map
<
Object
,
Object
>
hmget
(
String
key
)
{
return
redisTemplate
.
opsForHash
().
entries
(
key
);
}
/**
* 176
* HashSet
* 177
*
* @param key 键
* 178
* @param map 对应多个键值
* 179
* @return true 成功 false 失败
* 180
*/
public
boolean
hmset
(
String
key
,
Map
<
String
,
String
>
map
)
{
try
{
redisTemplate
.
opsForHash
().
putAll
(
key
,
map
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 192
* HashSet 并设置时间
* 193
*
* @param key 键
* 194
* @param map 对应多个键值
* 195
* @param time 时间(秒)
* 196
* @return true成功 false失败
* 197
*/
public
boolean
hmset
(
String
key
,
Map
<
String
,
String
>
map
,
long
time
)
{
try
{
redisTemplate
.
opsForHash
().
putAll
(
key
,
map
);
if
(
time
>
0
)
{
expire
(
key
,
time
);
}
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 212
* 向一张hash表中放入数据,如果不存在将创建
* 213
*
* @param key 键
* 214
* @param item 项
* 215
* @param value 值
* 216
* @return true 成功 false失败
* 217
*/
public
boolean
hset
(
String
key
,
String
item
,
String
value
)
{
try
{
redisTemplate
.
opsForHash
().
put
(
key
,
item
,
value
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 229
* 向一张hash表中放入数据,如果不存在将创建
* 230
*
* @param key 键
* 231
* @param item 项
* 232
* @param value 值
* 233
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* 234
* @return true 成功 false失败
* 235
*/
public
boolean
hset
(
String
key
,
String
item
,
String
value
,
long
time
)
{
try
{
redisTemplate
.
opsForHash
().
put
(
key
,
item
,
value
);
if
(
time
>
0
)
{
expire
(
key
,
time
);
}
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 250
* 删除hash表中的值
* 251
*
* @param key 键 不能为null
* 252
* @param item 项 可以使多个 不能为null
* 253
*/
public
void
hdel
(
String
key
,
String
...
item
)
{
redisTemplate
.
opsForHash
().
delete
(
key
,
item
);
}
/**
* 259
* 判断hash表中是否有该项的值
* 260
*
* @param key 键 不能为null
* 261
* @param item 项 不能为null
* 262
* @return true 存在 false不存在
* 263
*/
public
boolean
hHasKey
(
String
key
,
String
item
)
{
return
redisTemplate
.
opsForHash
().
hasKey
(
key
,
item
);
}
/**
* 269
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
* 270
*
* @param key 键
* 271
* @param item 项
* 272
* @param by 要增加几(大于0)
* 273
* @return 274
*/
public
double
hincr
(
String
key
,
String
item
,
double
by
)
{
return
redisTemplate
.
opsForHash
().
increment
(
key
,
item
,
by
);
}
/**
* 280
* hash递减
* 281
*
* @param key 键
* 282
* @param item 项
* 283
* @param by 要减少记(小于0)
* 284
* @return 285
*/
public
double
hdecr
(
String
key
,
String
item
,
double
by
)
{
return
redisTemplate
.
opsForHash
().
increment
(
key
,
item
,
-
by
);
}
// ============================set=============================
/**
* 292
* 根据key获取Set中的所有值
* 293
*
* @param key 键
* 294
* @return 295
*/
public
Set
<
String
>
sGet
(
String
key
)
{
try
{
return
redisTemplate
.
opsForSet
().
members
(
key
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
/**
* 306
* 根据value从一个set中查询,是否存在
* 307
*
* @param key 键
* 308
* @param value 值
* 309
* @return true 存在 false不存在
* 310
*/
public
boolean
sHasKey
(
String
key
,
String
value
)
{
try
{
return
redisTemplate
.
opsForSet
().
isMember
(
key
,
value
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 321
* 将数据放入set缓存
* 322
*
* @param key 键
* 323
* @param values 值 可以是多个
* 324
* @return 成功个数
* 325
*/
public
long
sSet
(
String
key
,
String
...
values
)
{
try
{
return
redisTemplate
.
opsForSet
().
add
(
key
,
values
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
0
;
}
}
/**
* 336
* 将set数据放入缓存
* 337
*
* @param key 键
* 338
* @param time 时间(秒)
* 339
* @param values 值 可以是多个
* 340
* @return 成功个数
* 341
*/
public
long
sSetAndTime
(
String
key
,
long
time
,
String
...
values
)
{
try
{
Long
count
=
redisTemplate
.
opsForSet
().
add
(
key
,
values
);
if
(
time
>
0
)
expire
(
key
,
time
);
return
count
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
0
;
}
}
/**
* 355
* 获取set缓存的长度
* 356
*
* @param key 键
* 357
* @return 358
*/
public
long
sGetSetSize
(
String
key
)
{
try
{
return
redisTemplate
.
opsForSet
().
size
(
key
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
0
;
}
}
/**
* 369
* 移除值为value的
* 370
*
* @param key 键
* 371
* @param values 值 可以是多个
* 372
* @return 移除的个数
* 373
*/
public
long
setRemove
(
String
key
,
String
...
values
)
{
try
{
Long
count
=
redisTemplate
.
opsForSet
().
remove
(
key
,
values
);
return
count
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
0
;
}
}
// ===============================list=================================
/**
* 386
* 获取list缓存的内容
* 387
*
* @param key 键
* 388
* @param start 开始
* 389
* @param end 结束 0 到 -1代表所有值
* 390
* @return 391
*/
public
List
<
String
>
lGet
(
String
key
,
long
start
,
long
end
)
{
try
{
return
redisTemplate
.
opsForList
().
range
(
key
,
start
,
end
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
/**
* 402
* 获取list缓存的长度
* 403
*
* @param key 键
* 404
* @return 405
*/
public
long
lGetListSize
(
String
key
)
{
try
{
return
redisTemplate
.
opsForList
().
size
(
key
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
0
;
}
}
/**
* 416
* 通过索引 获取list中的值
* 417
*
* @param key 键
* 418
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
* 419
* @return 420
*/
public
String
lGetIndex
(
String
key
,
long
index
)
{
try
{
return
redisTemplate
.
opsForList
().
index
(
key
,
index
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
/**
* 431
* 将list放入缓存
* 432
*
* @param key 键
* 433
* @param value 值
* 434
* 435
* @return 436
*/
public
boolean
lSet
(
String
key
,
String
value
)
{
try
{
redisTemplate
.
opsForList
().
rightPush
(
key
,
value
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 将list放入缓存
*
* @param key 键
* @param value 值
* @param time 时间(秒)
* @return
*/
public
boolean
lSet
(
String
key
,
String
value
,
long
time
)
{
try
{
redisTemplate
.
opsForList
().
rightPush
(
key
,
value
);
if
(
time
>
0
)
expire
(
key
,
time
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 467
* 将list放入缓存
* 468
*
* @param key 键
* 469
* @param value 值
* 470
* 471
* @return 472
*/
public
boolean
lSet
(
String
key
,
List
<
String
>
value
)
{
try
{
redisTemplate
.
opsForList
().
rightPushAll
(
key
,
value
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 484
* 将list放入缓存
* 485
* <p>
* 486
*
* @param key 键
* 487
* @param value 值
* 488
* @param time 时间(秒)
* 489
* @return 490
*/
public
boolean
lSet
(
String
key
,
List
<
String
>
value
,
long
time
)
{
try
{
redisTemplate
.
opsForList
().
rightPushAll
(
key
,
value
);
if
(
time
>
0
)
expire
(
key
,
time
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 504
* 根据索引修改list中的某条数据
* 505
*
* @param key 键
* 506
* @param index 索引
* 507
* @param value 值
* 508
* @return 509
*/
public
boolean
lUpdateIndex
(
String
key
,
long
index
,
String
value
)
{
try
{
redisTemplate
.
opsForList
().
set
(
key
,
index
,
value
);
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
/**
* 521
* 移除N个值为value
* 522
*
* @param key 键
* 523
* @param count 移除多少个
* 524
* @param value 值
* 525
* @return 移除的个数
* 526
*/
public
long
lRemove
(
String
key
,
long
count
,
String
value
)
{
try
{
Long
remove
=
redisTemplate
.
opsForList
().
remove
(
key
,
count
,
value
);
return
remove
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
0
;
}
}
}
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