1. 增加 API 访问日志、异常日志表

2. 调整过滤器的顺序
pull/2/head
YunaiV 2021-02-26 01:07:57 +08:00
parent fe94e4590a
commit 70af2bc78c
15 changed files with 364 additions and 112 deletions

View File

@ -1,93 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 日志存放路径 -->
<property name="log.path" value="/home/ruoyi/logs" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-info.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-error.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 用户访问日志输出 -->
<appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-user.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.ruoyi" level="info" />
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn" />
<root level="info">
<appender-ref ref="console" />
</root>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
</root>
<!--系统用户操作日志-->
<logger name="sys-user" level="info">
<appender-ref ref="sys-user"/>
</logger>
</configuration>

View File

@ -0,0 +1,25 @@
package cn.iocoder.dashboard.common.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
*
*/
@AllArgsConstructor
@Getter
public enum UserTypeEnum {
MEMBER(1, "会员"), // 面向 c 端,普通用户
ADMIN(2, "管理员"); // 面向 b 端,管理后台
/**
*
*/
private final Integer value;
/**
*
*/
private final String name;
}

View File

@ -0,0 +1,30 @@
package cn.iocoder.dashboard.framework.logger.apilog.config;
import cn.iocoder.dashboard.framework.logger.apilog.core.filter.ApiAccessLogFilter;
import cn.iocoder.dashboard.framework.web.config.WebProperties;
import cn.iocoder.dashboard.framework.web.core.enums.FilterOrderEnum;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
@Configuration
public class ApiLogConfiguration {
/**
* ApiAccessLogFilter Bean API
*/
@Bean
public FilterRegistrationBean<ApiAccessLogFilter> apiAccessLogFilter(WebProperties webProperties) {
ApiAccessLogFilter filter = new ApiAccessLogFilter(webProperties);
return createFilterBean(filter, FilterOrderEnum.API_ACCESS_LOG_FILTER);
}
private static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
bean.setOrder(order);
return bean;
}
}

View File

@ -0,0 +1,71 @@
package cn.iocoder.dashboard.framework.logger.apilog.core.filter;
import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.dashboard.framework.logger.apilog.core.service.dto.ApiAccessLogCreateDTO;
import cn.iocoder.dashboard.framework.web.config.WebProperties;
import cn.iocoder.dashboard.util.servlet.ServletUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
/**
* API 访 Filter
*
* @author
*/
@AllArgsConstructor
@Slf4j
public class ApiAccessLogFilter extends OncePerRequestFilter {
private final WebProperties webProperties;
@Override
protected boolean shouldNotFilter(HttpServletRequest request) {
return !request.getRequestURI().startsWith(webProperties.getApiPrefix());
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
// 获得开始时间
Date startTime = new Date();
// 提前获得参数,避免 XssFilter 过滤处理
Map<String, String> queryString = ServletUtil.getParamMap(request);
String requestBody = ServletUtils.isJsonRequest(request) ? ServletUtil.getBody(request) : null;
try {
// 继续过滤器
filterChain.doFilter(request, response);
// 正常执行,记录日志
createApiAccessLog(request, startTime, queryString, requestBody, null);
} catch (Exception ex) {
// 异常执行,记录日志
createApiAccessLog(request, startTime, queryString, requestBody, ex);
throw ex;
}
}
private void createApiAccessLog(HttpServletRequest request, Date startTime,
Map<String, String> queryString, String requestBody, Exception ex) {
try {
ApiAccessLogCreateDTO createDTO = this.buildApiAccessLogDTO(request, startTime, queryString, requestBody, ex);
} catch (Exception e) {
log.error("[createApiAccessLog][url({}) 发生异常]", request.getRequestURI(), ex);
}
}
private ApiAccessLogCreateDTO buildApiAccessLogDTO(HttpServletRequest request, Date startTime,
Map<String, String> queryString, String requestBody, Exception ex) {
return null;
}
}

View File

@ -0,0 +1,82 @@
package cn.iocoder.dashboard.framework.logger.apilog.core.service.dto;
import javax.validation.constraints.NotNull;
import java.util.Date;
/**
* API 访 DTO
*
* @author
*/
public class ApiAccessLogCreateDTO {
/**
*
*/
private String traceId;
/**
*
*/
private Integer userId;
/**
*
*/
private Integer userType;
/**
*
*/
@NotNull(message = "应用名不能为空")
private String applicationName;
/**
*
*/
@NotNull(message = "http 请求方法不能为空")
private String requestMethod;
/**
* 访
*/
@NotNull(message = "访问地址不能为空")
private String requestUrl;
/**
*
*/
@NotNull(message = "请求参数不能为空")
private String requestParams;
/**
* IP
*/
@NotNull(message = "ip 不能为空")
private String userIp;
/**
* UA
*/
@NotNull(message = "User-Agent 不能为空")
private String userAgent;
/**
*
*/
@NotNull(message = "开始请求时间不能为空")
private Date startTime;
/**
*
*/
@NotNull(message = "结束请求时间不能为空")
private Date endTime;
/**
*
*/
@NotNull(message = "执行时长不能为空")
private Integer duration;
/**
*
*/
@NotNull(message = "错误码不能为空")
private Integer resultCode;
/**
*
*/
private String resultMsg;
}

View File

@ -0,0 +1 @@
package cn.iocoder.dashboard.framework.logger.apilog.core.service;

View File

@ -37,6 +37,5 @@ public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {
} }
// 返回成功 // 返回成功
ServletUtils.writeJSON(response, null); ServletUtils.writeJSON(response, null);
// ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.OK.value(), "退出成功")));
} }
} }

View File

@ -1,11 +1,12 @@
package cn.iocoder.dashboard.framework.web.config; package cn.iocoder.dashboard.framework.web.config;
import cn.iocoder.dashboard.framework.web.core.enums.FilterOrderEnum;
import cn.iocoder.dashboard.framework.web.core.filter.RequestBodyCacheFilter; import cn.iocoder.dashboard.framework.web.core.filter.RequestBodyCacheFilter;
import cn.iocoder.dashboard.framework.web.core.filter.XssFilter; import cn.iocoder.dashboard.framework.web.core.filter.XssFilter;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.util.PathMatcher; import org.springframework.util.PathMatcher;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
@ -15,10 +16,8 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.Filter;
/**
* Web
*/
@Configuration @Configuration
@EnableConfigurationProperties({WebProperties.class, XssProperties.class}) @EnableConfigurationProperties({WebProperties.class, XssProperties.class})
public class WebConfiguration implements WebMvcConfigurer { public class WebConfiguration implements WebMvcConfigurer {
@ -39,8 +38,7 @@ public class WebConfiguration implements WebMvcConfigurer {
* CorsFilter Bean * CorsFilter Bean
*/ */
@Bean @Bean
@Order(Integer.MIN_VALUE) public FilterRegistrationBean<CorsFilter> corsFilterBean() {
public CorsFilter corsFilter() {
// 创建 CorsConfiguration 对象 // 创建 CorsConfiguration 对象
CorsConfiguration config = new CorsConfiguration(); CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); config.setAllowCredentials(true);
@ -50,25 +48,29 @@ public class WebConfiguration implements WebMvcConfigurer {
// 创建 UrlBasedCorsConfigurationSource 对象 // 创建 UrlBasedCorsConfigurationSource 对象
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config); // 对接口配置跨域设置 source.registerCorsConfiguration("/**", config); // 对接口配置跨域设置
return new CorsFilter(source); return createFilterBean(new CorsFilter(source), FilterOrderEnum.CORS_FILTER);
} }
/** /**
* RequestBodyCacheFilter Bean * RequestBodyCacheFilter Bean
*/ */
@Bean @Bean
@Order(Integer.MIN_VALUE) public FilterRegistrationBean<RequestBodyCacheFilter> requestBodyCacheFilter() {
public RequestBodyCacheFilter requestBodyCacheFilter() { return createFilterBean(new RequestBodyCacheFilter(), FilterOrderEnum.REQUEST_BODY_CACHE_FILTER);
return new RequestBodyCacheFilter();
} }
/** /**
* XssFilter Bean Xss * XssFilter Bean Xss
*/ */
@Bean @Bean
@Order(Integer.MIN_VALUE + 1000) // 需要保证在 RequestBodyCacheFilter 后面 public FilterRegistrationBean<XssFilter> xssFilter(XssProperties properties, PathMatcher pathMatcher) {
public XssFilter xssFilter(XssProperties properties, PathMatcher pathMatcher) { return createFilterBean(new XssFilter(properties, pathMatcher), FilterOrderEnum.XSS_FILTER);
return new XssFilter(properties, pathMatcher); }
private static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
bean.setOrder(order);
return bean;
} }
} }

View File

@ -0,0 +1,22 @@
package cn.iocoder.dashboard.framework.web.core.enums;
/**
*
*
* @author
*/
public interface FilterOrderEnum {
int CORS_FILTER = Integer.MIN_VALUE;
int REQUEST_BODY_CACHE_FILTER = Integer.MIN_VALUE + 500;
// OrderedRequestContextFilter 默认为 -105用于国际化上下文等等
int API_ACCESS_LOG_FILTER = -104; // 需要保证在 RequestBodyCacheFilter 后面
int XSS_FILTER = -103; // 需要保证在 RequestBodyCacheFilter 后面
// Spring Security Filter 默认为 -100可见 SecurityProperties 配置属性类
}

View File

@ -1,7 +1,6 @@
package cn.iocoder.dashboard.framework.web.core.filter; package cn.iocoder.dashboard.framework.web.core.filter;
import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.util.servlet.ServletUtils;
import org.springframework.http.MediaType;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper; import org.springframework.web.util.ContentCachingRequestWrapper;
@ -29,7 +28,7 @@ public class RequestBodyCacheFilter extends OncePerRequestFilter {
@Override @Override
protected boolean shouldNotFilter(HttpServletRequest request) { protected boolean shouldNotFilter(HttpServletRequest request) {
// 只处理 json 请求内容 // 只处理 json 请求内容
return !StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE); return !ServletUtils.isJsonRequest(request);
} }
} }

View File

@ -6,6 +6,7 @@ import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HTMLFilter; import cn.hutool.http.HTMLFilter;
import cn.iocoder.dashboard.util.servlet.ServletUtils;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import javax.servlet.ReadListener; import javax.servlet.ReadListener;
@ -56,7 +57,7 @@ public class XssRequestWrapper extends HttpServletRequestWrapper {
@Override @Override
public ServletInputStream getInputStream() throws IOException { public ServletInputStream getInputStream() throws IOException {
// 如果非 json 请求,不进行 Xss 处理 // 如果非 json 请求,不进行 Xss 处理
if (!StrUtil.startWithIgnoreCase(super.getContentType(), MediaType.APPLICATION_JSON_VALUE)) { if (!ServletUtils.isJsonRequest(this)) {
return super.getInputStream(); return super.getInputStream();
} }

View File

@ -9,7 +9,7 @@ import lombok.*;
import java.util.Date; import java.util.Date;
/** /**
* sys_job_log *
* *
* @author * @author
*/ */

View File

@ -0,0 +1,103 @@
package cn.iocoder.dashboard.modules.system.dal.dataobject.logger;
import cn.iocoder.dashboard.common.enums.UserTypeEnum;
import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.util.Date;
/**
* API 访
*
* @author
*/
@TableName("sys_api_access_log")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SysApiAccessLogDO extends BaseDO {
/**
*
*/
@TableId
private Integer id;
/**
*
*
* 访logger
*/
private String traceId;
/**
*
*/
private Integer userId;
/**
*
*
* {@link UserTypeEnum}
*/
private Integer userType;
/**
*
*
* `spring.application.name`
*/
private String applicationName;
/**
*
*/
private String requestMethod;
/**
* 访
*/
private String requestUrl;
/**
*
*
* query: Query String
* body: Quest Body
*/
private String requestParams;
/**
* IP
*/
private String userIp;
/**
* UA
*/
private String userAgent;
/**
*
*/
private Date startTime;
/**
*
*/
private Date endTime;
/**
*
*/
private Integer duration;
/**
*
*
* 使 {@link CommonResult#getCode()}
*/
private Integer resultCode;
/**
*
*
* 使 {@link CommonResult#getMsg()}
*/
private String resultMsg;
}

View File

@ -0,0 +1,4 @@
package cn.iocoder.dashboard.modules.system.dal.dataobject.logger;
public class SysApiErrorLogDO {
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.dashboard.util.servlet; package cn.iocoder.dashboard.util.servlet;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
import cn.iocoder.dashboard.util.json.JsonUtils; import cn.iocoder.dashboard.util.json.JsonUtils;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -8,6 +9,7 @@ import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
@ -86,4 +88,8 @@ public class ServletUtils {
return ServletUtil.getClientIP(request); return ServletUtil.getClientIP(request);
} }
public static boolean isJsonRequest(ServletRequest request) {
return StrUtil.startWithIgnoreCase(request.getContentType(), MediaType.APPLICATION_JSON_VALUE);
}
} }