feat:新增退出功能

This commit is contained in:
weihongyang 2022-08-09 14:21:05 +08:00
parent 78fa6f42b0
commit f5988be736
3 changed files with 36 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import com.cnbm.admin.service.CaptchaService;
import com.cnbm.admin.service.LoginService;
import com.cnbm.admin.utils.ResponseResult;
import com.cnbm.common.exception.ErrorCode;
import com.cnbm.common.utils.Result;
import com.cnbm.common.validator.AssertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@ -52,13 +53,15 @@ public class LoginController {
@PostMapping("/login")
@ApiOperation(value = "登录")
public ResponseResult login(HttpServletRequest request, @RequestBody LoginParam loginParam) {
public Result login(HttpServletRequest request, @RequestBody LoginParam loginParam) {
return loginService.login(request,loginParam);
}
@PostMapping("/doLogout")
@PostMapping("logout")
@ApiOperation(value = "退出")
public void logout(){
public Result logout(HttpServletRequest request){
loginService.logout(request);
return new Result();
}

View File

@ -1,7 +1,7 @@
package com.cnbm.admin.service;
import com.cnbm.admin.params.LoginParam;
import com.cnbm.admin.utils.ResponseResult;
import com.cnbm.common.utils.Result;
import javax.servlet.http.HttpServletRequest;
@ -12,6 +12,8 @@ import javax.servlet.http.HttpServletRequest;
*/
public interface LoginService {
ResponseResult login(HttpServletRequest request, LoginParam loginParam);
Result login(HttpServletRequest request, LoginParam loginParam);
void logout(HttpServletRequest request);
}

View File

@ -12,6 +12,8 @@ import com.cnbm.admin.service.LoginService;
import com.cnbm.admin.utils.ResponseResult;
import com.cnbm.common.exception.ErrorCode;
import com.cnbm.common.utils.IpUtils;
import com.cnbm.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
@ -48,11 +50,11 @@ public class LoginServiceImpl implements LoginService {
private SysLogLoginService sysLogLoginService;
@Override
public ResponseResult login(HttpServletRequest request, LoginParam loginParam) {
public Result login(HttpServletRequest request, LoginParam loginParam) {
//验证码是否正确
boolean flag = captchaService.validate(loginParam.getUuid(), loginParam.getCaptcha());
if(!flag){
return new ResponseResult(ErrorCode.CAPTCHA_ERROR,"验证码错误");
return new Result<>().error(ErrorCode.CAPTCHA_ERROR, "验证码错误");
}
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginParam.getUsername(),loginParam.getPassword());
Authentication authenticate = authenticationManager.authenticate(authenticationToken);
@ -83,7 +85,28 @@ public class LoginServiceImpl implements LoginService {
log.setCreator(loginUser.getSysUserEntity().getId());
log.setCreatorName(loginUser.getUsername());
sysLogLoginService.save(log);
return new ResponseResult(200,"登录成功",map);
return new Result<>().ok(map);
}
@Override
public void logout(HttpServletRequest request) {
//获取SecurityContextHolder中的用户id
UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
Long userid = loginUser.getSysUserEntity().getId();
//日志
SysLogLoginEntity log = new SysLogLoginEntity();
log.setOperation(LoginOperationEnum.LOGOUT.value());
log.setIp(IpUtils.getIpAddr(request));
log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
log.setIp(IpUtils.getIpAddr(request));
log.setStatus(LoginStatusEnum.SUCCESS.value());
log.setCreator(userid);
log.setCreatorName(loginUser.getUsername());
log.setCreateDate(new Date());
sysLogLoginService.save(log);
//删除redis中的值
redisTemplate.delete("login:"+userid);
}
}