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 029970c..14d31ca 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 @@ -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(); } 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 9f93524..c5bd308 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 @@ -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); } 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 d1cf8c1..27468aa 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 @@ -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); } }