From 40c7df817b22fef0118297998a3c90ecdd74f058 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Thu, 23 Jun 2022 11:55:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=80=80=E5=87=BA?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/LoginController.java | 26 ++++++++++++------- .../com/cnbm/admin/service/LoginService.java | 2 ++ .../admin/service/impl/LoginServiceImpl.java | 12 +++++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java b/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java index 39e020b..64cc902 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java +++ b/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java @@ -1,5 +1,6 @@ package com.cnbm.admin.controller; +import com.cnbm.admin.handler.LogoutSuccessHandlerImpl; import com.cnbm.admin.params.LoginParam; import com.cnbm.admin.service.LoginService; import com.cnbm.admin.utils.ResponseResult; @@ -8,12 +9,14 @@ import io.swagger.annotations.ApiOperation; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; -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.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; +import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * @Author weihongyang @@ -28,16 +31,19 @@ public class LoginController { @Autowired private LoginService loginService; - @RequestMapping("/hello") - @PreAuthorize("@ex.hasAuthority('sys:user:page')") - public String hello(){ - log.info("hello"); - return "hello"; - } + @Autowired + private LogoutSuccessHandlerImpl logoutSuccessHandler; @PostMapping("/login") @ApiOperation(value = "登录") public ResponseResult login(HttpServletRequest request, @RequestBody LoginParam loginParam) { return loginService.login(request,loginParam); } + + @PostMapping("/doLogout") + @ApiOperation(value = "退出") + public void logout(){ + } + + } diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/LoginService.java b/ym-admin/src/main/java/com/cnbm/admin/service/LoginService.java index 2dfce88..c3f3cff 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/LoginService.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/LoginService.java @@ -13,4 +13,6 @@ import javax.servlet.http.HttpServletRequest; public interface LoginService { ResponseResult login(HttpServletRequest request, LoginParam loginParam); + + ResponseResult logout(); } diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java index 8425e2e..cee7f12 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java @@ -10,6 +10,7 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; @@ -48,4 +49,15 @@ public class LoginServiceImpl implements LoginService { redisTemplate.opsForValue().set("login:"+userid,loginUser); return new ResponseResult(200,"登录成功",map); } + + @Override + public ResponseResult logout() { + //获取SecurityContextHolder中的用户id + UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + LoginUser loginUser = (LoginUser) authentication.getPrincipal(); + String userid = loginUser.getSysUserEntity().getId().toString(); + //删除redis中的值 + redisTemplate.delete("login:"+userid); + return new ResponseResult(200,"注销成功"); + } }