feat: 新增退出账号功能

This commit is contained in:
weihongyang 2022-06-23 11:55:44 +08:00
parent 681b5fbe63
commit 40c7df817b
3 ha cambiato i file con 30 aggiunte e 10 eliminazioni

Vedi File

@ -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(){
}
}

Vedi File

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

Vedi File

@ -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,"注销成功");
}
}