审计日志相关代码提交

This commit is contained in:
2022-07-13 20:16:32 +08:00
parent 783e75e341
commit 128518989f
19 changed files with 100 additions and 46 deletions

View File

@@ -3,8 +3,11 @@ package com.njcn.auth.controller;
import com.nimbusds.jose.jwk.JWKSet; import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey; import com.nimbusds.jose.jwk.RSAKey;
import com.njcn.auth.service.UserTokenService; import com.njcn.auth.service.UserTokenService;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.constant.SecurityConstants; import com.njcn.common.pojo.constant.SecurityConstants;
import com.njcn.common.pojo.dto.UserTokenInfo; import com.njcn.common.pojo.dto.UserTokenInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum; import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult; import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil; import com.njcn.common.utils.HttpResultUtil;
@@ -59,7 +62,10 @@ public class AuthController extends BaseController {
private final UserTokenService userTokenService; private final UserTokenService userTokenService;
@ApiIgnore @ApiIgnore
@OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.AUTHENTICATE)
@ApiOperation("登录认证") @ApiOperation("登录认证")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = SecurityConstants.GRANT_TYPE, defaultValue = "password", value = "授权模式", required = true), @ApiImplicitParam(name = SecurityConstants.GRANT_TYPE, defaultValue = "password", value = "授权模式", required = true),
@@ -88,7 +94,7 @@ public class AuthController extends BaseController {
//用户的登录名&密码校验成功后,判断当前该用户是否可以正常使用系统 //用户的登录名&密码校验成功后,判断当前该用户是否可以正常使用系统
userFeignClient.judgeUserStatus(username); userFeignClient.judgeUserStatus(username);
//登录成功后记录token信息并处理踢人效果 //登录成功后记录token信息并处理踢人效果
userTokenService.recordUserInfo(oAuth2AccessToken); userTokenService.recordUserInfo(oAuth2AccessToken,RequestUtil.getRealIp());
if (!grantType.equalsIgnoreCase(SecurityConstants.PASSWORD)) { if (!grantType.equalsIgnoreCase(SecurityConstants.PASSWORD)) {
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, oAuth2AccessToken, methodDescribe); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, oAuth2AccessToken, methodDescribe);
} else { } else {

View File

@@ -42,11 +42,13 @@ public class AuthExceptionHandler {
@ExceptionHandler(InvalidGrantException.class) @ExceptionHandler(InvalidGrantException.class)
public HttpResult<String> handleInvalidGrantException(HttpServletRequest httpServletRequest, InvalidGrantException invalidGrantException) { public HttpResult<String> handleInvalidGrantException(HttpServletRequest httpServletRequest, InvalidGrantException invalidGrantException) {
String loginName = invalidGrantException.getMessage(); String loginName = invalidGrantException.getMessage();
logService.recodeAuthExceptionLog(invalidGrantException, httpServletRequest, UserResponseEnum.LOGIN_WRONG_PWD.getMessage(), loginName);
HttpResult<String> result = userFeignClient.updateUserLoginErrorTimes(loginName); HttpResult<String> result = userFeignClient.updateUserLoginErrorTimes(loginName);
if (result.getData().equals(UserResponseEnum.LOGIN_USER_LOCKED.getMessage())) { if (result.getData().equals(UserResponseEnum.LOGIN_USER_LOCKED.getMessage())) {
logService.recodeAuthExceptionLog(invalidGrantException, httpServletRequest, UserResponseEnum.LOGIN_USER_LOCKED.getMessage(), loginName);
return HttpResultUtil.assembleResult(UserResponseEnum.LOGIN_USER_LOCKED.getCode(), null, UserResponseEnum.LOGIN_USER_LOCKED.getMessage()); return HttpResultUtil.assembleResult(UserResponseEnum.LOGIN_USER_LOCKED.getCode(), null, UserResponseEnum.LOGIN_USER_LOCKED.getMessage());
} else { } else {
logService.recodeAuthExceptionLog(invalidGrantException, httpServletRequest, UserResponseEnum.LOGIN_WRONG_PWD.getMessage(), loginName);
return HttpResultUtil.assembleResult(UserResponseEnum.LOGIN_WRONG_PWD.getCode(), null, UserResponseEnum.LOGIN_WRONG_PWD.getMessage()); return HttpResultUtil.assembleResult(UserResponseEnum.LOGIN_WRONG_PWD.getCode(), null, UserResponseEnum.LOGIN_WRONG_PWD.getMessage());
} }
} }

View File

@@ -2,11 +2,16 @@ package com.njcn.auth.service;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.github.tocrhz.mqtt.publisher.MqttPublisher;
import com.nimbusds.jose.JWSObject; import com.nimbusds.jose.JWSObject;
import com.njcn.common.config.GeneralInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.constant.SecurityConstants; import com.njcn.common.pojo.constant.SecurityConstants;
import com.njcn.common.pojo.dto.LogInfoDTO;
import com.njcn.common.pojo.dto.UserTokenInfo; import com.njcn.common.pojo.dto.UserTokenInfo;
import com.njcn.common.pojo.enums.response.CommonResponseEnum; import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException; import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.PubUtils;
import com.njcn.redis.utils.RedisUtil; import com.njcn.redis.utils.RedisUtil;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -33,6 +38,10 @@ public class UserTokenService {
private final RedisUtil redisUtil; private final RedisUtil redisUtil;
private final MqttPublisher publisher;
private final GeneralInfo generalInfo;
/** /**
* 记录用户token信息并经过处理后达到最新登录的使用者将之前的token信息置为黑名单过期状态 * 记录用户token信息并经过处理后达到最新登录的使用者将之前的token信息置为黑名单过期状态
@@ -45,7 +54,7 @@ public class UserTokenService {
* @param oAuth2AccessToken 认证后的最新token信息 * @param oAuth2AccessToken 认证后的最新token信息
*/ */
@Async("asyncExecutor") @Async("asyncExecutor")
public void recordUserInfo(OAuth2AccessToken oAuth2AccessToken) { public void recordUserInfo(OAuth2AccessToken oAuth2AccessToken,String ip) {
UserTokenInfo userTokenInfo = new UserTokenInfo(); UserTokenInfo userTokenInfo = new UserTokenInfo();
String accessTokenValue = oAuth2AccessToken.getValue(); String accessTokenValue = oAuth2AccessToken.getValue();
JWSObject accessJwsObject ; JWSObject accessJwsObject ;
@@ -56,6 +65,7 @@ public class UserTokenService {
} }
JSONObject accessJson = JSONUtil.parseObj(accessJwsObject.getPayload().toString()); JSONObject accessJson = JSONUtil.parseObj(accessJwsObject.getPayload().toString());
String userIndex = accessJson.getStr(SecurityConstants.USER_INDEX_KEY); String userIndex = accessJson.getStr(SecurityConstants.USER_INDEX_KEY);
String nickName = accessJson.getStr(SecurityConstants.USER_NICKNAME_KEY);
//查询是否有在线的当前用户 //查询是否有在线的当前用户
String onlineUserKey = SecurityConstants.TOKEN_ONLINE_PREFIX + userIndex; String onlineUserKey = SecurityConstants.TOKEN_ONLINE_PREFIX + userIndex;
Object onlineTokenInfoOld = redisUtil.getObjectByKey(onlineUserKey); Object onlineTokenInfoOld = redisUtil.getObjectByKey(onlineUserKey);
@@ -91,6 +101,10 @@ public class UserTokenService {
userTokenInfo.setRefreshTokenExpire(refreshLifeTime); userTokenInfo.setRefreshTokenExpire(refreshLifeTime);
//生命周期在refreshToken的基础上延迟5分钟 //生命周期在refreshToken的基础上延迟5分钟
redisUtil.saveByKeyWithExpire(onlineUserKey, userTokenInfo, refreshLifeTime.plusMinutes(5L).toEpochSecond(ZoneOffset.of("+8")) - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"))); redisUtil.saveByKeyWithExpire(onlineUserKey, userTokenInfo, refreshLifeTime.plusMinutes(5L).toEpochSecond(ZoneOffset.of("+8")) - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
//记录成功登录后的信息
LogInfoDTO logInfoDTO = new LogInfoDTO(nickName, ip, "登录认证", OperateType.AUTHENTICATE, 1, "",0, 1, generalInfo.getMicroServiceName(), userIndex);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false);
} }
/** /**

View File

@@ -51,6 +51,6 @@ mybatis-plus:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@ client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs

View File

@@ -7,16 +7,16 @@ package com.njcn.common.pojo.constant;
*/ */
public interface LogInfo { public interface LogInfo {
String UNKNOWN_OPERATE = "未知业务"; String UNKNOWN_OPERATE = "unknown operate";
String UNKNOWN_USER = "未知用户"; String UNKNOWN_USER = "unknown user";
String UNKNOWN_IP = "未知IP地址"; String UNKNOWN_IP = "unknown IP";
String UNKNOWN_CLIENT = "未知客户端"; String UNKNOWN_CLIENT = "unknown client";
String UNKNOWN_SERVER = "未知服务"; String UNKNOWN_SERVER = "unknown sever";
String UNKNOWN_DEPT = "未知部门"; String UNKNOWN_DEPT = "unknown department";
} }

View File

@@ -34,6 +34,11 @@ public class LogInfoDTO implements Serializable {
*/ */
private Integer result; private Integer result;
/**
* 失败原因
*/
private String failReason;
/** /**
* 严重度 0.普通 1.中等 2.严重 * 严重度 0.普通 1.中等 2.严重
*/ */
@@ -49,6 +54,6 @@ public class LogInfoDTO implements Serializable {
/** /**
* 0 未登录; 1 已登录 * 0 未登录; 1 已登录
*/ */
private Integer isLogin; private String userIndex;
} }

View File

@@ -1,9 +1,5 @@
package com.njcn.web.service.impl; package com.njcn.web.service.impl;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tocrhz.mqtt.publisher.MqttPublisher; import com.github.tocrhz.mqtt.publisher.MqttPublisher;
import com.njcn.common.config.GeneralInfo; import com.njcn.common.config.GeneralInfo;
import com.njcn.common.pojo.constant.LogInfo; import com.njcn.common.pojo.constant.LogInfo;
@@ -19,14 +15,11 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter; import org.springframework.core.MethodParameter;
import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpRequest;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@@ -65,20 +58,21 @@ public class LogServiceImpl implements ILogService {
public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) { public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) {
//处理审计日志 //处理审计日志
String userName; String userName;
int isLogin = 0; String userIndex ;
HttpServletRequest httpServletRequest = RequestUtil.getRequest(request); HttpServletRequest httpServletRequest = RequestUtil.getRequest(request);
if (UN_LOGIN_METHOD.contains(methodDescribe)) { if (UN_LOGIN_METHOD.contains(methodDescribe)) {
userName = RequestUtil.getLoginName(httpServletRequest); userName = RequestUtil.getLoginName(httpServletRequest);
userIndex = userName;
} else { } else {
userName = RequestUtil.getUserNickname(request); userName = RequestUtil.getUserNickname(request);
isLogin = 1; userIndex = RequestUtil.getUserIndex(request);
} }
String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage(); String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage();
String ip = RequestUtil.getRealIp(request); String ip = RequestUtil.getRealIp(request);
String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType(); String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel(); String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod()); String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod());
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin); LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, "",levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false);
} }
@@ -95,10 +89,12 @@ public class LogServiceImpl implements ILogService {
public void recodeBusinessExceptionLog(Exception exception, HttpServletRequest request, String message) { public void recodeBusinessExceptionLog(Exception exception, HttpServletRequest request, String message) {
LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request); LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request);
//认证前,获取用户信息 //认证前,获取用户信息
int isLogin = 1; String userIndex;
if (Objects.equals(tempLogInfo.getUserName(), LogInfo.UNKNOWN_USER)) { if (Objects.equals(tempLogInfo.getUserName(), LogInfo.UNKNOWN_USER)) {
tempLogInfo.setUserName(RequestUtil.getLoginName(request)); tempLogInfo.setUserName(RequestUtil.getLoginName(request));
isLogin = 0; userIndex = tempLogInfo.getUserName();
}else{
userIndex = RequestUtil.getUserIndex(request);
} }
//根据异常获取method方法 //根据异常获取method方法
Method method = ReflectCommonUtil.getMethod(exception); Method method = ReflectCommonUtil.getMethod(exception);
@@ -106,12 +102,11 @@ public class LogServiceImpl implements ILogService {
MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) exception; MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) exception;
method = methodArgumentNotValidException.getParameter().getMethod(); method = methodArgumentNotValidException.getParameter().getMethod();
} }
String methodDescribe = StrFormatter.format("{}{}{}", ReflectCommonUtil.getMethodDescribeByMethod(method), StrUtil.C_COMMA, message);
String result = CommonResponseEnum.FAIL.getMessage(); String result = CommonResponseEnum.FAIL.getMessage();
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType(); String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel(); String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method); String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin); LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1,message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
} }
@@ -132,13 +127,12 @@ public class LogServiceImpl implements ILogService {
MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) exception; MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) exception;
method = methodArgumentNotValidException.getParameter().getMethod(); method = methodArgumentNotValidException.getParameter().getMethod();
} }
String methodDescribe = StrFormatter.format("{}{}{}", ReflectCommonUtil.getMethodDescribeByMethod(method), StrUtil.C_COMMA, message);
String result = CommonResponseEnum.FAIL.getMessage(); String result = CommonResponseEnum.FAIL.getMessage();
String ip = RequestUtil.getRealIp(request); String ip = RequestUtil.getRealIp(request);
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType(); String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel(); String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method); String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), 0); LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1,message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userName);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
} }

View File

@@ -26,6 +26,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
import java.util.List;
import java.util.Objects; import java.util.Objects;
/** /**
@@ -70,7 +71,6 @@ public class RequestUtil {
String refreshToken = request.getParameter(SecurityConstants.REFRESH_TOKEN_KEY); String refreshToken = request.getParameter(SecurityConstants.REFRESH_TOKEN_KEY);
String payload = StrUtil.toString(JWSObject.parse(refreshToken).getPayload()); String payload = StrUtil.toString(JWSObject.parse(refreshToken).getPayload());
cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(payload); cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(payload);
String authenticationMethod = jsonObject.getStr(SecurityConstants.AUTHENTICATION_METHOD); String authenticationMethod = jsonObject.getStr(SecurityConstants.AUTHENTICATION_METHOD);
if (StrUtil.isBlank(authenticationMethod)) { if (StrUtil.isBlank(authenticationMethod)) {
authenticationMethod = AuthenticationMethodEnum.USERNAME.getValue(); authenticationMethod = AuthenticationMethodEnum.USERNAME.getValue();
@@ -132,8 +132,8 @@ public class RequestUtil {
*/ */
public static JSONObject getJwtPayload() { public static JSONObject getJwtPayload() {
JSONObject jsonObject = null; JSONObject jsonObject = null;
String jwtPayload = getRequest().getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
try { try {
String jwtPayload = getRequest().getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
if (StrUtil.isNotBlank(jwtPayload)) { if (StrUtil.isNotBlank(jwtPayload)) {
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString()); jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
jsonObject = JSONObject.fromObject(jwtPayload); jsonObject = JSONObject.fromObject(jwtPayload);
@@ -150,8 +150,8 @@ public class RequestUtil {
*/ */
public static JSONObject getJwtPayload(HttpServletRequest request) { public static JSONObject getJwtPayload(HttpServletRequest request) {
JSONObject jsonObject = null; JSONObject jsonObject = null;
String jwtPayload = request.getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
try { try {
String jwtPayload = request.getHeader(SecurityConstants.JWT_PAYLOAD_KEY);
if (StrUtil.isNotBlank(jwtPayload)) { if (StrUtil.isNotBlank(jwtPayload)) {
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString()); jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
jsonObject = JSONObject.fromObject(jwtPayload); jsonObject = JSONObject.fromObject(jwtPayload);
@@ -170,8 +170,8 @@ public class RequestUtil {
JSONObject jsonObject = null; JSONObject jsonObject = null;
HttpHeaders headers = request.getHeaders(); HttpHeaders headers = request.getHeaders();
try { try {
if (CollectionUtils.isNotEmpty(headers.get(SecurityConstants.JWT_PAYLOAD_KEY))) {
String jwtPayload = headers.get(SecurityConstants.JWT_PAYLOAD_KEY).get(0); String jwtPayload = headers.get(SecurityConstants.JWT_PAYLOAD_KEY).get(0);
if (StrUtil.isNotBlank(jwtPayload)) {
jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString()); jwtPayload = URLDecoder.decode(jwtPayload, StandardCharsets.UTF_8.toString());
jsonObject = JSONObject.fromObject(jwtPayload); jsonObject = JSONObject.fromObject(jwtPayload);
} }
@@ -182,6 +182,29 @@ public class RequestUtil {
return jsonObject; return jsonObject;
} }
/**
* HttpServletRequest获取在网关中存储的用户索引
*/
public static String getUserIndex(ServerHttpRequest request) {
String userIndex = LogInfo.UNKNOWN_USER;
JSONObject jwtPayload = getJwtPayload(request);
if (Objects.nonNull(jwtPayload)) {
userIndex = jwtPayload.getString(SecurityConstants.USER_INDEX_KEY);
}
return userIndex;
}
/**
* HttpServletRequest获取在网关中存储的用户索引
*/
public static String getUserIndex(HttpServletRequest request) {
String userIndex = LogInfo.UNKNOWN_USER;
JSONObject jwtPayload = getJwtPayload(request);
if (Objects.nonNull(jwtPayload)) {
userIndex = jwtPayload.getString(SecurityConstants.USER_INDEX_KEY);
}
return userIndex;
}
/** /**
* HttpServletRequest获取在网关中存储的用户索引 * HttpServletRequest获取在网关中存储的用户索引

View File

@@ -58,7 +58,7 @@ mybatis-plus:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@ client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs

View File

@@ -50,6 +50,6 @@ mybatis-plus:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@_cdf client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs

View File

@@ -50,7 +50,7 @@ mybatis-plus:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@ client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs

View File

@@ -135,7 +135,7 @@ whitelist:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@ client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs

View File

@@ -50,7 +50,7 @@ mybatis-plus:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@ client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs

View File

@@ -52,6 +52,6 @@ mybatis-plus:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@ client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs

View File

@@ -48,6 +48,11 @@ public class UserLog extends BaseEntity {
*/ */
private Integer result; private Integer result;
/**
* 失败原因
*/
private String failReason;
/** /**
* 严重度 0.普通 1.中等 2.严重 * 严重度 0.普通 1.中等 2.严重
*/ */

View File

@@ -29,10 +29,8 @@ public class UserLogServiceImpl extends ServiceImpl<UserLogMapper, UserLog> impl
public void addUserLog(LogInfoDTO logInfoDTO) { public void addUserLog(LogInfoDTO logInfoDTO) {
UserLog userLog = new UserLog(); UserLog userLog = new UserLog();
BeanUtil.copyProperties(logInfoDTO, userLog); BeanUtil.copyProperties(logInfoDTO, userLog);
if(logInfoDTO.getIsLogin() == 0){ userLog.setCreateBy(logInfoDTO.getUserIndex());
userLog.setCreateBy(userLog.getUserName()); userLog.setUpdateBy(logInfoDTO.getUserIndex());
userLog.setUpdateBy(userLog.getUserName());
}
userLog.setState(0); userLog.setState(0);
this.baseMapper.insert(userLog); this.baseMapper.insert(userLog);
} }

View File

@@ -51,6 +51,6 @@ mybatis-plus:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@ client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs

View File

@@ -89,6 +89,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
} }
//超级管理员则不做任何逻辑判断 //超级管理员则不做任何逻辑判断
if (user.getType() == 0) { if (user.getType() == 0) {
//更新用户登录时间,以及错误登录记录的信息归零。
user.setState(UserState.ENABLE);
user.setLoginErrorTimes(0);
user.setLoginTime(LocalDateTime.now());
user.setFirstErrorTime(null);
user.setLockTime(null);
this.baseMapper.updateById(user);
return; return;
} }
//根据用户类型获取对应用户策略 //根据用户类型获取对应用户策略

View File

@@ -51,6 +51,6 @@ mybatis-plus:
mqtt: mqtt:
uri: tcp://@middle.server.url@:1883 uri: tcp://@middle.server.url@:1883
client-id: @artifactId@ client-id: @artifactId@${random.value}
username: t_user username: t_user
password: njcnpqs password: njcnpqs