SpringBoot配置使用Spring AOP日志记录
2021-01-21 21:15
标签:substring error span 显示 dep odi def sign false 1、引入pom 关于@Slf4j IDEA无法显示import的问题 https://www.cnblogs.com/dzcici/p/12877551.html 目录结构 2、使用注解方式进行注明 RequestUtil.class 3、使用 SpringBoot配置使用Spring AOP日志记录 标签:substring error span 显示 dep odi def sign false 原文地址:https://www.cnblogs.com/dzcici/p/12893599.html
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-aopartifactId>
dependency>
dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
dependency>
LogAuditAspect.class
package com.dbcoding.mall.cms.aspect;
import com.dbcoding.mall.cms.aspect.annotation.SystemLogs;
import com.dbcoding.mall.cms.service.SellChannelAddressService;
import com.dbcoding.mall.utils.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName: AAA
* @Description: TODO
* @Author: Stephen.Zhang
* @CreateDate: 2020-05-15 09:37
* @Version: 1.0
**/
@Aspect
@Component
@Slf4j
public class LogAuditAspect {
private String LOG_AUDIT_MESSAGE = "log_audit.properties";
@Autowired
private SellChannelAddressService sellService;
@Pointcut("@annotation(com.dbcoding.mall.cms.aspect.annotation.SystemLogs)")
public void aspect() {
}
@After("aspect()")
public void doAfter(JoinPoint joinPoint) throws Exception {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
Map
SystemLogs
package com.dbcoding.mall.cms.aspect.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({java.lang.annotation.ElementType.PARAMETER, java.lang.annotation.ElementType.METHOD})
public @interface SystemLogs {
String key() default "";
}
package com.dbcoding.mall.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.regex.Matcher;
/**
* @ClassName: AAA
* @Description: TODO
* @Author: Stephen.Zhang
* @CreateDate: 2020-05-15 10:18
* @Version: 1.0
**/
@Slf4j
public class RequestUtil {
public static int getPageid(HttpServletRequest request, String name) {
int pageid = NumberUtils.toInt(request.getParameter(name));
if (pageid ) {
return 1;
}
return pageid;
}
public static String getUrl(HttpServletRequest request) {
StringBuffer sb = request.getRequestURL();
String queryString = request.getQueryString();
if (StringUtils.isNotEmpty(queryString)) {
sb.append(‘?‘).append(queryString);
}
return sb.toString();
}
public static String getReferer(HttpServletRequest request) {
return request.getHeader("referer");
}
public static String getRequestUri(HttpServletRequest request) {
return request.getRequestURI();
}
private static final java.util.regex.Pattern IS_LICIT_IP_PATTERN = java.util.regex.Pattern.compile("^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$");
public static boolean isLicitIp(final String ip) {
if (StringUtils.isEmpty(ip)) {
return false;
}
Matcher m = IS_LICIT_IP_PATTERN.matcher(ip);
if (!m.find()) {
return false;
}
return true;
}
public static boolean isSsl(HttpServletRequest request) {
boolean isSsl = "true".equalsIgnoreCase(request.getHeader("ssl"));
return isSsl;
}
public static String getProtocol(HttpServletRequest request) {
boolean isSsl = RequestUtil.isSsl(request);
String protocol;
if (isSsl) {
protocol = "https";
} else {
protocol = "http";
}
return protocol;
}
public static String getRedirect(HttpServletRequest request, String url) {
if (StringUtils.isEmpty(url)) {
log.error("url parameters can not be null.");
throw new NullPointerException("url parameters can not be null.");
}
if (url.startsWith("http")) {
return url;
}
boolean isSsl = RequestUtil.isSsl(request);
if (!isSsl) {
return url;
}
if (!url.startsWith("/")) {
String path = request.getServletPath();
int index = path.lastIndexOf(‘/‘);
if (index != -1) {
url = path.substring(0, index + 1) + url;
}
}
String serverName = request.getServerName();
return "https://" + serverName + url;
}
@SuppressWarnings("unchecked")
public static void printHeaders(HttpServletRequest request) {
Enumeration
文章标题:SpringBoot配置使用Spring AOP日志记录
文章链接:http://soscw.com/index.php/essay/45140.html