Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
smart_gov_platform
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵啸非
smart_gov_platform
Commits
5b8aa538
Commit
5b8aa538
authored
Mar 05, 2025
by
赵啸非
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
测试流控功能
parent
68a0603f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
378 additions
and
29 deletions
+378
-29
base-manager/pom.xml
base-manager/pom.xml
+9
-0
base-manager/src/main/java/com/mortals/xhx/busiz/DiscussPostRepository.java
...ain/java/com/mortals/xhx/busiz/DiscussPostRepository.java
+15
-0
base-manager/src/main/java/com/mortals/xhx/busiz/message/impl/MessageServiceImpl.java
...om/mortals/xhx/busiz/message/impl/MessageServiceImpl.java
+101
-29
base-manager/src/main/java/com/mortals/xhx/busiz/web/ElasticsearchTestsController.java
...m/mortals/xhx/busiz/web/ElasticsearchTestsController.java
+120
-0
base-manager/src/main/java/com/mortals/xhx/common/utils/SecurityUtils.java
...main/java/com/mortals/xhx/common/utils/SecurityUtils.java
+133
-0
No files found.
base-manager/pom.xml
View file @
5b8aa538
...
...
@@ -241,6 +241,15 @@
<artifactId>
DmJdbcDriver18
</artifactId>
<version>
8.1.1.193
</version>
</dependency>
<!-- Spring Boot Data Elasticsearch依赖 -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-elasticsearch
</artifactId>
<version>
2.7.18
</version>
</dependency>
</dependencies>
...
...
base-manager/src/main/java/com/mortals/xhx/busiz/DiscussPostRepository.java
0 → 100644
View file @
5b8aa538
//package com.mortals.xhx.busiz;
//import com.nowcoder.community.entity.DiscussPost;
//import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
//import org.springframework.stereotype.Repository;
//
///*
//ElasticsearchRepository<DiscussPost, Integer>
//DiscussPost:接口要处理的实体类
//Integer:实体类中的主键是什么类型
//ElasticsearchRepository:父接口,其中已经事先定义好了对es服务器访问的增删改查各种方法。Spring会给它自动做一个实现,我们直接去调就可以了。
// */
//@Repository
//public interface DiscussPostRepository extends ElasticsearchRepository<DiscussPost, Integer> {
//
//}
base-manager/src/main/java/com/mortals/xhx/busiz/message/impl/MessageServiceImpl.java
View file @
5b8aa538
...
...
@@ -9,14 +9,17 @@ import com.mortals.xhx.busiz.message.MessageService;
import
com.mortals.xhx.busiz.req.MessageReq
;
import
com.mortals.xhx.busiz.rsp.MessageResp
;
import
com.mortals.xhx.common.utils.RSAUtils
;
import
com.mortals.xhx.common.utils.SecurityUtils
;
import
com.mortals.xhx.common.utils.ServicePlatformInvoker
;
import
lombok.extern.slf4j.Slf4j
;
import
org.bouncycastle.jce.provider.BouncyCastleProvider
;
import
org.springframework.http.*
;
import
org.springframework.lang.NonNull
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.Assert
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.client.RestTemplate
;
import
javax.crypto.Cipher
;
import
javax.crypto.spec.SecretKeySpec
;
...
...
@@ -40,6 +43,8 @@ import java.util.List;
public
class
MessageServiceImpl
implements
MessageService
{
private
RestTemplate
restTemplate
=
new
RestTemplate
();
@Override
public
MessageReq
decryptMessage
(
MessageReq
req
)
{
if
(
"RSA"
.
equalsIgnoreCase
(
req
.
getAlgorithm
()))
{
...
...
@@ -58,36 +63,11 @@ public class MessageServiceImpl implements MessageService {
throw
new
AppException
(
e
);
}
/* SM4 sm4 = SmUtil.sm4(req.getPassword().getBytes());
String decryptContent = sm4.decryptStr(req.getContent());*/
/* SM4Utils sm4Utils = new SM4Utils();
sm4Utils.setHexString(false);
sm4Utils.setSecretKey(req.getPassword());
log.info("password:{},secretKey:{},keybytes:{}",req.getPassword(),sm4Utils.getSecretKey(),sm4Utils.getSecretKey().getBytes());
String decryptContent = sm4Utils.decryptData_ECB(req.getContent());*/
MessageReq
messageReq
=
new
MessageReq
();
messageReq
.
setKey
(
req
.
getKey
());
messageReq
.
setDecryptContent
(
decryptContent
);
return
messageReq
;
}
else
if
(
"SM2"
.
equalsIgnoreCase
(
req
.
getAlgorithm
()))
{
/* SM2 sm2 = SmUtil.sm2();
byte[] sign = sm2.sign(req.getContent().getBytes(), req.getPassword().getBytes());
// SM2 sm2 = SmUtil.sm2(req.getPassword().getBytes());
// String decryptContent = sm4.decryptStr(req.getContent());
*//* SM4Utils sm4Utils = new SM4Utils();
sm4Utils.setHexString(false);
sm4Utils.setSecretKey(req.getPassword());
log.info("password:{},secretKey:{},keybytes:{}",req.getPassword(),sm4Utils.getSecretKey(),sm4Utils.getSecretKey().getBytes());
String decryptContent = sm4Utils.decryptData_ECB(req.getContent());*//*
MessageReq messageReq = new MessageReq();
messageReq.setKey(req.getKey());
messageReq.setDecryptContent(decryptContent);
return messageReq;*/
}
return
req
;
}
...
...
@@ -101,14 +81,94 @@ public class MessageServiceImpl implements MessageService {
messageReq
.
setDecryptContent
(
decryptContent
);
return
messageReq
;
}
else
if
(
"SM4"
.
equalsIgnoreCase
(
req
.
getAlgorithm
()))
{
//加密数据 并请求 再解密数据
JSONObject
jsonObject
=
JSON
.
parseObject
(
req
.
getContent
());
JSONObject
bizJson
=
jsonObject
.
getJSONObject
(
"biz_content"
);
String
access_key
=
jsonObject
.
getString
(
"access_key"
);
String
format
=
jsonObject
.
getString
(
"format"
);
String
request_id
=
jsonObject
.
getString
(
"request_id"
);
String
timestamp
=
jsonObject
.
getString
(
"timestamp"
);
String
version
=
jsonObject
.
getString
(
"version"
);
JSONObject
dataJson
=
bizJson
.
getJSONObject
(
"data"
);
log
.
info
(
"未加密的数据:{}"
,
dataJson
.
toJSONString
());
//针对datajson加密
String
encryptContent
=
null
;
try
{
encryptContent
=
encryptSM4
(
req
.
getPassword
(),
dataJson
.
toJSONString
());
log
.
info
(
"加密后的数据:{}"
,
encryptContent
);
bizJson
.
put
(
"data"
,
encryptContent
);
}
catch
(
Exception
e
)
{
throw
new
AppException
(
e
+
"数据sm4加密错误"
);
}
// 封装数据
MultiValueMap
<
String
,
String
>
params
=
new
LinkedMultiValueMap
<>();
params
.
set
(
"access_key"
,
access_key
);
params
.
add
(
"biz_content"
,
bizJson
.
toJSONString
());
params
.
add
(
"format"
,
format
);
params
.
add
(
"request_id"
,
request_id
);
params
.
add
(
"timestamp"
,
timestamp
);
params
.
add
(
"version"
,
version
);
// 获取签名字符串
List
<
String
>
signSourceParams
=
new
ArrayList
<>();
params
.
forEach
((
key
,
values
)
->
{
if
(
values
!=
null
&&
values
.
size
()
>
0
)
{
Object
value
=
values
.
get
(
0
);
if
(
value
!=
null
)
{
String
valueStr
=
value
.
toString
();
if
(!
valueStr
.
isEmpty
())
{
signSourceParams
.
add
(
String
.
format
(
"%s=%s"
,
key
,
valueStr
));
}
}
}
});
Collections
.
sort
(
signSourceParams
);
String
sign
=
null
;
try
{
String
paramStr
=
String
.
join
(
"&"
,
signSourceParams
);
// String paramStr = "access_key=DYZWFWZX&biz_content={\"aaz001\":\"1701142468806\",\"appid\":\"uRsNCSjIRWaN\",\"data\":\"{\\\"aaz001\\\":\\\"1701049617366\\\",\\\"data\\\":\\\"K7JVkCiKcUyv3VcKx8WZXvY3E+P2pXMFE3QDkhiVhJDgmJWN+NMwoQhoVjBSmO9fWdO+4kG6iYYc8JEefgYn4g==\\\",\\\"serviceCode\\\":\\\"00500100101\\\",\\\"appid\\\":\\\"uRsNCSjIRWaN\\\"}\"}&format=json&request_id=2be36a654da24cc8bb20×tamp=1701049618303&version=1.0";
log
.
info
(
"paramStr==>"
+
paramStr
);
byte
[]
paramStrBytes
=
paramStr
.
getBytes
(
StandardCharsets
.
UTF_8
);
byte
[]
signBytes
=
null
;
signBytes
=
ServicePlatformInvoker
.
SignUtils
.
sm2Sign
(
req
.
getPrivate_key
(),
paramStrBytes
);
sign
=
Base64
.
getEncoder
().
encodeToString
(
signBytes
);
}
catch
(
Exception
e
)
{
log
.
error
(
"异常"
,
e
);
throw
new
AppException
(
e
.
getMessage
()
+
",签名错误"
);
}
params
.
add
(
"sign"
,
sign
);
// 发送POST请求, 并获取响应
HttpHeaders
headers
=
new
HttpHeaders
();
String
responseContent
;
try
{
headers
.
set
(
"Content-Type"
,
MediaType
.
APPLICATION_FORM_URLENCODED_VALUE
);
ResponseEntity
<
String
>
responseEntity
=
restTemplate
.
exchange
(
req
.
getUrl
(),
HttpMethod
.
POST
,
new
HttpEntity
<>(
params
,
headers
),
String
.
class
);
responseContent
=
responseEntity
.
getBody
();
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"网络错误: "
+
e
.
getMessage
());
}
JSONObject
respJson
=
JSON
.
parseObject
(
responseContent
);
String
code
=
respJson
.
getString
(
"code"
);
if
(!
"1"
.
equals
(
code
))
{
messageReq
.
setKey
(
req
.
getKey
());
messageReq
.
setDecryptContent
(
respJson
.
toJSONString
());
return
messageReq
;
}
JSONObject
biz_data
=
respJson
.
getJSONObject
(
"biz_data"
);
String
data
=
biz_data
.
getString
(
"data"
);
log
.
info
(
"data==>{}"
,
data
);
String
decryptContent
=
null
;
try
{
decryptContent
=
encryptSM4
(
req
.
getPassword
(),
req
.
getContent
());
decryptContent
=
decryptSM4
(
req
.
getPassword
(),
data
);
JSONObject
jsonObject1
=
JSONObject
.
parseObject
(
decryptContent
);
biz_data
.
put
(
"data"
,
jsonObject1
);
respJson
.
put
(
"biz_data"
,
biz_data
);
}
catch
(
Exception
e
)
{
throw
new
AppException
(
"加密异常"
+
e
.
getMessage
()
);
log
.
error
(
"异常"
,
e
);
}
messageReq
.
setKey
(
req
.
getKey
());
messageReq
.
setDecryptContent
(
decryptContent
);
messageReq
.
setDecryptContent
(
respJson
.
toJSONString
()
);
return
messageReq
;
}
return
messageReq
;
...
...
@@ -119,7 +179,7 @@ public class MessageServiceImpl implements MessageService {
if
(
content
==
null
)
{
return
null
;
}
Cipher
cipher
=
Cipher
.
getInstance
(
"SM4/ECB/PKCS7Padding"
,
new
BouncyCastleProvider
()
);
Cipher
cipher
=
SecurityUtils
.
getSM4Cipher
(
key
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
new
SecretKeySpec
(
key
.
getBytes
(
StandardCharsets
.
UTF_8
),
"SM4"
),
(
AlgorithmParameters
)
null
);
return
Base64
.
getEncoder
().
encodeToString
(
cipher
.
doFinal
(
content
.
getBytes
(
StandardCharsets
.
UTF_8
)));
}
...
...
@@ -135,6 +195,18 @@ public class MessageServiceImpl implements MessageService {
return
new
String
(
cipher
.
doFinal
(
Base64
.
getDecoder
().
decode
(
encryptContent
)),
StandardCharsets
.
UTF_8
);
}
public
String
decryptSM4
(
@NonNull
String
key
,
String
encryptContent
)
throws
Exception
{
Assert
.
hasText
(
key
,
"密钥不能为空"
);
if
(
encryptContent
==
null
)
{
return
null
;
}
Cipher
cipher
=
null
;
cipher
=
Cipher
.
getInstance
(
"SM4/ECB/PKCS7Padding"
,
SecurityUtils
.
geBouncyCastleProvider
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
new
SecretKeySpec
(
key
.
getBytes
(
StandardCharsets
.
UTF_8
),
"SM4"
),
(
AlgorithmParameters
)
null
);
return
new
String
(
cipher
.
doFinal
(
Base64
.
getDecoder
().
decode
(
encryptContent
)),
StandardCharsets
.
UTF_8
);
}
@Override
public
MessageResp
signMessage
(
MessageReq
req
)
{
...
...
base-manager/src/main/java/com/mortals/xhx/busiz/web/ElasticsearchTestsController.java
0 → 100644
View file @
5b8aa538
//package com.mortals.xhx.busiz.web;
//
//import com.alibaba.fastjson.JSONObject;
//import lombok.extern.slf4j.Slf4j;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
//import org.elasticsearch.search.sort.SortBuilders;
//import org.elasticsearch.search.sort.SortOrder;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.io.IOException;
//import java.util.LinkedList;
//
//@RestController
//@RequestMapping("elTest")
//@Slf4j
//public class ElasticsearchTestsController {
//
// @Qualifier("client")
// @Autowired
// private RestHighLevelClient restHighLevelClient;
//
// //判断某id的文档(数据库中的行)是否存在
// public void testExist(){
//
// }
//
// //一次保存一条数据
// public void testInsert() {
// //把id为241的DiscussPost的对象保存到discusspost索引(es的索引相当于数据库的表)
// }
//
//
// //不带高亮的查询
// public void noHighlightQuery() throws IOException {
// SearchRequest searchRequest = new SearchRequest("discusspost");//discusspost是索引名,就是表名
//
// //构建搜索条件
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
// //在discusspost索引的title和content字段中都查询“互联网寒冬”
// .query(QueryBuilders.multiMatchQuery("互联网寒冬", "title", "content"))
// // matchQuery是模糊查询,会对key进行分词:searchSourceBuilder.query(QueryBuilders.matchQuery(key,value));
// // termQuery是精准查询:searchSourceBuilder.query(QueryBuilders.termQuery(key,value));
// .sort(SortBuilders.fieldSort("type").order(SortOrder.DESC))
// .sort(SortBuilders.fieldSort("score").order(SortOrder.DESC))
// .sort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
// //一个可选项,用于控制允许搜索的时间:searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// .from(0)// 指定从哪条开始查询
// .size(10);// 需要查出的总记录条数
//
// searchRequest.source(searchSourceBuilder);
// SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//
// System.out.println(JSONObject.toJSON(searchResponse));
//
///* List<DiscussPost> list = new LinkedList<>();
// for (SearchHit hit : searchResponse.getHits().getHits()) {
// DiscussPost discussPost = JSONObject.parseObject(hit.getSourceAsString(), DiscussPost.class);
// System.out.println(discussPost);
// list.add(discussPost);
// }*/
// }
//
//
// //带高亮的查询
// public void highlightQuery() throws Exception{
// SearchRequest searchRequest = new SearchRequest("discusspost");//discusspost是索引名,就是表名
//
// //高亮
// HighlightBuilder highlightBuilder = new HighlightBuilder();
// highlightBuilder.field("title");
// highlightBuilder.field("content");
// highlightBuilder.requireFieldMatch(false);
// highlightBuilder.preTags("<span style='color:red'>");
// highlightBuilder.postTags("</span>");
//
// //构建搜索条件
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
// .query(QueryBuilders.multiMatchQuery("互联网寒冬", "title", "content"))
// .sort(SortBuilders.fieldSort("type").order(SortOrder.DESC))
// .sort(SortBuilders.fieldSort("score").order(SortOrder.DESC))
// .sort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
// .from(0)// 指定从哪条开始查询
// .size(10)// 需要查出的总记录条数
// .highlighter(highlightBuilder);//高亮
//
// searchRequest.source(searchSourceBuilder);
// SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
///*
// List<DiscussPost> list = new LinkedList<>();
// for (SearchHit hit : searchResponse.getHits().getHits()) {
// DiscussPost discussPost = JSONObject.parseObject(hit.getSourceAsString(), DiscussPost.class);
//
// // 处理高亮显示的结果
// HighlightField titleField = hit.getHighlightFields().get("title");
// if (titleField != null) {
// discussPost.setTitle(titleField.getFragments()[0].toString());
// }
// HighlightField contentField = hit.getHighlightFields().get("content");
// if (contentField != null) {
// discussPost.setContent(contentField.getFragments()[0].toString());
// }
// System.out.println(discussPost);
// list.add(discussPost);
// }*/
// }
//
//
//}
base-manager/src/main/java/com/mortals/xhx/common/utils/SecurityUtils.java
0 → 100644
View file @
5b8aa538
package
com.mortals.xhx.common.utils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.bouncycastle.jce.provider.BouncyCastleProvider
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
javax.crypto.Cipher
;
import
javax.crypto.NoSuchPaddingException
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.nio.charset.StandardCharsets
;
import
java.security.AlgorithmParameters
;
import
java.security.NoSuchAlgorithmException
;
/**
* 安全服务工具类
*
* @author zxfei
*/
@Slf4j
public
class
SecurityUtils
{
private
static
Cipher
SM4Cipher
=
null
;
private
static
Cipher
SM4DecryptCipher
;
static
{
try
{
SM4DecryptCipher
=
Cipher
.
getInstance
(
"SM4/ECB/PKCS7Padding"
,
new
BouncyCastleProvider
());
}
catch
(
NoSuchAlgorithmException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
NoSuchPaddingException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
private
static
BouncyCastleProvider
bouncyCastleProvider
=
new
BouncyCastleProvider
();
public
static
BouncyCastleProvider
geBouncyCastleProvider
()
{
return
bouncyCastleProvider
;
}
public
synchronized
static
Cipher
getSM4Cipher
(
String
key
)
{
try
{
if
(
SM4Cipher
==
null
)
{
//创建实例之前可能会有一些准备性的耗时工作
Thread
.
sleep
(
300
);
synchronized
(
SecurityUtils
.
class
)
{
if
(
SM4Cipher
==
null
)
{
//二次检查
SM4Cipher
=
Cipher
.
getInstance
(
"SM4/ECB/PKCS7Padding"
,
new
BouncyCastleProvider
());
}
}
}
}
catch
(
Exception
e
)
{
log
.
error
(
"SM4Cipher init error"
,
e
);
}
return
SM4Cipher
;
}
public
synchronized
static
Cipher
getSM4DecryptCipher
(
String
key
)
{
try
{
if
(
SM4DecryptCipher
==
null
)
{
//创建实例之前可能会有一些准备性的耗时工作
Thread
.
sleep
(
300
);
synchronized
(
SecurityUtils
.
class
)
{
if
(
SM4DecryptCipher
==
null
)
{
//二次检查
//SM4DecryptCipher = Cipher.getInstance("SM4/ECB/PKCS7Padding", new BouncyCastleProvider());
SM4DecryptCipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
new
SecretKeySpec
(
key
.
getBytes
(
StandardCharsets
.
UTF_8
),
"SM4"
),
(
AlgorithmParameters
)
null
);
}
}
}
SM4DecryptCipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
new
SecretKeySpec
(
key
.
getBytes
(
StandardCharsets
.
UTF_8
),
"SM4"
),
(
AlgorithmParameters
)
null
);
}
catch
(
Exception
e
)
{
log
.
error
(
"SM4Cipher init error"
,
e
);
}
return
SM4DecryptCipher
;
}
/**
* 获取Authentication
*/
//public static Authentication getAuthentication() {
// return SecurityContextHolder.getContext().getAuthentication();
// }
/**
* 生成BCryptPasswordEncoder密码
*
* @param password 密码
* @return 加密字符串
*/
public
static
String
encryptPassword
(
String
password
)
{
BCryptPasswordEncoder
passwordEncoder
=
new
BCryptPasswordEncoder
();
return
passwordEncoder
.
encode
(
password
);
}
/**
* 判断密码是否相同
*
* @param rawPassword 真实密码
* @param encodedPassword 加密后字符
* @return 结果
*/
public
static
boolean
matchesPassword
(
String
rawPassword
,
String
encodedPassword
)
{
BCryptPasswordEncoder
passwordEncoder
=
new
BCryptPasswordEncoder
();
return
passwordEncoder
.
matches
(
rawPassword
,
encodedPassword
);
}
/**
* 是否为管理员
*
* @param userId 用户ID
* @return 结果
*/
public
static
boolean
isAdmin
(
Long
userId
)
{
return
userId
!=
null
&&
1L
==
userId
;
}
}
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