Эх сурвалжийг харах

电商系统服务端环境搭建与基础配置

wangqi49 2 өдөр өмнө
parent
commit
c8863e98ee
33 өөрчлөгдсөн 609 нэмэгдсэн , 31 устгасан
  1. 1 0
      pom.xml
  2. 25 0
      resources/nacos-yaml/webchat-client-mall-service-dev.yaml
  3. 2 2
      webchat-client-chat/src/main/java/com/webchat/client/mall/config/ClientWebMvcConfig.java
  4. 1 1
      webchat-client-chat/src/main/java/com/webchat/client/mall/config/interceptor/SafeClickInterceptor.java
  5. 3 3
      webchat-client-chat/src/main/java/com/webchat/client/mall/controller/AccountController.java
  6. 2 2
      webchat-client-chat/src/main/java/com/webchat/client/mall/controller/AccountRelationController.java
  7. 2 2
      webchat-client-chat/src/main/java/com/webchat/client/mall/controller/ArticleController.java
  8. 2 2
      webchat-client-chat/src/main/java/com/webchat/client/mall/controller/ChatMessageController.java
  9. 1 1
      webchat-client-chat/src/main/java/com/webchat/client/mall/controller/FileController.java
  10. 2 2
      webchat-client-chat/src/main/java/com/webchat/client/mall/controller/MomentController.java
  11. 2 3
      webchat-client-chat/src/main/java/com/webchat/client/mall/controller/RedPacketController.java
  12. 2 2
      webchat-client-chat/src/main/java/com/webchat/client/mall/filter/OauthFilter.java
  13. 1 2
      webchat-client-chat/src/main/java/com/webchat/client/mall/service/AccountRelationService.java
  14. 1 2
      webchat-client-chat/src/main/java/com/webchat/client/mall/service/AccountService.java
  15. 1 1
      webchat-client-chat/src/main/java/com/webchat/client/mall/service/ArticleService.java
  16. 1 3
      webchat-client-chat/src/main/java/com/webchat/client/mall/service/ChatMessageService.java
  17. 1 1
      webchat-client-chat/src/main/java/com/webchat/client/mall/service/MomentService.java
  18. 1 1
      webchat-client-chat/src/main/java/com/webchat/client/mall/service/RedPacketService.java
  19. 1 1
      webchat-client-chat/src/main/java/com/webchat/client/mall/service/UserSessionService.java
  20. 19 0
      webchat-client-mall/.mvn/wrapper/maven-wrapper.properties
  21. 51 0
      webchat-client-mall/pom.xml
  22. 19 0
      webchat-client-mall/src/main/java/com/webchat/client/WebchatMallApplication.java
  23. 64 0
      webchat-client-mall/src/main/java/com/webchat/client/advice/GlobalExceptionAdvice.java
  24. 27 0
      webchat-client-mall/src/main/java/com/webchat/client/mall/config/MallWebMvcConfig.java
  25. 69 0
      webchat-client-mall/src/main/java/com/webchat/client/mall/config/interceptor/SafeClickInterceptor.java
  26. 56 0
      webchat-client-mall/src/main/java/com/webchat/client/mall/controller/AccountController.java
  27. 114 0
      webchat-client-mall/src/main/java/com/webchat/client/mall/filter/OauthFilter.java
  28. 45 0
      webchat-client-mall/src/main/java/com/webchat/client/mall/service/AccountService.java
  29. 69 0
      webchat-client-mall/src/main/java/com/webchat/client/mall/service/UserSessionService.java
  30. 1 0
      webchat-client-mall/src/main/resources/application.yml
  31. 21 0
      webchat-client-mall/src/main/resources/bootstrap.yml
  32. 2 0
      webchat-common/src/main/java/com/webchat/common/constants/CookieConstants.java
  33. 0 0
      webchat-gateway/src/main/resources/application.yml

+ 1 - 0
pom.xml

@@ -32,6 +32,7 @@
         <module>webchat-search</module>
         <module>webchat-act</module>
         <module>webchat-api</module>
+        <module>webchat-client-mall</module>
     </modules>
 
     <properties>

+ 25 - 0
resources/nacos-yaml/webchat-client-mall-service-dev.yaml

@@ -0,0 +1,25 @@
+spring:
+  servlet:
+    multipart:
+      max-file-size: 10MB
+      max-request-size: 10MB
+  data:
+    redis:
+      host: 127.0.0.1
+      port: 6379
+      database: 6
+      jedis:
+        timeout: 5000
+        pool:
+          max-active: 100
+          max-wait: -1
+          min-idle: 10
+
+minio:
+  endpoint: http://127.0.0.1:9000
+  accessKey: BArEoI6PE8uGh13jHRuN
+  secretKey: eHRL9vpXUH6vGOGkL5Vth2ZQRpzh7AvdKzNlASko
+  bucketName: webchat
+
+oauth:
+  server-url: http://localhost:5000/?call_back=%s

+ 2 - 2
webchat-client-chat/src/main/java/com/webchat/client/chat/config/ClientWebMvcConfig.java → webchat-client-chat/src/main/java/com/webchat/client/mall/config/ClientWebMvcConfig.java

@@ -1,6 +1,6 @@
-package com.webchat.client.chat.config;
+package com.webchat.client.mall.config;
 
-import com.webchat.client.chat.config.interceptor.SafeClickInterceptor;
+import com.webchat.client.mall.config.interceptor.SafeClickInterceptor;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

+ 1 - 1
webchat-client-chat/src/main/java/com/webchat/client/chat/config/interceptor/SafeClickInterceptor.java → webchat-client-chat/src/main/java/com/webchat/client/mall/config/interceptor/SafeClickInterceptor.java

@@ -1,4 +1,4 @@
-package com.webchat.client.chat.config.interceptor;
+package com.webchat.client.mall.config.interceptor;
 
 import com.webchat.common.config.annotation.SafeClick;
 import com.webchat.common.constants.WebConstant;

+ 3 - 3
webchat-client-chat/src/main/java/com/webchat/client/chat/controller/AccountController.java → webchat-client-chat/src/main/java/com/webchat/client/mall/controller/AccountController.java

@@ -1,8 +1,8 @@
-package com.webchat.client.chat.controller;
+package com.webchat.client.mall.controller;
 
 
-import com.webchat.client.chat.service.AccountService;
-import com.webchat.client.chat.service.UserSessionService;
+import com.webchat.client.mall.service.AccountService;
+import com.webchat.client.mall.service.UserSessionService;
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;
 import com.webchat.common.helper.SessionHelper;

+ 2 - 2
webchat-client-chat/src/main/java/com/webchat/client/chat/controller/AccountRelationController.java → webchat-client-chat/src/main/java/com/webchat/client/mall/controller/AccountRelationController.java

@@ -1,7 +1,7 @@
-package com.webchat.client.chat.controller;
+package com.webchat.client.mall.controller;
 
 
-import com.webchat.client.chat.service.AccountRelationService;
+import com.webchat.client.mall.service.AccountRelationService;
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;
 import com.webchat.common.helper.SessionHelper;

+ 2 - 2
webchat-client-chat/src/main/java/com/webchat/client/chat/controller/ArticleController.java → webchat-client-chat/src/main/java/com/webchat/client/mall/controller/ArticleController.java

@@ -1,7 +1,7 @@
-package com.webchat.client.chat.controller;
+package com.webchat.client.mall.controller;
 
 
-import com.webchat.client.chat.service.ArticleService;
+import com.webchat.client.mall.service.ArticleService;
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;
 import com.webchat.common.helper.SessionHelper;

+ 2 - 2
webchat-client-chat/src/main/java/com/webchat/client/chat/controller/ChatMessageController.java → webchat-client-chat/src/main/java/com/webchat/client/mall/controller/ChatMessageController.java

@@ -1,7 +1,7 @@
-package com.webchat.client.chat.controller;
+package com.webchat.client.mall.controller;
 
 
-import com.webchat.client.chat.service.ChatMessageService;
+import com.webchat.client.mall.service.ChatMessageService;
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;
 import com.webchat.common.helper.SessionHelper;

+ 1 - 1
webchat-client-chat/src/main/java/com/webchat/client/chat/controller/FileController.java → webchat-client-chat/src/main/java/com/webchat/client/mall/controller/FileController.java

@@ -1,4 +1,4 @@
-package com.webchat.client.chat.controller;
+package com.webchat.client.mall.controller;
 
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;

+ 2 - 2
webchat-client-chat/src/main/java/com/webchat/client/chat/controller/MomentController.java → webchat-client-chat/src/main/java/com/webchat/client/mall/controller/MomentController.java

@@ -1,7 +1,7 @@
-package com.webchat.client.chat.controller;
+package com.webchat.client.mall.controller;
 
 
-import com.webchat.client.chat.service.MomentService;
+import com.webchat.client.mall.service.MomentService;
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;
 import com.webchat.common.config.annotation.SafeClick;

+ 2 - 3
webchat-client-chat/src/main/java/com/webchat/client/chat/controller/RedPacketController.java → webchat-client-chat/src/main/java/com/webchat/client/mall/controller/RedPacketController.java

@@ -1,11 +1,10 @@
-package com.webchat.client.chat.controller;
+package com.webchat.client.mall.controller;
 
 
-import com.webchat.client.chat.service.RedPacketService;
+import com.webchat.client.mall.service.RedPacketService;
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;
 import com.webchat.common.config.annotation.SafeClick;
-import com.webchat.common.config.annotation.ValidateLogin;
 import com.webchat.common.enums.ClickEvent;
 import com.webchat.common.helper.SessionHelper;
 import com.webchat.domain.vo.request.SendRedPacketRequestVO;

+ 2 - 2
webchat-client-chat/src/main/java/com/webchat/client/chat/filter/OauthFilter.java → webchat-client-chat/src/main/java/com/webchat/client/mall/filter/OauthFilter.java

@@ -1,6 +1,6 @@
-package com.webchat.client.chat.filter;
+package com.webchat.client.mall.filter;
 
-import com.webchat.client.chat.service.UserSessionService;
+import com.webchat.client.mall.service.UserSessionService;
 import com.webchat.common.config.properties.OauthProperties;
 import com.webchat.common.enums.BaseErrCodeEnum;
 import com.webchat.common.helper.SessionHelper;

+ 1 - 2
webchat-client-chat/src/main/java/com/webchat/client/chat/service/AccountRelationService.java → webchat-client-chat/src/main/java/com/webchat/client/mall/service/AccountRelationService.java

@@ -1,4 +1,4 @@
-package com.webchat.client.chat.service;
+package com.webchat.client.mall.service;
 
 
 import com.webchat.common.bean.APIResponseBean;
@@ -11,7 +11,6 @@ import com.webchat.rmi.user.AccountRelationClient;
 import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.web.bind.annotation.RequestBody;
 
 import java.util.List;
 

+ 1 - 2
webchat-client-chat/src/main/java/com/webchat/client/chat/service/AccountService.java → webchat-client-chat/src/main/java/com/webchat/client/mall/service/AccountService.java

@@ -1,8 +1,7 @@
-package com.webchat.client.chat.service;
+package com.webchat.client.mall.service;
 
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;
-import com.webchat.domain.vo.response.AccountRelationResponseVO;
 import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
 import com.webchat.rmi.user.UserServiceClient;
 import org.springframework.beans.factory.annotation.Autowired;

+ 1 - 1
webchat-client-chat/src/main/java/com/webchat/client/chat/service/ArticleService.java → webchat-client-chat/src/main/java/com/webchat/client/mall/service/ArticleService.java

@@ -1,4 +1,4 @@
-package com.webchat.client.chat.service;
+package com.webchat.client.mall.service;
 
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;

+ 1 - 3
webchat-client-chat/src/main/java/com/webchat/client/chat/service/ChatMessageService.java → webchat-client-chat/src/main/java/com/webchat/client/mall/service/ChatMessageService.java

@@ -1,4 +1,4 @@
-package com.webchat.client.chat.service;
+package com.webchat.client.mall.service;
 
 
 import com.webchat.common.bean.APIResponseBean;
@@ -11,8 +11,6 @@ import com.webchat.rmi.ugc.ChatMessageClient;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestParam;
 
 import java.util.Collections;
 import java.util.List;

+ 1 - 1
webchat-client-chat/src/main/java/com/webchat/client/chat/service/MomentService.java → webchat-client-chat/src/main/java/com/webchat/client/mall/service/MomentService.java

@@ -1,4 +1,4 @@
-package com.webchat.client.chat.service;
+package com.webchat.client.mall.service;
 
 import com.webchat.common.bean.APIResponseBean;
 import com.webchat.common.bean.APIResponseBeanUtil;

+ 1 - 1
webchat-client-chat/src/main/java/com/webchat/client/chat/service/RedPacketService.java → webchat-client-chat/src/main/java/com/webchat/client/mall/service/RedPacketService.java

@@ -1,4 +1,4 @@
-package com.webchat.client.chat.service;
+package com.webchat.client.mall.service;
 
 
 import com.webchat.common.bean.APIResponseBean;

+ 1 - 1
webchat-client-chat/src/main/java/com/webchat/client/chat/service/UserSessionService.java → webchat-client-chat/src/main/java/com/webchat/client/mall/service/UserSessionService.java

@@ -1,4 +1,4 @@
-package com.webchat.client.chat.service;
+package com.webchat.client.mall.service;
 
 import com.webchat.common.constants.CookieConstants;
 import com.webchat.common.enums.RedisKeyEnum;

+ 19 - 0
webchat-client-mall/.mvn/wrapper/maven-wrapper.properties

@@ -0,0 +1,19 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+wrapperVersion=3.3.2
+distributionType=only-script
+distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip

+ 51 - 0
webchat-client-mall/pom.xml

@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.webchat</groupId>
+        <artifactId>webchat</artifactId>
+        <version>0.0.1-SNAPSHOT</version>
+    </parent>
+
+    <groupId>com.webchat</groupId>
+    <artifactId>webchat-client-mall</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>webchat-client-mall</name>
+    <description>webchat-client-mall</description>
+
+    <properties>
+        <java.version>17</java.version>
+    </properties>
+    <dependencies>
+
+        <dependency>
+            <groupId>com.webchat</groupId>
+            <artifactId>webchat-remote</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <finalName>webchat-client-mall</finalName>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 19 - 0
webchat-client-mall/src/main/java/com/webchat/client/WebchatMallApplication.java

@@ -0,0 +1,19 @@
+package com.webchat.client;
+
+import com.webchat.rmi.annotation.EnableRpcPermissionInterceptor;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.annotation.ComponentScan;
+
+@SpringBootApplication
+@ComponentScan("com.webchat")
+@EnableFeignClients("com.webchat.rmi")
+@EnableRpcPermissionInterceptor
+public class WebchatMallApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(WebchatMallApplication.class, args);
+    }
+
+}

+ 64 - 0
webchat-client-mall/src/main/java/com/webchat/client/advice/GlobalExceptionAdvice.java

@@ -0,0 +1,64 @@
+package com.webchat.client.advice;
+
+import com.webchat.common.bean.APIResponseBean;
+import com.webchat.common.bean.APIResponseBeanUtil;
+import com.webchat.common.enums.APIErrorCommonEnum;
+import com.webchat.common.exception.AuthException;
+import com.webchat.common.exception.BusinessException;
+import com.webchat.common.exception.NotFoundException;
+import jakarta.servlet.http.HttpServletRequest;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.converter.HttpMessageConversionException;
+import org.springframework.web.HttpRequestMethodNotSupportedException;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+
+/***
+ * 统一异常处理器
+ * @Author 程序员七七 ==> http://www.coderutil.com网站作者
+ */
+@Slf4j
+@ControllerAdvice
+public class GlobalExceptionAdvice {
+
+    @ExceptionHandler(Exception.class)
+    @ResponseBody
+    public ResponseEntity<APIResponseBean> exceptionHandler(HttpServletRequest request, Exception e) {
+        log.error("异常详情信息:", e);
+        if (e instanceof AuthException) {
+            APIResponseBean apiResponseBean = APIResponseBeanUtil.error(((AuthException) e).getCode(), e.getMessage());
+            return new ResponseEntity(apiResponseBean, HttpStatus.OK);
+        }
+        if (e instanceof IllegalArgumentException) {
+            APIResponseBean apiResponseBean = APIResponseBeanUtil.error(500, e.getMessage());
+            return new ResponseEntity(apiResponseBean, HttpStatus.OK);
+        } else if (e instanceof BusinessException) {
+            APIResponseBean apiResponseBean = APIResponseBeanUtil.error(((BusinessException) e).getCode(), e.getMessage());
+            return new ResponseEntity(apiResponseBean, HttpStatus.OK);
+        } else if (e instanceof NotFoundException) {
+            APIResponseBean apiResponseBean = APIResponseBeanUtil.error(((NotFoundException) e).getCode(), e.getMessage());
+            return new ResponseEntity(apiResponseBean, HttpStatus.OK);
+        } else if (e instanceof HttpRequestMethodNotSupportedException) {
+            APIResponseBean apiResponseBean = APIResponseBeanUtil.error(500, e.getMessage());
+            return new ResponseEntity(apiResponseBean, HttpStatus.OK);
+        } else if (e instanceof MethodArgumentNotValidException) {
+            /***
+             * 处理@Validated参数校验失败异常
+             */
+            APIResponseBean apiResponseBean = APIResponseBeanUtil.error(APIErrorCommonEnum.VALID_EXCEPTION.getCode(),
+                    "参数校验异常");
+            return new ResponseEntity(apiResponseBean, HttpStatus.OK);
+        } else if (e instanceof HttpMessageConversionException) {
+            APIResponseBean apiResponseBean = APIResponseBeanUtil.error(APIErrorCommonEnum.VALID_EXCEPTION.getCode(),
+                    "参数转换异常");
+            return new ResponseEntity(apiResponseBean, HttpStatus.OK);
+        }
+        APIResponseBean apiResponseBean = APIResponseBeanUtil.error(500, "服务端繁忙");
+        return new ResponseEntity(apiResponseBean, HttpStatus.INTERNAL_SERVER_ERROR);
+    }
+}

+ 27 - 0
webchat-client-mall/src/main/java/com/webchat/client/mall/config/MallWebMvcConfig.java

@@ -0,0 +1,27 @@
+package com.webchat.client.mall.config;
+
+import com.webchat.client.mall.config.interceptor.SafeClickInterceptor;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+@Configuration
+public class MallWebMvcConfig implements WebMvcConfigurer {
+
+    private static final String CLINT_REQUEST_PATH_PREFIX = "/mall-service/";
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+
+        /**
+         * 注册安全点击拦截器
+         */
+        registry.addInterceptor(safeClickInterceptor()).addPathPatterns(CLINT_REQUEST_PATH_PREFIX);
+    }
+
+    @Bean
+    public SafeClickInterceptor safeClickInterceptor() {
+        return new SafeClickInterceptor();
+    }
+}

+ 69 - 0
webchat-client-mall/src/main/java/com/webchat/client/mall/config/interceptor/SafeClickInterceptor.java

@@ -0,0 +1,69 @@
+package com.webchat.client.mall.config.interceptor;
+
+import com.webchat.common.config.annotation.SafeClick;
+import com.webchat.common.constants.WebConstant;
+import com.webchat.common.exception.AuthException;
+import com.webchat.common.exception.BusinessException;
+import com.webchat.common.helper.SessionHelper;
+import com.webchat.common.service.RedisService;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.method.HandlerMethod;
+import org.springframework.web.servlet.HandlerInterceptor;
+
+import java.lang.reflect.Method;
+
+/**
+ * 安全点击自定义注解实现,拦截器实现
+ *
+ * 实现原理:基于redis分布式实现限制当前登录用于在一定时间内只能触发一次特定事件,超过限制则报错提醒
+ */
+@Slf4j
+public class SafeClickInterceptor implements HandlerInterceptor {
+
+
+    @Autowired
+    private RedisService redisService;
+
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
+
+        // 检查 handler 是否为 HandlerMethod 类型
+        if (!(handler instanceof HandlerMethod)) {
+            // 不是 HandlerMethod 类型,可能是静态资源请求,直接放行
+            return true;
+        }
+        HandlerMethod handlerMethod = (HandlerMethod) handler;
+        Method method = handlerMethod.getMethod();
+        // 获取当前被拦截方法是否由SafeClick
+        SafeClick safeClick = method.getAnnotation(SafeClick.class);
+        if (safeClick == null) {
+            // 没有添加@SafeClick,则不需要校验安全点击
+            return true;
+        }
+        // 获取操作人(当前登录人) ====>
+        // 当前请求在主线程内(tomcat线程池分配主线程),所有可以从ThreadLocal中直接获取
+        String userId = SessionHelper.getCurrentUserId();
+        if (StringUtils.isBlank(userId)) {
+            // 当前用户未登录(实际上合理不会触发未登录,因为我们在filter中刚完整了SSO登录认证)
+            // PS:但是为保证逻辑的严谨性,这里还是做了判空
+            throw new AuthException("未登录");
+        }
+        // 获取用户安全点击事件类型
+        String action = safeClick.event().name();
+        // 允许多久内操作一次
+        Long time = safeClick.time();
+        // 构造redis分布式锁redis key【规则:事件名称-用户id】
+        String redisKey = action.concat("-").concat(userId);
+        boolean installLock = redisService.installLockForMS(redisKey, WebConstant.CACHE_NONE, time);
+        if (installLock) {
+            // 分布式加锁成功,说明当前用户在请求可以允许放行,反之则触发限流
+            return true;
+        }
+        log.error("<<< {} >>>, userId: {}, action:{}", safeClick.message(), userId, action);
+        // 触发限流,抛异常,异常提示来自用户注解自定义异常描述
+        throw new BusinessException(safeClick.message());
+    }
+}

+ 56 - 0
webchat-client-mall/src/main/java/com/webchat/client/mall/controller/AccountController.java

@@ -0,0 +1,56 @@
+package com.webchat.client.mall.controller;
+
+
+import com.webchat.client.mall.service.AccountService;
+import com.webchat.client.mall.service.UserSessionService;
+import com.webchat.common.bean.APIResponseBean;
+import com.webchat.common.bean.APIResponseBeanUtil;
+import com.webchat.common.helper.SessionHelper;
+import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
+import jakarta.servlet.http.HttpServletRequest;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/mall-service/account")
+public class AccountController {
+
+    @Autowired
+    private AccountService userService;
+
+    @Autowired
+    private UserSessionService userSessionService;
+
+    @Autowired
+    private HttpServletRequest request;
+
+    @Value("${oauth.server-url}")
+    private String logoutServer;
+
+    /**
+     * 查询电商服务登录用户信息
+     *
+     * @return
+     */
+    @GetMapping("/current/info")
+    public APIResponseBean<UserBaseResponseInfoVO> getCurrentUserInfo() {
+        String userId = SessionHelper.getCurrentUserId();
+        return APIResponseBeanUtil.success(userService.userInfo(userId));
+    }
+
+    /**
+     * 退出登录
+     *
+     * @return
+     */
+    @GetMapping("/logout")
+    public APIResponseBean<String> logout() {
+        userSessionService.logout(request);
+        String ssoServerUrl = String.format(logoutServer, request.getHeader("origin-url"));
+        return APIResponseBeanUtil.success(ssoServerUrl);
+    }
+
+}

+ 114 - 0
webchat-client-mall/src/main/java/com/webchat/client/mall/filter/OauthFilter.java

@@ -0,0 +1,114 @@
+package com.webchat.client.mall.filter;
+
+import com.webchat.client.mall.service.UserSessionService;
+import com.webchat.common.config.properties.OauthProperties;
+import com.webchat.common.enums.BaseErrCodeEnum;
+import com.webchat.common.helper.SessionHelper;
+import com.webchat.common.util.JsonUtil;
+import com.webchat.rmi.sso.OauthServiceClient;
+import jakarta.servlet.Filter;
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.FilterConfig;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+import jakarta.servlet.annotation.WebFilter;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @Author: 程序员七七
+ * @Date: 16.11.21 11:26 下午
+ */
+@Slf4j
+@Order(-6)
+@WebFilter(filterName = "oauthFilter", urlPatterns = {"/*"})
+@Component
+public class
+OauthFilter implements Filter {
+
+    @Autowired
+    private OauthProperties oauthProperties;
+
+    @Autowired
+    private OauthServiceClient oauthServiceClient;
+
+    @Autowired
+    private UserSessionService userSessionService;
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+
+    }
+
+    @Override
+    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
+        HttpServletRequest request = (HttpServletRequest) servletRequest;
+        HttpServletResponse response = (HttpServletResponse) servletResponse;
+        // 取集中session服务判断是否已经登录,如果已经登录则直接进入后续方法。
+        String oauthCode = request.getHeader("oauth-code");
+        String userId = userSessionService.getUserIdByRequest(request);
+        if (StringUtils.isNotBlank(userId)) {
+            // 已经登录
+            SessionHelper.setUserId(userId);
+            try {
+                filterChain.doFilter(request, response);
+            } finally {
+                SessionHelper.clear();
+            }
+            return;
+        }
+        if (StringUtils.isNotBlank(oauthCode)) {
+            userId = oauthServiceClient.getUserIdByOauthCode(oauthCode).getData();
+            if (StringUtils.isNotBlank(userId)) {
+                userSessionService.createUserLoginSession(response, userId);
+                SessionHelper.setUserId(userId);
+                try {
+                    filterChain.doFilter(request, response);
+                } finally {
+                    SessionHelper.clear();
+                }
+                return;
+            }
+        }
+        /**
+         * 302 重定向到统一登录页面
+         */
+        this.unLogin(request, response);
+    }
+
+    private void unLogin(HttpServletRequest request, HttpServletResponse response) {
+        Map<String, Object> responseMap = new HashMap<>();
+        response.setCharacterEncoding("UTF-8"); // 设置字符编码为 UTF-8
+        response.setContentType("application/json;charset=UTF-8"); // 设置内容类型和字符编码
+        responseMap.put("code", BaseErrCodeEnum.UN_LOGIN.getCode());
+        responseMap.put("message", BaseErrCodeEnum.UN_LOGIN.getMessage());
+        responseMap.put("redirect_url", getRedirectUrl(request));
+        try {
+            response.getWriter().println(JsonUtil.toJsonString(responseMap));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private String getRedirectUrl(HttpServletRequest request) {
+        String serverUrl = oauthProperties.getServerUrl();
+        String originUrl = request.getHeader("origin-url");
+        return String.format(serverUrl, URLEncoder.encode(originUrl));
+    }
+
+    @Override
+    public void destroy() {
+
+    }
+}

+ 45 - 0
webchat-client-mall/src/main/java/com/webchat/client/mall/service/AccountService.java

@@ -0,0 +1,45 @@
+package com.webchat.client.mall.service;
+
+import com.webchat.common.bean.APIResponseBean;
+import com.webchat.common.bean.APIResponseBeanUtil;
+import com.webchat.domain.vo.response.UserBaseResponseInfoVO;
+import com.webchat.rmi.user.UserServiceClient;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class AccountService {
+
+    @Autowired
+    private UserServiceClient userServiceClient;
+
+    /**
+     * 获取用户信息
+     *
+     * @param userId
+     * @return
+     */
+    public UserBaseResponseInfoVO userInfo(String userId) {
+        APIResponseBean<UserBaseResponseInfoVO> responseBean = userServiceClient.userInfo(userId);
+        if (APIResponseBeanUtil.isOk(responseBean)) {
+            return responseBean.getData();
+        }
+        // TODO
+        return null;
+    }
+
+    /**
+     * 用户账号查询详情
+     *
+     * @param account
+     * @return
+     */
+    public UserBaseResponseInfoVO queryAccount( String account) {
+        APIResponseBean<UserBaseResponseInfoVO> responseBean = userServiceClient.queryAccount(account);
+        if (APIResponseBeanUtil.isOk(responseBean)) {
+            return responseBean.getData();
+        }
+        // TODO
+        return null;
+    }
+}

+ 69 - 0
webchat-client-mall/src/main/java/com/webchat/client/mall/service/UserSessionService.java

@@ -0,0 +1,69 @@
+package com.webchat.client.mall.service;
+
+import com.webchat.common.constants.CookieConstants;
+import com.webchat.common.enums.RedisKeyEnum;
+import com.webchat.common.service.RedisService;
+import com.webchat.common.util.DateUtils;
+import com.webchat.common.util.MD5Utils;
+import jakarta.servlet.http.Cookie;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+@Service
+public class UserSessionService {
+
+    @Autowired
+    private RedisService redisService;
+
+    public String getUserIdByRequest(HttpServletRequest request) {
+        Cookie[] cookies = request.getCookies();
+        if (cookies != null) {
+            for (Cookie cookie : cookies) {
+                if (CookieConstants.WEB_CHAT_MALL_USER_COOKIE_KEY.equals(cookie.getName())) {
+                    String sessionId = cookie.getValue();
+                    return this.getUserIdBySessionId(sessionId);
+                }
+            }
+        }
+        return null;
+    }
+
+    public void logout(HttpServletRequest request) {
+        Cookie[] cookies = request.getCookies();
+        if (cookies != null) {
+            for (Cookie cookie : cookies) {
+                if (CookieConstants.WEB_CHAT_MALL_USER_COOKIE_KEY.equals(cookie.getName())) {
+                    String sessionId = cookie.getValue();
+                    this.deleteUserLoginSession(sessionId);
+                }
+            }
+        }
+    }
+
+    public void createUserLoginSession(HttpServletResponse response, String userId) {
+        String sessionId = MD5Utils.md5(userId + DateUtils.getCurrentFormatDate());
+        this.setUserIdBySessionId(sessionId, userId);
+        // 种浏览器Cookie
+        Cookie cookie = new Cookie(CookieConstants.WEB_CHAT_MALL_USER_COOKIE_KEY, sessionId);
+        cookie.setPath("/");
+        cookie.setMaxAge(CookieConstants.COOKIE_OUT_TIME);
+        response.addCookie(cookie);
+    }
+
+    private void deleteUserLoginSession(String sessionId) {
+        String sessionCacheKey = RedisKeyEnum.USER_SESSION_PREFIX.getKey(sessionId);
+        redisService.remove(sessionCacheKey);
+    }
+
+    public String getUserIdBySessionId(String sessionId) {
+        String sessionCacheKey = RedisKeyEnum.USER_SESSION_PREFIX.getKey(sessionId);
+        return redisService.get(sessionCacheKey);
+    }
+
+    public void setUserIdBySessionId(String sessionId, String userId) {
+        String sessionCacheKey = RedisKeyEnum.USER_SESSION_PREFIX.getKey(sessionId);
+        redisService.set(sessionCacheKey, userId, RedisKeyEnum.USER_SESSION_PREFIX.getExpireTime());
+    }
+}

+ 1 - 0
webchat-client-mall/src/main/resources/application.yml

@@ -0,0 +1 @@
+server:
  port: 8083

+ 21 - 0
webchat-client-mall/src/main/resources/bootstrap.yml

@@ -0,0 +1,21 @@
+spring:
+  application:
+    name: webchat-client-mall-service
+  profiles:
+    active: dev
+  cloud:
+    nacos:
+      config:
+        server-addr: 127.0.0.1:8848
+        namespace: 2cdfb8e3-f127-4020-93df-2d4c3dc805b5
+        file-extension: yaml
+        group: DEFAULT_GROUP
+        refresh-enabled: true
+      discovery:
+        server-addr: 127.0.0.1:8848
+        namespace: 2cdfb8e3-f127-4020-93df-2d4c3dc805b5
+    sentinel:
+      transport:
+        port: 8719
+        dashboard: 127.0.0.1:8080
+      eager: true

+ 2 - 0
webchat-common/src/main/java/com/webchat/common/constants/CookieConstants.java

@@ -9,6 +9,8 @@ public class CookieConstants {
 
     public static final String WEB_CHAT_CLIENT_USER_COOKIE_KEY = "WEB_CHAT_CLIENT_USER_COOKIE_KEY";
 
+    public static final String WEB_CHAT_MALL_USER_COOKIE_KEY = "WEB_CHAT_MALL_USER_COOKIE_KEY";
+
     /***
      * cookie过期时间 秒为单位,3天
      */

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
webchat-gateway/src/main/resources/application.yml


Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно