From 5684bf988ca723a054ee83628eac5da5c0a687a7 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Fri, 27 May 2022 11:22:06 +0800
Subject: [PATCH] 20220527

---
 src/main/java/com/xcong/farmer/cms/configurations/security/WebSecurityConfig.java       |    2 +-
 src/main/java/com/xcong/farmer/cms/modules/system/service/ICommonService.java           |    1 +
 src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java |   15 +++++++++++----
 src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java   |   18 ++++++++++++++++++
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/xcong/farmer/cms/configurations/security/WebSecurityConfig.java b/src/main/java/com/xcong/farmer/cms/configurations/security/WebSecurityConfig.java
index 0fe9e94..7d8f85e 100644
--- a/src/main/java/com/xcong/farmer/cms/configurations/security/WebSecurityConfig.java
+++ b/src/main/java/com/xcong/farmer/cms/configurations/security/WebSecurityConfig.java
@@ -42,7 +42,7 @@
                 .antMatchers("/swagger**/**").permitAll()
                 .antMatchers("/webjars/**").permitAll()
                 .antMatchers("/v2/**").permitAll()
-                .antMatchers("/api/common/**").permitAll()
+                .antMatchers("/api/common/login").permitAll()
                 .anyRequest().authenticated()
                 .and().apply(securityConfiguereAdapter());
     }
diff --git a/src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java b/src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java
index edccb6c..4156ded 100644
--- a/src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java
+++ b/src/main/java/com/xcong/farmer/cms/modules/system/Controller/AdminCommonController.java
@@ -18,10 +18,7 @@
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
@@ -52,4 +49,14 @@
         return iCommonService.login(adminLoginDto);
     }
 
+    /**
+     * 用户退出登录
+     * @return
+     */
+    @ApiOperation(value="用户退出登录", notes="用户退出登录")
+    @GetMapping(value = "/Logout/{id}")
+    public Result  memberLogout(@PathVariable(value = "id") Long id) {
+        return iCommonService.memberLogout(id);
+    }
+
 }
diff --git a/src/main/java/com/xcong/farmer/cms/modules/system/service/ICommonService.java b/src/main/java/com/xcong/farmer/cms/modules/system/service/ICommonService.java
index 5eb9be0..5969a21 100644
--- a/src/main/java/com/xcong/farmer/cms/modules/system/service/ICommonService.java
+++ b/src/main/java/com/xcong/farmer/cms/modules/system/service/ICommonService.java
@@ -9,4 +9,5 @@
 
     Result login(AdminLoginDto adminLoginDto);
 
+    Result memberLogout(Long id);
 }
diff --git a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java
index 6b165e1..4399b6a 100644
--- a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java
+++ b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java
@@ -8,12 +8,15 @@
 import com.xcong.farmer.cms.common.response.Result;
 import com.xcong.farmer.cms.configurations.properties.ApplicationProperties;
 import com.xcong.farmer.cms.configurations.properties.SecurityProperties;
+import com.xcong.farmer.cms.modules.member.entity.MemberEntity;
 import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto;
 import com.xcong.farmer.cms.modules.system.entity.UserEntity;
 import com.xcong.farmer.cms.modules.system.mapper.UserMapper;
 import com.xcong.farmer.cms.modules.system.service.ICommonService;
+import com.xcong.farmer.cms.utils.MessageSourceUtils;
 import com.xcong.farmer.cms.utils.RedisUtils;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Service;
 import java.util.HashMap;
 import java.util.Map;
@@ -69,6 +72,21 @@
         return Result.ok("登录成功", authInfo);
     }
 
+    @Override
+    public Result memberLogout(Long id) {
+        //获取用户ID
+        UserEntity userEntity = userMapper.selectById(id);
+        if (ObjectUtil.isEmpty(userEntity)) {
+            return Result.fail("用户不存在");
+        }
+
+        String redisMember = AppContants.APP_LOGIN_PREFIX + userEntity.getId();
+        String token = redisUtils.getString(redisMember);
+        redisUtils.del(AppContants.APP_LOGIN_PREFIX + token);
+        SecurityContextHolder.clearContext();
+        return Result.ok("退出成功");
+    }
+
     public String generateAsaToken(String token) {
         RSA rsa = new RSA(null, securityProperties.getPublicKey());
         return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);

--
Gitblit v1.9.1