Commit e93e63ce authored by zengtianlai3's avatar zengtianlai3

Merge branch 'm33' into 'master'

完善日志

See merge request !26
parents 91d9ee19 4c09d235
......@@ -3,6 +3,7 @@ package iot.sixiang.license.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import iot.sixiang.license.jwt.UserUtils;
import iot.sixiang.license.log.BusinessType;
import iot.sixiang.license.log.MyLog;
import iot.sixiang.license.model.BaseResult;
......@@ -22,7 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
*/
@RestController
@RequestMapping("/iot_license/alarm_read")
@Api(value = "告警已读模块", tags = {"告警已读模块"})
@Api(value = "告警模块", tags = {"告警模块"})
public class AlarmReadController {
@Autowired
......@@ -32,8 +33,8 @@ public class AlarmReadController {
@PostMapping("read")
@MyLog(title = "将告警信息状态设为已读", businessType = BusinessType.OTHER)
public BaseResult readAlarm(){
// int userId = jsonObject.getIntValue("userId");
int userId = 2147483647;
String id = UserUtils.getLoginUserId();
int userId = Integer.valueOf(id);
boolean res = alarmReadService.readAlarm(userId);
if (res) {
return BaseResult.success();
......
package iot.sixiang.license.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* <p>
* 前端控制器
* </p>
*
* @author m33
* @since 2022-06-10
*/
@RestController
@RequestMapping("/iot_license/alarm_type")
public class AlarmTypeController {
}
......@@ -68,21 +68,6 @@ public class LoginController {
@ApiImplicitParam(name = "password",value = "密码",required = true)
})
public ResResult logout(@RequestParam("userName") String userName, @RequestParam("password") String password) {
// @RequestBody JSONObject jsonObject
LoginUser user = new LoginUser();
user.setUserName(userName);
user.setPassword(password);
for (LoginUser dbUser : userMap.values()) {
if (dbUser.getUserName().equals(user.getUserName()) && dbUser.getPassword().equals(user.getPassword())) {
log.info("登录成功!生成token!");
String token = JwtUtil.createToken(dbUser);
Map<String, String> map = new HashMap<>();
map.put("authorization", token);
return ResResult.success().record(map);
}
}
return ResResult.fail().msg("用户名或密码错误");
}
}
package iot.sixiang.license.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import iot.sixiang.license.entity.SysOperLog;
import iot.sixiang.license.log.BusinessType;
import iot.sixiang.license.log.MyLog;
import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.model.PageResult;
import iot.sixiang.license.service.SysOperLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by m33 on 2022/6/14 13:54
*/
@RestController
@RequestMapping("/iot_license/oper_log")
@Api(value = "日志模块", tags = {"日志模块"})
public class SysOperLogController {
@Autowired
private SysOperLogService sysOperLogService;
/**
* 分页查询所有的oper_log
* @param pageNo
* @param pageSize
* @return
*/
@ApiOperation(value = "获取日志列表接口", notes = "用于获取日志列表")
@GetMapping("list")
@MyLog(title = "获取日志列表", businessType = BusinessType.SELECT)
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNo",value = "当前在第几页", required = true,dataType = "int"),
@ApiImplicitParam(name = "pageSize",value = "每页显示多少页", required = true, dataType = "int")
})
public PageResult getOperLogList(@RequestParam(value = "pageNo", defaultValue = "0") int pageNo,
@RequestParam(value = "pageSize",defaultValue = "0") int pageSize) {
PageInfoModel<SysOperLog> records = sysOperLogService.getOperLogList(pageNo,pageSize);
int total = records.getTotal();
int pages = total/pageSize;//pages为总页数
int mod = total%pageSize;
if(mod!=0){
pages = pages +1;
}
List<SysOperLog> result = records.getResult();
return new PageResult(200, "查找成功", pageNo, pages, total, result);
}
}
......@@ -2,15 +2,13 @@ package iot.sixiang.license.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
/**
* <p>
* 操作日志记录
......@@ -30,7 +28,7 @@ public class SysOperLog implements Serializable {
* 日志主键
*/
@TableId(value = "id", type = IdType.AUTO)
private Long id;
private Integer id;
/**
* 模块标题
......@@ -47,9 +45,9 @@ public class SysOperLog implements Serializable {
private Integer businessType;
/**
* 方法名称
* 路径名称
*/
private String method;
private String uri;
/**
* 操作状态(0正常 1异常)
......
......@@ -24,5 +24,6 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
UserUtils.removeUser();
UserUtils.removeUri();
}
}
\ No newline at end of file
......@@ -42,7 +42,11 @@ public class JwtFilter implements Filter {
String token = request.getHeader("authorization");
boolean check = true;
String uri = request.getRequestURI();
if (uri.contains(url1) || uri.contains(url2) || uri.contains(url3) || uri.contains(url4) || uri.contains(url7) || uri.contains(url8)) {
if(uri.contains(url1) || uri.contains(url2) ) {
UserUtils.setUri(uri);
}
check = false;
}
if (!check) {
......@@ -76,7 +80,7 @@ public class JwtFilter implements Filter {
LoginUser loginUser = new LoginUser(userId, userName, password);
UserUtils.setLoginUser(loginUser);
UserUtils.setUri(uri);
filterChain.doFilter(request, response);
}
}
......
......@@ -7,7 +7,9 @@ package iot.sixiang.license.jwt;
public abstract class UserUtils {
//线程变量,存放user实体类信息,即使是静态的与其他线程也是隔离的
private static ThreadLocal<LoginUser> userThreadLocal = new ThreadLocal<LoginUser>();
private static ThreadLocal<LoginUser> userThreadLocal = new ThreadLocal<>();
//线程变量,存放uri,即使是静态的与其他线程也是隔离的
private static ThreadLocal<String> uriThreadLocal = new ThreadLocal<>();
//从当前线程变量中获取用户信息
public static LoginUser getLoginUser() {
......@@ -34,8 +36,27 @@ public abstract class UserUtils {
userThreadLocal.set(user);
}
//清除线程变量
//清除userThreadLocal线程变量
public static void removeUser() {
userThreadLocal.remove();
}
//为当前的线程变量赋值上uri信息
public static void setUri(String uri) {
uriThreadLocal.set(uri);
}
/**
* 获取当前访问方法的uri
* @return
*/
public static String getUri() {
String uri = uriThreadLocal.get();
return uri;
}
//清除uriThreadLocal线程变量
public static void removeUri() {
uriThreadLocal.remove();
}
}
......@@ -2,6 +2,8 @@ package iot.sixiang.license.log;
import com.alibaba.fastjson.JSONObject;
import iot.sixiang.license.entity.SysOperLog;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.jwt.UserUtils;
import iot.sixiang.license.service.SysOperLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
......@@ -67,16 +69,16 @@ public class LogAspect {
operLog.setOperTime(new Date());
if (e != null) {
operLog.setStatus(1);
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
operLog.setErrorMsg(StringUtils.substring(((IotLicenseException) e).getMsg(), 0, 2000));
}
// 设置方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
operLog.setMethod(className + "." + methodName + "()");
String uri = UserUtils.getUri();
operLog.setUri(uri);
// operLog.setMethod(className + "." + methodName + "()");
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, operLog);
// 保存数据库
sysOperLogService.addOperlog(operLog.getTitle(), operLog.getBusinessType(), operLog.getMethod(), operLog.getStatus(), operLog.getOptParam(), operLog.getErrorMsg(), operLog.getOperTime());
sysOperLogService.addOperlog(operLog.getTitle(), operLog.getBusinessType(), operLog.getUri(), operLog.getStatus(), operLog.getOptParam(), operLog.getErrorMsg(), operLog.getOperTime());
} catch (Exception exp) {
log.error("==前置通知异常==");
log.error("日志异常信息 {}", exp);
......
package iot.sixiang.license.mapper;
import iot.sixiang.license.entity.SysOperLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import iot.sixiang.license.entity.SysOperLog;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
/**
* <p>
......@@ -16,5 +16,7 @@ import java.util.Date;
*/
public interface SysOperLogMapper extends BaseMapper<SysOperLog> {
boolean addOperlog(String title, Integer businessType, String method, Integer status, String optParam, String errorMsg, Date operTime);
boolean addOperlog(String title, Integer businessType, String uri, Integer status, String optParam, String errorMsg, Date operTime);
List<SysOperLog> getOperLogList();
}
package iot.sixiang.license.service;
import iot.sixiang.license.entity.SysOperLog;
import com.baomidou.mybatisplus.extension.service.IService;
import iot.sixiang.license.entity.SysOperLog;
import iot.sixiang.license.model.PageInfoModel;
import java.time.LocalDateTime;
import java.util.Date;
/**
......@@ -15,5 +15,7 @@ import java.util.Date;
* @since 2022-06-13
*/
public interface SysOperLogService extends IService<SysOperLog> {
boolean addOperlog(String title, Integer businessType, String method, Integer status, String optParam, String errorMsg, Date operTime);
boolean addOperlog(String title, Integer businessType, String uri, Integer status, String optParam, String errorMsg, Date operTime);
PageInfoModel<SysOperLog> getOperLogList(int pageNo, int pageSize);
}
package iot.sixiang.license.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import iot.sixiang.license.entity.Server;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.entity.SysOperLog;
import iot.sixiang.license.mapper.ServerMapper;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.mapper.SysOperLogMapper;
import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.service.SysOperLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import iot.sixiang.license.util.JsonUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* <p>
......@@ -30,8 +30,29 @@ public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOper
@Override
public boolean addOperlog(String title, Integer businessType, String method, Integer status, String optParam, String errorMsg, Date operTime) {
return sysOperLogMapper.addOperlog(title, businessType, method, status, optParam, errorMsg, operTime);
public boolean addOperlog(String title, Integer businessType, String uri, Integer status, String optParam, String errorMsg, Date operTime) {
return sysOperLogMapper.addOperlog(title, businessType, uri, status, optParam, errorMsg, operTime);
}
@Override
public PageInfoModel<SysOperLog> getOperLogList(int pageNo, int pageSize) {
if(pageNo == 0) {
throw new IotLicenseException(400,"pageNo不能为空");
}
if(pageSize == 0) {
throw new IotLicenseException(400, "pageSize不能为空");
}
List<SysOperLog> records = sysOperLogMapper.getOperLogList();
records = records.stream().sorted(Comparator.comparing(SysOperLog::getId)).collect(Collectors.toList());
List<SysOperLog> result = new ArrayList<>();
int begin = (pageNo - 1) * pageSize;
if (begin >= 0 && records.size() > 0) {
result = records.stream().skip(begin).limit(pageSize).collect(Collectors.toList());
}
PageInfoModel<SysOperLog> objectPageInfoModel = new PageInfoModel<>();
objectPageInfoModel.setTotal(records.size());
objectPageInfoModel.setResult(result);
return objectPageInfoModel;
}
}
......@@ -2,6 +2,10 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="iot.sixiang.license.mapper.SysOperLogMapper">
<insert id="addOperlog" parameterType="iot.sixiang.license.entity.SysOperLog">
insert into sys_oper_log(title, business_type, method, status, opt_param, error_msg, oper_time) values (#{title},#{businessType},#{method},#{status},#{optParam},#{errorMsg},#{operTime})
insert into sys_oper_log(title, business_type, uri, status, opt_param, error_msg, oper_time) values (#{title},#{businessType},#{uri},#{status},#{optParam},#{errorMsg},#{operTime})
</insert>
<select id="getOperLogList" resultType="iot.sixiang.license.entity.SysOperLog">
select id, title, business_type, uri, status, opt_param, error_msg, oper_time from sys_oper_log
</select>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment