feat: 新增退出账号功能

This commit is contained in:
weihongyang 2022-06-23 11:55:44 +08:00
parent 681b5fbe63
commit 40c7df817b
3 changed files with 30 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package com.cnbm.admin.controller; package com.cnbm.admin.controller;
import com.cnbm.admin.handler.LogoutSuccessHandlerImpl;
import com.cnbm.admin.params.LoginParam; import com.cnbm.admin.params.LoginParam;
import com.cnbm.admin.service.LoginService; import com.cnbm.admin.service.LoginService;
import com.cnbm.admin.utils.ResponseResult; import com.cnbm.admin.utils.ResponseResult;
@ -8,12 +9,14 @@ import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RestController; import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** /**
* @Author weihongyang * @Author weihongyang
@ -28,16 +31,19 @@ public class LoginController {
@Autowired @Autowired
private LoginService loginService; private LoginService loginService;
@RequestMapping("/hello") @Autowired
@PreAuthorize("@ex.hasAuthority('sys:user:page')") private LogoutSuccessHandlerImpl logoutSuccessHandler;
public String hello(){
log.info("hello");
return "hello";
}
@PostMapping("/login") @PostMapping("/login")
@ApiOperation(value = "登录") @ApiOperation(value = "登录")
public ResponseResult login(HttpServletRequest request, @RequestBody LoginParam loginParam) { public ResponseResult login(HttpServletRequest request, @RequestBody LoginParam loginParam) {
return loginService.login(request,loginParam); return loginService.login(request,loginParam);
} }
@PostMapping("/doLogout")
@ApiOperation(value = "退出")
public void logout(){
}
} }

View File

@ -13,4 +13,6 @@ import javax.servlet.http.HttpServletRequest;
public interface LoginService { public interface LoginService {
ResponseResult login(HttpServletRequest request, LoginParam loginParam); ResponseResult login(HttpServletRequest request, LoginParam loginParam);
ResponseResult logout();
} }

View File

@ -10,6 +10,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -48,4 +49,15 @@ public class LoginServiceImpl implements LoginService {
redisTemplate.opsForValue().set("login:"+userid,loginUser); redisTemplate.opsForValue().set("login:"+userid,loginUser);
return new ResponseResult(200,"登录成功",map); 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,"注销成功");
}
} }