package com.moon.server.controller.all;
|
|
import com.moon.server.annotation.SysLog;
|
import com.moon.server.config.PropertiesConfig;
|
import com.moon.server.entity.all.ResponseMsg;
|
import com.moon.server.entity.all.SettingData;
|
import com.moon.server.entity.all.StaticData;
|
import com.moon.server.entity.sys.LoginEntity;
|
import com.moon.server.entity.sys.OperateEntity;
|
import com.moon.server.entity.sys.TokenEntity;
|
import com.moon.server.entity.sys.UserEntity;
|
import com.moon.server.service.all.ScheduleService;
|
import com.moon.server.service.sys.LoginService;
|
import com.moon.server.service.sys.OperateService;
|
import com.moon.server.service.sys.TokenService;
|
import com.moon.server.service.sys.UserService;
|
import com.moon.server.service.all.SignService;
|
import com.moon.server.helper.Md5Helper;
|
import com.moon.server.helper.RsaHelper;
|
import com.moon.server.helper.StringHelper;
|
import com.moon.server.helper.WebHelper;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParams;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.servlet.ModelAndView;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
/**
|
* 签名控制器
|
* @author WWW
|
* @date 2022-09-21
|
*/
|
@Api(tags = "运维管理\\签名管理")
|
@RestController
|
@RequestMapping("/sign")
|
public class SignController extends BaseController {
|
@Autowired
|
UserService userService;
|
|
@Autowired
|
LoginService loginService;
|
|
@Autowired
|
TokenService tokenService;
|
|
@Autowired
|
SignService signService;
|
|
@Autowired
|
private OperateService operateService;
|
|
@Autowired
|
PropertiesConfig propertiesConfig;
|
|
@Autowired
|
private ScheduleService scheduleService;
|
|
@SysLog()
|
@ApiOperation(value = "跳转首页")
|
@GetMapping({"/", "/toIndex"})
|
public ModelAndView toIndex(ModelAndView mv, HttpServletRequest req) {
|
mv.setViewName("index");
|
|
UserEntity ue = tokenService.getCurrentUser(req);
|
if (ue != null) {
|
mv.addObject("msg", "Hello " + ue.getUname() + " !");
|
}
|
|
return mv;
|
}
|
|
@SysLog()
|
@ApiOperation(value = "跳转登录页")
|
@GetMapping("/toLogin")
|
public ModelAndView toLogin(ModelAndView mv) {
|
mv.setViewName("login");
|
|
return mv;
|
}
|
|
@SysLog()
|
@ApiOperation(value = "数据库监控")
|
@GetMapping(value = "/toDruid")
|
public ModelAndView toDruid(HttpServletRequest req, HttpServletResponse res) {
|
ModelAndView mv = new ModelAndView();
|
mv.setViewName("druid");
|
|
try {
|
UserEntity ue = tokenService.getCurrentUser(req);
|
if (ue != null) {
|
String sessionId = WebHelper.getCookieByKey(StaticData.DRUID_COOKIE_KEY, req);
|
if (StringHelper.isNull(sessionId)) {
|
signService.loginDruid(req, res);
|
}
|
}
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
}
|
|
return mv;
|
}
|
|
@SysLog()
|
@ApiOperation(value = "资源监控")
|
@GetMapping(value = "/toMonitor")
|
public ModelAndView toMonitor(ModelAndView mv, HttpServletRequest req, HttpServletResponse res) {
|
try {
|
mv.setViewName("redirect:/toLogin");
|
|
UserEntity ue = tokenService.getCurrentUser(req);
|
if (ue != null) {
|
mv.setViewName("monitor");
|
}
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
}
|
|
return mv;
|
}
|
|
@SysLog()
|
@ApiOperation(value = "登录")
|
@ApiImplicitParams({
|
@ApiImplicitParam(name = "user", value = "用戶名", dataType = "UsersEntity", paramType = "body", example = "")
|
})
|
@PostMapping(value = "/login", produces = "application/json; charset=UTF-8")
|
public ResponseMsg<TokenEntity> login(@RequestBody UserEntity user, HttpServletRequest req, HttpServletResponse res) {
|
try {
|
String str = userService.validateLoginPwd(user);
|
if (str != null) {
|
return fail(str, null);
|
}
|
|
UserEntity ue = userService.selectByUid(user.getUid());
|
if (ue == null) {
|
return fail("用户名不存在", null);
|
}
|
|
if (!Md5Helper.validatePassword(user.getPwd(), ue.getPwd())) {
|
tokenService.setPwdErrCache(ue);
|
return fail("密码不正确", null);
|
}
|
|
LoginEntity le = loginService.getNewLogin(ue.getId(), 1, 1, 1, req);
|
Integer rows = loginService.insertLogin(le);
|
if (rows == 0) {
|
return fail("创建登录日志失败", null);
|
}
|
|
TokenEntity te = tokenService.getNewToken(ue, req);
|
rows = tokenService.insertToken(te);
|
if (rows == 0) {
|
return fail("创建令牌失败", null);
|
}
|
|
te.setAutoLogOut(SettingData.AUTO_LOGOUT);
|
tokenService.saveToken(ue, te, req, res);
|
|
int onlineUser = scheduleService.countOnlineUsers();
|
String msg = onlineUser >= SettingData.MAX_USER_LOGIN ? "警告:系统已经到达用户访问量的上限!" : "";
|
|
return success(msg, te);
|
} catch (Exception ex) {
|
return fail(ex, null);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "登出")
|
@GetMapping(value = "/logout")
|
public ResponseMsg<Boolean> logout(HttpServletRequest req, HttpServletResponse res) {
|
try {
|
String token = WebHelper.getToken(req);
|
if (StringHelper.isEmpty(token)) {
|
return fail("没有检测到令牌或无效", false);
|
}
|
|
Boolean flag = tokenService.logout(token, req, res);
|
|
return success(flag ? "登出成功" : "登出失败", flag);
|
} catch (Exception ex) {
|
return fail(ex, false);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "检查是/否登录")
|
@GetMapping("/check")
|
public ResponseMsg<Boolean> check(HttpServletRequest req, HttpServletResponse res) {
|
try {
|
Boolean flag = tokenService.isLogin(req, res);
|
if (flag) {
|
// 写日志
|
UserEntity ue = tokenService.getCurrentUser(req);
|
LoginEntity le = loginService.getNewLogin(ue.getId(), 1, 2, 1, req);
|
Integer rows = loginService.insertLogin(le);
|
}
|
|
return success(flag ? "用户已登录" : "用户未登录", flag);
|
} catch (Exception ex) {
|
return fail(ex, false);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "获取当前用户")
|
@GetMapping("/getCurrentUser")
|
public ResponseMsg<UserEntity> getCurrentUser(HttpServletRequest req) {
|
try {
|
UserEntity ue = tokenService.getCurrentUser(req);
|
if (ue == null) {
|
return fail("没有找到", null);
|
}
|
|
return success(ue);
|
} catch (Exception ex) {
|
return fail(ex, null);
|
}
|
}
|
|
@SysLog()
|
@ApiOperation(value = "获取RSA加密公钥")
|
@GetMapping("/getPublicKey")
|
public ResponseMsg<String> getPublicKey() {
|
try {
|
String key = RsaHelper.getPublicKey();
|
|
return success(key);
|
} catch (Exception ex) {
|
return fail(ex, null);
|
}
|
}
|
|
@ApiOperation(value = "插入操作日志")
|
@GetMapping("/insertOpLog")
|
public ResponseMsg<Object> insertOpLog(String m1, String m2, HttpServletRequest req) {
|
try {
|
if (StringHelper.isEmpty(m1)) {
|
return fail("一级模块必填", 0);
|
}
|
|
OperateEntity oe = new OperateEntity();
|
oe.setIp(WebHelper.getIpAddress(req));
|
oe.setModular1(m1);
|
oe.setModular2(m2);
|
oe.setType(0);
|
|
UserEntity ue = tokenService.getCurrentUser(req);
|
if (ue != null) {
|
oe.setUserid(ue.getId());
|
}
|
|
int rows = operateService.insertOperate(oe);
|
|
return success(rows);
|
} catch (Exception ex) {
|
return fail(ex, 0);
|
}
|
}
|
}
|