Quellcode durchsuchen

新增访问日志

zhouhao vor 8 Jahren
Ursprung
Commit
bbe40c2176

+ 1 - 1
hsweb-commons/hsweb-commons-controller/pom.xml

@@ -45,7 +45,7 @@
         </dependency>
         <dependency>
             <groupId>org.hswebframework.web</groupId>
-            <artifactId>hsweb-logging</artifactId>
+            <artifactId>hsweb-access-logging-api</artifactId>
             <version>${project.version}</version>
         </dependency>
         <dependency>

+ 5 - 0
hsweb-examples/hsweb-examples-simple/pom.xml

@@ -71,6 +71,11 @@
             <artifactId>slf4j-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-access-logging-aop</artifactId>
+            <version>${project.version}</version>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-jdbc</artifactId>

+ 10 - 0
hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/SpringBootExample.java

@@ -17,6 +17,7 @@
 
 package org.hswebframework.web.example.simple;
 
+import com.alibaba.fastjson.JSON;
 import org.hsweb.ezorm.rdb.executor.AbstractJdbcSqlExecutor;
 import org.hsweb.ezorm.rdb.executor.SqlExecutor;
 import org.hswebframework.web.authorization.Authentication;
@@ -30,6 +31,9 @@ import org.hswebframework.web.dao.datasource.DatabaseType;
 import org.hswebframework.web.entity.authorization.*;
 import org.hswebframework.web.entity.authorization.bind.BindPermissionRoleEntity;
 import org.hswebframework.web.entity.authorization.bind.BindRoleUserEntity;
+import org.hswebframework.web.loggin.aop.EnableAccessLogger;
+import org.hswebframework.web.logging.AccessLoggerInfo;
+import org.hswebframework.web.logging.AccessLoggerListener;
 import org.hswebframework.web.service.authorization.PermissionService;
 import org.hswebframework.web.service.authorization.RoleService;
 import org.hswebframework.web.service.authorization.UserService;
@@ -70,8 +74,14 @@ import java.util.Collections;
 @EnableSwagger2
 @EnableCaching
 @EnableAspectJAutoProxy
+@EnableAccessLogger
 public class SpringBootExample implements CommandLineRunner {
 
+    @Bean
+    public AccessLoggerListener accessLoggerListener() {
+        return loggerInfo -> System.out.println("有请求啦:" + JSON.toJSONString(loggerInfo));
+    }
+
     @Bean
     public Docket createRestApi() {
         return new Docket(DocumentationType.SWAGGER_2)

+ 46 - 0
hsweb-logging/hsweb-access-logging-aop/pom.xml

@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>hsweb-logging</artifactId>
+        <groupId>org.hswebframework.web</groupId>
+        <version>3.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>hsweb-access-logging-aop</artifactId>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-access-logging-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-boost-aop</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-commons-utils</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-aop</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.5</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>

+ 82 - 0
hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/loggin/aop/AopAccessLoggerSupport.java

@@ -0,0 +1,82 @@
+package org.hswebframework.web.loggin.aop;
+
+import org.aopalliance.intercept.MethodInterceptor;
+import org.hswebframework.web.AopUtils;
+import org.hswebframework.web.WebUtil;
+import org.hswebframework.web.boost.aop.context.MethodInterceptorHolder;
+import org.hswebframework.web.logging.AccessLogger;
+import org.hswebframework.web.logging.AccessLoggerInfo;
+import org.hswebframework.web.logging.AccessLoggerListener;
+import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
+import org.springframework.core.Ordered;
+
+import javax.servlet.http.HttpServletRequest;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+public class AopAccessLoggerSupport extends StaticMethodMatcherPointcutAdvisor {
+
+    private final List<AccessLoggerListener> listeners = new ArrayList<>();
+
+    public AopAccessLoggerSupport addListener(AccessLoggerListener loggerListener) {
+        listeners.add(loggerListener);
+        return this;
+    }
+
+    public AopAccessLoggerSupport() {
+        setAdvice((MethodInterceptor) methodInvocation -> {
+            MethodInterceptorHolder methodInterceptorHolder = MethodInterceptorHolder.create(methodInvocation);
+            AccessLoggerInfo info = createLogger(methodInterceptorHolder);
+            Object response;
+            try {
+                response = methodInvocation.proceed();
+                info.setResponse(response);
+                info.setResponseTime(System.currentTimeMillis());
+            } catch (Throwable e) {
+                info.setException(e);
+                throw e;
+            } finally {
+                listeners.forEach(listener -> listener.onLogger(info));
+            }
+            return response;
+        });
+    }
+
+    protected AccessLoggerInfo createLogger(MethodInterceptorHolder holder) {
+        AccessLoggerInfo info = new AccessLoggerInfo();
+        info.setRequestTime(System.currentTimeMillis());
+
+        AccessLogger ann = holder.findAnnotation(AccessLogger.class);
+        info.setAction(ann.value());
+        info.setDescribe(String.join("\n", ann.describe()));
+        info.setParameters(holder.getArgs());
+        info.setTarget(holder.getTarget().getClass());
+        info.setMethod(holder.getMethod());
+
+        HttpServletRequest request = WebUtil.getHttpServletRequest();
+        if (null != request) {
+            info.setHttpHeaders(WebUtil.getHeaders(request));
+            info.setIp(WebUtil.getIpAddr(request));
+            info.setHttpMethod(request.getMethod());
+            info.setUrl(request.getRequestURL().toString());
+        }
+        return info;
+
+    }
+
+    @Override
+    public int getOrder() {
+        return Ordered.HIGHEST_PRECEDENCE;
+    }
+
+    @Override
+    public boolean matches(Method method, Class<?> aClass) {
+        return null != AopUtils.findAnnotation(aClass, method, AccessLogger.class);
+    }
+}

+ 47 - 0
hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/loggin/aop/AopAccessLoggerSupportAutoConfiguration.java

@@ -0,0 +1,47 @@
+package org.hswebframework.web.loggin.aop;
+
+
+import org.hswebframework.web.logging.AccessLoggerListener;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.BeanPostProcessor;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+@Configuration
+public class AopAccessLoggerSupportAutoConfiguration {
+
+    @Bean
+    public AopAccessLoggerSupport aopAccessLoggerSupport() {
+        return new AopAccessLoggerSupport();
+    }
+
+    @Bean
+    public ListenerProcessor listenerProcessor() {
+        return new ListenerProcessor();
+    }
+
+    public static class ListenerProcessor implements BeanPostProcessor {
+
+        @Autowired
+        private AopAccessLoggerSupport aopAccessLoggerSupport;
+
+        @Override
+        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
+            return bean;
+        }
+
+        @Override
+        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+            if (bean instanceof AccessLoggerListener) {
+                aopAccessLoggerSupport.addListener(((AccessLoggerListener) bean));
+            }
+            return bean;
+        }
+    }
+}

+ 31 - 0
hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/loggin/aop/EnableAccessLogger.java

@@ -0,0 +1,31 @@
+/*
+ *
+ *  * Copyright 2016 http://www.hswebframework.org
+ *  *
+ *  * Licensed under the Apache License, Version 2.0 (the "License");
+ *  * you may not use this file except in compliance with the License.
+ *  * You may obtain a copy of the License at
+ *  *
+ *  *     http://www.apache.org/licenses/LICENSE-2.0
+ *  *
+ *  * Unless required by applicable law or agreed to in writing, software
+ *  * distributed under the License is distributed on an "AS IS" BASIS,
+ *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  * See the License for the specific language governing permissions and
+ *  * limitations under the License.
+ *
+ */
+
+package org.hswebframework.web.loggin.aop;
+
+import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
+
+import java.lang.annotation.*;
+
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Inherited
+@ImportAutoConfiguration(AopAccessLoggerSupportAutoConfiguration.class)
+public @interface EnableAccessLogger {
+}

+ 15 - 0
hsweb-logging/hsweb-access-logging-api/pom.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>hsweb-logging</artifactId>
+        <groupId>org.hswebframework.web</groupId>
+        <version>3.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>hsweb-access-logging-api</artifactId>
+
+
+</project>

hsweb-logging/src/main/java/org/hswebframework/web/logging/AccessLogger.java → hsweb-logging/hsweb-access-logging-api/src/main/java/org/hswebframework/web/logging/AccessLogger.java


+ 144 - 0
hsweb-logging/hsweb-access-logging-api/src/main/java/org/hswebframework/web/logging/AccessLoggerInfo.java

@@ -0,0 +1,144 @@
+package org.hswebframework.web.logging;
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+public class AccessLoggerInfo implements Serializable {
+
+    private String action;
+
+    private String describe;
+
+    private Method method;
+
+    private Class target;
+
+    private Map<String, Object> parameters;
+
+    private String ip;
+
+    private String url;
+
+    private Map<String, String> httpHeaders;
+
+    private String httpMethod;
+
+    private Object response;
+
+    private long requestTime;
+
+    private long responseTime;
+
+    private Throwable exception;
+
+
+    public String getAction() {
+        return action;
+    }
+
+    public void setAction(String action) {
+        this.action = action;
+    }
+
+    public String getDescribe() {
+        return describe;
+    }
+
+    public void setDescribe(String describe) {
+        this.describe = describe;
+    }
+
+    public Method getMethod() {
+        return method;
+    }
+
+    public void setMethod(Method method) {
+        this.method = method;
+    }
+
+    public Class getTarget() {
+        return target;
+    }
+
+    public void setTarget(Class target) {
+        this.target = target;
+    }
+
+    public Map<String, Object> getParameters() {
+        return parameters;
+    }
+
+    public void setParameters(Map<String, Object> parameters) {
+        this.parameters = parameters;
+    }
+
+    public String getIp() {
+        return ip;
+    }
+
+    public void setIp(String ip) {
+        this.ip = ip;
+    }
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public Map<String, String> getHttpHeaders() {
+        return httpHeaders;
+    }
+
+    public void setHttpHeaders(Map<String, String> httpHeaders) {
+        this.httpHeaders = httpHeaders;
+    }
+
+    public String getHttpMethod() {
+        return httpMethod;
+    }
+
+    public void setHttpMethod(String httpMethod) {
+        this.httpMethod = httpMethod;
+    }
+
+    public Object getResponse() {
+        return response;
+    }
+
+    public void setResponse(Object response) {
+        this.response = response;
+    }
+
+    public long getRequestTime() {
+        return requestTime;
+    }
+
+    public void setRequestTime(long requestTime) {
+        this.requestTime = requestTime;
+    }
+
+    public long getResponseTime() {
+        return responseTime;
+    }
+
+    public void setResponseTime(long responseTime) {
+        this.responseTime = responseTime;
+    }
+
+    public Throwable getException() {
+        return exception;
+    }
+
+    public void setException(Throwable exception) {
+        this.exception = exception;
+    }
+}

+ 10 - 0
hsweb-logging/hsweb-access-logging-api/src/main/java/org/hswebframework/web/logging/AccessLoggerListener.java

@@ -0,0 +1,10 @@
+package org.hswebframework.web.logging;
+
+/**
+ * TODO 完成注释
+ *
+ * @author zhouhao
+ */
+public interface AccessLoggerListener {
+    void onLogger(AccessLoggerInfo loggerInfo);
+}

+ 5 - 0
hsweb-logging/pom.xml

@@ -28,6 +28,11 @@
     <modelVersion>4.0.0</modelVersion>
 
     <artifactId>hsweb-logging</artifactId>
+    <packaging>pom</packaging>
+    <modules>
+        <module>hsweb-access-logging-api</module>
+        <module>hsweb-access-logging-aop</module>
+    </modules>
 
 
 </project>