将所有delete 方法修改为post

This commit is contained in:
weiweiw 2024-11-08 15:15:11 +08:00
parent 2a8f87024b
commit 511fc3e312
11 changed files with 325 additions and 22 deletions

View File

@ -149,6 +149,10 @@ public class SysLogsVo {
private String type; private String type;
private String capacity; private String capacity;
/**
* 0未处理1已处理
*/
private String warningStatus;
/** /**
* 越权记录 * 越权记录

View File

@ -15,6 +15,7 @@ import java.util.List;
@ConfigurationProperties(prefix = "system-config") @ConfigurationProperties(prefix = "system-config")
@Data @Data
public class SystemConfig { public class SystemConfig {
/** /**
* 登录配置 * 登录配置
*/ */
@ -44,6 +45,11 @@ public class SystemConfig {
*/ */
private PasswordConfig passwordConfig; private PasswordConfig passwordConfig;
/**
* websocketUrl
*/
private String webSocketurl;
@Data @Data
@RefreshScope @RefreshScope
public static class LoginConfig { public static class LoginConfig {

View File

@ -1,5 +1,7 @@
package com.bonus.system.service.impl; package com.bonus.system.service.impl;
import com.bonus.system.warning.SysWarning;
import com.bonus.system.warning.WaringLogEvent;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.bonus.common.core.utils.DateUtils; import com.bonus.common.core.utils.DateUtils;
@ -15,6 +17,8 @@ import com.bonus.system.api.domain.SysLogsVo;
import com.bonus.system.api.model.LoginUser; import com.bonus.system.api.model.LoginUser;
import com.bonus.system.mapper.SysLogMapper; import com.bonus.system.mapper.SysLogMapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.transaction.interceptor.TransactionAspectSupport;
@ -24,7 +28,7 @@ import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.bonus.common.log.enums.LogType;
/** /**
* @authorcwchen * @authorcwchen
* @date2024-02-28-14:02 * @date2024-02-28-14:02
@ -38,16 +42,13 @@ public class SysLogServiceImpl implements ISysLogService {
@Resource(name = "SysLogMapper") @Resource(name = "SysLogMapper")
private SysLogMapper mapper; private SysLogMapper mapper;
@Autowired
private ApplicationEventPublisher eventPublisher;
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public AjaxResult saveLogs(SysLogsVo sysLog) { public AjaxResult saveLogs(SysLogsVo sysLog) {
try { try {
//weiweiw
// if(sysLog.getResult() == SystemGlobal.SUCCESS_NUM){
// sysLog.setFruit("成功");
// }else {
// sysLog.setFruit("失败");
// }
//如果是异常日志 //如果是异常日志
if(SystemGlobal.LOG_ERR.equals(sysLog.getErrType()) && StringUtils.isEmpty(sysLog.getModel())) { if(SystemGlobal.LOG_ERR.equals(sysLog.getErrType()) && StringUtils.isEmpty(sysLog.getModel())) {
SysLogsVo sysLog1=mapper.getModule(sysLog.getOperUri()); SysLogsVo sysLog1=mapper.getModule(sysLog.getOperUri());
@ -61,6 +62,7 @@ public class SysLogServiceImpl implements ISysLogService {
} }
} }
mapper.saveLogs(sysLog); mapper.saveLogs(sysLog);
eventPublisher.publishEvent(new WaringLogEvent(new SysWarning(sysLog.getLogId(),"测试系统日志告警","",sysLog.getIp(),sysLog.getGrade(),sysLog.getOperaUserName())));
} catch (Exception e) { } catch (Exception e) {
log.error("保存系统日志"); log.error("保存系统日志");
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
@ -72,8 +74,10 @@ public class SysLogServiceImpl implements ISysLogService {
public void saveLogs(SysLogsVo sysLog, HttpServletRequest request) { public void saveLogs(SysLogsVo sysLog, HttpServletRequest request) {
try{ try{
// sysLog.setFruit("失败"); // sysLog.setFruit("失败");
sysLog.setLogId(IdUtils.fastUUID()); String loginUuid = IdUtils.fastUUID();
sysLog.setIp(IpUtils.getIpAddr(request)); String ip = IpUtils.getIpAddr(request);
sysLog.setLogId(loginUuid);
sysLog.setIp(ip);
sysLog.setGrade(""); sysLog.setGrade("");
sysLog.setErrType("越权访问"); sysLog.setErrType("越权访问");
sysLog.setFailureReason("页面未授权"); sysLog.setFailureReason("页面未授权");
@ -95,6 +99,7 @@ public class SysLogServiceImpl implements ISysLogService {
} }
mapper.saveLogs(sysLog); mapper.saveLogs(sysLog);
} }
eventPublisher.publishEvent(new WaringLogEvent(new SysWarning(loginUuid,"越权访问","",ip,"",user.getUsername())));
}catch (Exception e){ }catch (Exception e){
log.error(e.toString(),e); log.error(e.toString(),e);
} }

View File

@ -9,7 +9,11 @@ import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.log.enums.OperaType; import com.bonus.common.log.enums.OperaType;
import com.bonus.system.api.domain.SysLogsVo; import com.bonus.system.api.domain.SysLogsVo;
import com.bonus.system.domain.SysLogsMenuHead; import com.bonus.system.domain.SysLogsMenuHead;
import com.bonus.system.warning.SysWarning;
import com.bonus.system.warning.WaringLogEvent;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.bonus.system.api.domain.SysOperLog; import com.bonus.system.api.domain.SysOperLog;
import com.bonus.system.mapper.SysOperLogMapper; import com.bonus.system.mapper.SysOperLogMapper;
@ -30,6 +34,9 @@ public class SysOperLogServiceImpl implements ISysOperLogService
private SysOperLogMapper operLogMapper; private SysOperLogMapper operLogMapper;
public static final String MENU_TYPE_FILE = "F"; public static final String MENU_TYPE_FILE = "F";
@Autowired
private ApplicationEventPublisher eventPublisher;
/** /**
* 新增操作日志 * 新增操作日志
* *
@ -94,6 +101,7 @@ public class SysOperLogServiceImpl implements ISysOperLogService
*/ */
@Override @Override
public int addLogs(SysLogsVo sysLogsVo) { public int addLogs(SysLogsVo sysLogsVo) {
eventPublisher.publishEvent(new WaringLogEvent(new SysWarning(sysLogsVo.getLogId(),"测试系统日志告警","",sysLogsVo.getIp(),sysLogsVo.getGrade(),sysLogsVo.getOperaUserName())));
return operLogMapper.addLogs(sysLogsVo); return operLogMapper.addLogs(sysLogsVo);
} }

View File

@ -0,0 +1,33 @@
package com.bonus.system.warning;
import lombok.Data;
import java.util.Date;
@Data
public class SysWarning {
public SysWarning(String warningId,String warningEvent, String warningContent,String warningIp,String warningGrade, String operaUserName ){
this.warningId = warningId;
this.warningEvent = warningEvent;
this.warningContent = warningContent;
this.warningIp = warningIp;
this.warningGrade = warningGrade;
this.operaUserName = operaUserName;
}
private String warningId;
private String warningEvent = "";
private String warningContent = "";
private String warningIp = "";
private String warningGrade = "";
private String operaUserName = "";
private Date warningTime;
private String warningStatus = "0";
}

View File

@ -0,0 +1,20 @@
package com.bonus.system.warning;
import org.springframework.context.ApplicationEvent;
public class WaringLogEvent extends ApplicationEvent {
private final SysWarning sysWarning;
public WaringLogEvent(SysWarning logEvent) {
super(logEvent);
this.sysWarning = logEvent;
}
public SysWarning getSysWarning(){
return this.sysWarning;
}
}

View File

@ -0,0 +1,52 @@
package com.bonus.system.warning;
import com.bonus.system.service.ISysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;
import javax.annotation.Resource;
import java.io.IOException;
@Component
@Slf4j
public class WarningListen {
@Autowired
private SimpMessagingTemplate messagingTemplate;
@Resource(name = "ISysLogService")
private ISysLogService service;
public static void closeSession(WebSocketSession session) throws IOException {
session.close();
}
@EventListener
public void onLogSaved(WaringLogEvent event) {
try {
SysWarning warning = event.getSysWarning();
sendAlert(warning);
} catch (Exception e) {
log.error("处理日志告警失败", e);
}
}
// 发送告警消息
public void sendAlert(SysWarning alert) {
messagingTemplate.convertAndSend("/topic/alerts", alert);
log.info("*************************告警消息已发送:{}", alert);
}
// 处理用户确认消息
@MessageMapping("/alert-handled")
public void handleAlert(String alertId) {
// 在这里处理告警确认逻辑
System.out.println("Alert " + alertId + " has been handled");
}
}

View File

@ -0,0 +1,31 @@
package com.bonus.system.warning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.config.annotation.*;
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer, WebSocketConfigurer {
@Autowired
private WebSocketHandler webSocketHandler;
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-alert").withSockJS();
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler, "/ws-alert")
.setAllowedOrigins("*"); // 允许跨域
}
}

View File

@ -0,0 +1,40 @@
package com.bonus.system.warning;
import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.concurrent.CopyOnWriteArrayList;
@Component
public class WebSocketHandler extends TextWebSocketHandler {
private static final Logger logger = LoggerFactory.getLogger(WebSocketHandler.class);
private static final CopyOnWriteArrayList<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
sessions.add(session);
logger.info("WebSocket 连接成功: " + session.getId());
}
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String payload = message.getPayload();
logger.info("接收到消息: " + payload);
// 处理接收到的消息例如广播给所有客户端
for (WebSocketSession wsSession : sessions) {
if (wsSession.isOpen()) {
wsSession.sendMessage(new TextMessage("服务端回应: " + payload));
}
}
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
sessions.remove(session);
logger.info("WebSocket 连接关闭: " + session.getId());
}
}

View File

@ -5,21 +5,56 @@
<!--保存系统日志--> <!--保存系统日志-->
<insert id="saveLogs" parameterType="SysLogsVo"> <insert id="saveLogs" parameterType="SysLogsVo">
insert into sys_logs( insert into sys_logs(
log_id,opera_user_name,ip,user_id, <if test=" logId!= null and logId != ''">log_id,</if>
model,oper_time,method,params, <if test=" operaUserName!= null">opera_user_name,</if>
operate_detail,oper_type,oper_uri, <if test=" ip!= null">ip,</if>
log_type,result,times, <if test=" userId!= null and userId != 0">user_id,</if>
failure_reason,grade,err_type, <if test=" model!= null">model,</if>
method_type,title,result_data <if test=" operTime!= null">oper_time,</if>
<if test=" method!= null">method,</if>
<if test=" params!= null">params,</if>
<if test=" operateDetail!= null">operate_detail,</if>
<if test=" operaType!= null">oper_type,</if>
<if test=" operUri!= null">oper_uri,</if>
<if test=" logType!= null">log_type,</if>
<if test=" result!= null">result,</if>
<if test=" times!= null">times,</if>
<if test=" failureReason!= null">failure_reason,</if>
<if test=" grade!= null">grade,</if>
<if test=" errType!= null">err_type,</if>
<if test=" methodType!= null">method_type,</if>
<if test=" title!= null">title,</if>
<if test=" resultData!= null">result_data,</if>
<if test=" warningStatus!= null">warning_status,</if>
)values ( )values (
#{logId},#{operaUserName},#{ip},#{userId}, <if test=" logId!= null and logId != ''">{logId},</if>
#{model},#{operTime},#{method},#{params}, <if test=" operaUserName!= null">{operaUserName},</if>
#{operateDetail},#{operaType},#{operUri}, <if test=" ip!= null">{ip},</if>
#{logType},#{result},#{times}, <if test=" userId!= null and userId != 0">{userId},</if>
#{failureReason},#{grade},#{errType}, <if test=" model!= null">{model},</if>
#{methodType},#{title},#{resultData} <if test=" operTime!= null">{operTime},</if>
) <if test=" method!= null">{method},</if>
<if test=" params!= null">{params},</if>
<if test=" operateDetail!= null">{operateDetail},</if>
<if test=" operaType!= null">{operaType},</if>
<if test=" operUri!= null">{operUri},</if>
<if test=" logType!= null">{logType},</if>
<if test=" result!= null">{result},</if>
<if test=" times!= null">{times},</if>
<if test=" failureReason!= null">{failureReason},</if>
<if test=" grade!= null">{grade},</if>
<if test=" errType!= null">{errType},</if>
<if test=" methodType!= null">{methodType},</if>
<if test=" title!= null">{title},</if>
<if test=" resultData!= null">{resultData},</if>
<if test=" warningStatus!= null">{warningStatus},</if>
</insert> </insert>
<update id="updateWarningStatus" parameterType="SysLogsVo">
update sys_warning
set warning_status = #{warningStatus}
where log_id = #{logId}
</update>
<!-- <insert id="saveLogs">--> <!-- <insert id="saveLogs">-->
<!-- INSERT INTO sys_logs--> <!-- INSERT INTO sys_logs-->
<!-- <trim prefix="(" suffix=")" suffixOverrides=",">--> <!-- <trim prefix="(" suffix=")" suffixOverrides=",">-->

View File

@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.system.mapper.SysWarningMapper">
<resultMap id="SysWarningResult" type="com.bonus.system.warning.SysWarning">
<id property="warningId" column="warning_id" />
<result property="warningEvent" column="warning_event" />
<result property="warningContent" column="warning_content" />
<result property="warningIp" column="warning_ip" />
<result property="warningGrade" column="warning_grade" />
<result property="operaUserName" column="opera_user_name" />
<result property="warningTime" column="warning_time" />
<result property="warningStatus" column="warning_status" />
</resultMap>
<!-- 使用 resultMap 的查询操作 -->
<select id="selectWarningList" parameterType="com.bonus.system.warning.SysWarning" resultMap="SysWarningResult">
SELECT
warning_id,
warning_event,
warning_content,
warning_ip,
warning_grade,
opera_user_name,
warning_time,
warning_status
FROM sys_warning
where
<if test="warning_status != null and warning_status != 0">
AND u.warning_status = #{warningStatus}
</if>
</select>
<select id="selectWarningById" parameterType="Long" resultMap="SysWarningResult">
where u.warning_id = #{warningId}
</select>
<insert id="insertWarning" parameterType="com.bonus.system.warning.SysWarning" useGeneratedKeys="true" keyProperty="warningId">
insert into sys_warning(
<if test=" warningId!= null and warningId != 0">warning_id,</if>
<if test="warningEvent != null and warningEvent != ''">warning_event,</if>
<if test="warningContent != null and warningContent != ''">warning_content,</if>
<if test="warningIp != null and warningIp != ''">warning_ip,</if>
<if test="warningGrade != null and warningGrade != ''">warning_grade,</if>
<if test="operaUserName != null and operaUserName != ''">opera_user_name,</if>
warning_time,
<if test="warningStatus != null and warningStatus != ''">warning_status,</if>
)values(
<if test=" warningId!= null and warningId != ''">#{warningId},</if>
<if test="warningEvent != null and warningEvent != ''">#{warningEvent},</if>
<if test="warningContent != null and warningContent != ''">#{warningContent},</if>
<if test="warningIp != null and warningIp != ''">#{warningIp},</if>
<if test="warningGrade != null and warningGrade != ''">#{warningGrade},</if>
<if test="operaUserName != null and operaUserName != ''">#{operaUserName},</if>
sysdate,
<if test="warningStatus != null and warningStatus != ''">#{warningStatus},</if>
)
</insert>
<update id="updateWarningStatus" parameterType="com.bonus.system.warning.SysWarning">
update sys_warning
set warning_status = #{warningStatus}
where warning_id = #{warningId}
</update>
</mapper>