refactor(系统设置): 登录登出增加日志

This commit is contained in:
wxg0103 2023-08-22 16:01:40 +08:00 committed by wxg0103
parent a0f562a3a8
commit 8fa5ed30dd
6 changed files with 26 additions and 96 deletions

View File

@ -2,11 +2,9 @@ package io.metersphere.sdk.controller;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.LicenseDTO; import io.metersphere.sdk.dto.LicenseDTO;
import io.metersphere.sdk.log.annotation.Log;
import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.service.LicenseLogService;
import io.metersphere.sdk.service.LicenseService; import io.metersphere.sdk.service.LicenseService;
import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.SessionUtils;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.Logical;
@ -30,11 +28,10 @@ public class LicenseController {
@PostMapping("/add") @PostMapping("/add")
@Operation(summary = "添加有效的License") @Operation(summary = "添加有效的License")
@RequiresPermissions(value= {PermissionConstants.SYSTEM_AUTH_READ, PermissionConstants.SYSTEM_AUTH_READ_UPDATE}, logical = Logical.OR) @RequiresPermissions(value= {PermissionConstants.SYSTEM_AUTH_READ, PermissionConstants.SYSTEM_AUTH_READ_UPDATE}, logical = Logical.OR)
@Log(type = OperationLogType.ADD, expression = "#msClass.addLog()", msClass = LicenseLogService.class)
public LicenseDTO addLicense(@RequestBody String licenseCode) { public LicenseDTO addLicense(@RequestBody String licenseCode) {
LicenseService licenseService = CommonBeanFactory.getBean(LicenseService.class); LicenseService licenseService = CommonBeanFactory.getBean(LicenseService.class);
if (licenseService != null) { if (licenseService != null) {
return licenseService.addLicense(licenseCode); return licenseService.addLicense(licenseCode, SessionUtils.getUserId());
} }
return new LicenseDTO(); return new LicenseDTO();
} }

View File

@ -1,16 +1,15 @@
package io.metersphere.sdk.controller; package io.metersphere.sdk.controller;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.UserSource; import io.metersphere.sdk.constants.UserSource;
import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.LoginRequest; import io.metersphere.sdk.dto.LoginRequest;
import io.metersphere.sdk.dto.SessionUser; import io.metersphere.sdk.dto.SessionUser;
import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.dto.UserDTO;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.annotation.Log;
import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.service.BaseUserService; import io.metersphere.sdk.service.BaseUserService;
import io.metersphere.sdk.service.LoginLogService;
import io.metersphere.sdk.util.RsaKey; import io.metersphere.sdk.util.RsaKey;
import io.metersphere.sdk.util.RsaUtil; import io.metersphere.sdk.util.RsaUtil;
import io.metersphere.sdk.util.SessionUtils; import io.metersphere.sdk.util.SessionUtils;
@ -59,7 +58,6 @@ public class LoginController {
@PostMapping(value = "/login") @PostMapping(value = "/login")
@Operation(summary = "登录") @Operation(summary = "登录")
@Log(type = OperationLogType.LOGIN, expression = "#msClass.loginLog()", msClass = LoginLogService.class)
public ResultHolder login(@RequestBody LoginRequest request) { public ResultHolder login(@RequestBody LoginRequest request) {
SessionUser sessionUser = SessionUtils.getUser(); SessionUser sessionUser = SessionUtils.getUser();
if (sessionUser != null) { if (sessionUser != null) {
@ -77,8 +75,8 @@ public class LoginController {
@GetMapping(value = "/signout") @GetMapping(value = "/signout")
@Operation(summary = "退出登录") @Operation(summary = "退出登录")
@Log(type = OperationLogType.LOGOUT, expression = "#msClass.logoutLog()", msClass = LoginLogService.class)
public ResultHolder logout(HttpServletResponse response) throws Exception { public ResultHolder logout(HttpServletResponse response) throws Exception {
baseUserService.saveLog(SessionUtils.getUserId(), HttpMethodConstants.GET.name(), "/signout", "登出成功", OperationLogType.LOGOUT.name());
SecurityUtils.getSubject().logout(); SecurityUtils.getSubject().logout();
return ResultHolder.success("logout success"); return ResultHolder.success("logout success");
} }

View File

@ -3,12 +3,12 @@ package io.metersphere.sdk.service;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.domain.ProjectExample; import io.metersphere.project.domain.ProjectExample;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.InternalUserRole; import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.constants.UserRoleType;
import io.metersphere.sdk.constants.UserSource;
import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.*; import io.metersphere.sdk.dto.*;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.log.service.OperationLogService;
import io.metersphere.sdk.mapper.BaseProjectMapper; import io.metersphere.sdk.mapper.BaseProjectMapper;
import io.metersphere.sdk.mapper.BaseUserMapper; import io.metersphere.sdk.mapper.BaseUserMapper;
import io.metersphere.sdk.util.CodingUtil; import io.metersphere.sdk.util.CodingUtil;
@ -54,6 +54,8 @@ public class BaseUserService {
private ProjectMapper projectMapper; private ProjectMapper projectMapper;
@Resource @Resource
private BaseProjectMapper baseProjectMapper; private BaseProjectMapper baseProjectMapper;
@Resource
private OperationLogService operationLogService;
public UserDTO getUserDTO(String userId) { public UserDTO getUserDTO(String userId) {
@ -78,11 +80,11 @@ public class BaseUserService {
if (!StringUtils.equals(login, UserSource.LDAP.name())) { if (!StringUtils.equals(login, UserSource.LDAP.name())) {
password = StringUtils.trim(request.getPassword()); password = StringUtils.trim(request.getPassword());
} }
UsernamePasswordToken token = new UsernamePasswordToken(username, password); UsernamePasswordToken token = new UsernamePasswordToken(username, password);
Subject subject = SecurityUtils.getSubject(); Subject subject = SecurityUtils.getSubject();
try { try {
subject.login(token); subject.login(token);
saveLog(SessionUtils.getUserId(), HttpMethodConstants.POST.name(), "/login", "登录成功", OperationLogType.LOGIN.name());
if (subject.isAuthenticated()) { if (subject.isAuthenticated()) {
SessionUser sessionUser = SessionUtils.getUser(); SessionUser sessionUser = SessionUtils.getUser();
autoSwitch(sessionUser); autoSwitch(sessionUser);
@ -106,6 +108,21 @@ public class BaseUserService {
throw new UnauthorizedException(Translator.get("not_authorized") + e.getMessage()); throw new UnauthorizedException(Translator.get("not_authorized") + e.getMessage());
} }
} }
//保存日志
public void saveLog(String userId, String method, String path, String content, String type){
User user = userMapper.selectByPrimaryKey(userId);
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
userId,
type,
OperationLogConstants.SYSTEM,
StringUtils.join(user.getName(),StringUtils.EMPTY, content));
dto.setMethod(method);
dto.setPath(path);
operationLogService.add(dto);
}
public void autoSwitch(UserDTO user) { public void autoSwitch(UserDTO user) {
// 用户有 last_project_id 权限 // 用户有 last_project_id 权限

View File

@ -1,35 +0,0 @@
package io.metersphere.sdk.service;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.OperationLogConstants;
import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(rollbackFor = Exception.class)
public class LicenseLogService {
/**
* 添加接口日志
*
* @return
*/
public LogDTO addLog() {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
null,
null,
OperationLogType.ADD.name(),
OperationLogModule.SYSTEM_AUTHORIZATION_MANAGEMENT,
"License授权");
dto.setPath("/license/add");
dto.setMethod(HttpMethodConstants.POST.name());
return dto;
}
}

View File

@ -9,6 +9,6 @@ public interface LicenseService {
LicenseDTO validate(); LicenseDTO validate();
LicenseDTO addLicense(String licenseCode); LicenseDTO addLicense(String licenseCode, String userId);
} }

View File

@ -1,47 +0,0 @@
package io.metersphere.sdk.service;
import io.metersphere.sdk.constants.OperationLogConstants;
import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.log.constants.OperationLogType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(rollbackFor = Exception.class)
public class LoginLogService {
/**
* 添加接口日志
*
* @return
*/
public LogDTO loginLog() {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
null,
null,
OperationLogType.LOGIN.name(),
OperationLogConstants.SYSTEM,
"登录");
return dto;
}
/**
* 添加接口日志
*
* @return
*/
public LogDTO logoutLog() {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
null,
null,
OperationLogType.LOGIN.name(),
OperationLogConstants.SYSTEM,
"登出");
return dto;
}
}