|
@@ -38,54 +38,48 @@ class SensitiveMiddleware implements MiddlewareInterface
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
{
|
|
|
$uri = $request->getUri();
|
|
|
- // 获取接口路径(不包含查询参数)
|
|
|
$path = $uri->getPath();
|
|
|
+
|
|
|
+ if (!$path || !in_array($path, self::STREAM_URL)) {
|
|
|
+ return $handler->handle($request);
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
- if($path && in_array($path,self::STREAM_URL)){
|
|
|
- $badWords = $this->redis->sMembers('black_word'); //黑名单
|
|
|
- $whiteWords = $this->redis->sMembers('white_word');//白名单
|
|
|
- // 获取所有请求参数并拼接成文本
|
|
|
- $params = $this->request->all();
|
|
|
- $concatenated = "";
|
|
|
- if($params){
|
|
|
- foreach ($params as $value) {
|
|
|
- if (is_array($value)) {
|
|
|
- // 如果值是数组,将数组元素用逗号连接
|
|
|
- $concatenated.= json_encode($value);
|
|
|
- } else {
|
|
|
- // 如果不是数组,直接拼接
|
|
|
- $concatenated.= $value;
|
|
|
- }
|
|
|
+ $badWords = $this->redis->sMembers('black_word');
|
|
|
+ $whiteWords = $this->redis->sMembers('white_word');
|
|
|
+ $params = $this->request->all();
|
|
|
+ $concatenated = "";
|
|
|
+
|
|
|
+ if ($params) {
|
|
|
+ foreach ($params as $value) {
|
|
|
+ if (is_array($value)) {
|
|
|
+ $concatenated .= json_encode($value);
|
|
|
+ } else {
|
|
|
+ $concatenated .= (string)$value;
|
|
|
}
|
|
|
}
|
|
|
- // 遍历违禁词,检查是否在文本中存在
|
|
|
- foreach ($badWords as $badWord) {
|
|
|
- // 判断该违禁词是否在白名单中,如果在则跳过
|
|
|
- if (in_array($badWord, $whiteWords)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (str_contains($concatenated, $badWord)) {
|
|
|
- // 处理找到违禁词的情况,例如返回错误信息
|
|
|
- $message = '发现违禁词: '. $badWord;
|
|
|
- return $this->response->json(
|
|
|
- [
|
|
|
- 'code' => 0,
|
|
|
- 'data' => [],
|
|
|
- 'message' => $message
|
|
|
- ]
|
|
|
- );
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($badWords as $badWord) {
|
|
|
+ if (in_array($badWord, $whiteWords)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (str_contains($concatenated, $badWord)) {
|
|
|
+ return $this->response->json([
|
|
|
+ 'code' => 0,
|
|
|
+ 'data' => [],
|
|
|
+ 'message' => '发现违禁词: ' . $badWord
|
|
|
+ ]);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return $handler->handle($request);
|
|
|
} catch (\Exception $e) {
|
|
|
- return $this->response->json(
|
|
|
- [
|
|
|
- 'code' => -1,
|
|
|
- 'data' => [],
|
|
|
- 'message' => '系统错误:' . $e->getMessage()
|
|
|
- ]
|
|
|
- );
|
|
|
+ return $this->response->json([
|
|
|
+ 'code' => -1,
|
|
|
+ 'data' => [],
|
|
|
+ 'message' => '系统错误:' . $e->getMessage()
|
|
|
+ ]);
|
|
|
}
|
|
|
}
|
|
|
}
|