Browse Source

add:common-log

yonge 5 years ago
parent
commit
3353f8aab0

+ 18 - 24
mec-common/common-log/pom.xml

@@ -1,26 +1,20 @@
 <?xml version="1.0"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
-  <modelVersion>4.0.0</modelVersion>
-  <parent>
-    <groupId>com.ym</groupId>
-    <artifactId>mec-common</artifactId>
-    <version>1.0</version>
-  </parent>
-  <groupId>com.ym</groupId>
-  <artifactId>common-log</artifactId>
-  <version>1.0</version>
-  <name>common-log</name>
-  <url>http://maven.apache.org</url>
-  <properties>
-    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-  </properties>
-  <dependencies>
-    <dependency>
-      <groupId>junit</groupId>
-      <artifactId>junit</artifactId>
-      <version>3.8.1</version>
-      <scope>test</scope>
-    </dependency>
-  </dependencies>
+<project
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+	<modelVersion>4.0.0</modelVersion>
+	<parent>
+		<groupId>com.ym</groupId>
+		<artifactId>mec-common</artifactId>
+		<version>1.0</version>
+	</parent>
+	<artifactId>common-log</artifactId>
+	<name>common-log</name>
+	<url>http://maven.apache.org</url>
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+	
+	<dependencies>
+	</dependencies>
 </project>

+ 31 - 0
mec-common/common-log/src/main/java/com/ym/mec/common/log/dal/dao/AuditLogDao.java

@@ -0,0 +1,31 @@
+package com.ym.mec.common.log.dal.dao;
+
+import java.util.List;
+import java.util.Map;
+
+import com.ym.mec.common.log.dal.model.AuditLog;
+
+public interface AuditLogDao {
+
+	int delete(Long id);
+
+	int insert(AuditLog auditLog);
+
+	AuditLog get(Long id);
+
+	int update(AuditLog auditLog);
+
+	/**
+	 * 查询记录数
+	 * @param params
+	 * @return
+	 */
+	public int queryCount(Map<String, Object> params);
+
+	/**
+	 * 查询对象列表
+	 * @param params
+	 * @return
+	 */
+	public List<AuditLog> queryPage(Map<String, Object> params);
+}

+ 76 - 0
mec-common/common-log/src/main/java/com/ym/mec/common/log/dal/model/AuditLog.java

@@ -0,0 +1,76 @@
+package com.ym.mec.common.log.dal.model;
+
+import java.util.Date;
+
+public class AuditLog {
+
+	private Long id;
+
+	private String username;
+
+	private String operateName;
+
+	private String interfaceUrl;
+
+	private String inputParams;
+
+	private Date operateTime;
+
+	private String userIp;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getUsername() {
+		return username;
+	}
+
+	public void setUsername(String username) {
+		this.username = username;
+	}
+
+	public String getOperateName() {
+		return operateName;
+	}
+
+	public void setOperateName(String operateName) {
+		this.operateName = operateName;
+	}
+
+	public String getInterfaceUrl() {
+		return interfaceUrl;
+	}
+
+	public void setInterfaceUrl(String interfaceUrl) {
+		this.interfaceUrl = interfaceUrl;
+	}
+
+	public String getInputParams() {
+		return inputParams;
+	}
+
+	public void setInputParams(String inputParams) {
+		this.inputParams = inputParams;
+	}
+
+	public Date getOperateTime() {
+		return operateTime;
+	}
+
+	public void setOperateTime(Date operateTime) {
+		this.operateTime = operateTime;
+	}
+
+	public String getUserIp() {
+		return userIp;
+	}
+
+	public void setUserIp(String userIp) {
+		this.userIp = userIp;
+	}
+}

+ 66 - 0
mec-common/common-log/src/main/java/com/ym/mec/common/log/interceptor/AuditLogInterceptor.java

@@ -0,0 +1,66 @@
+package com.ym.mec.common.log.interceptor;
+
+import java.io.IOException;
+import java.util.Date;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.method.HandlerMethod;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ym.mec.common.log.dal.model.AuditLog;
+import com.ym.mec.common.log.model.AuditLogAnnotation;
+import com.ym.mec.common.log.service.AuditLogService;
+import com.ym.mec.util.web.WebUtil;
+
+/**
+ * 日志审计的拦截器
+ */
+public class AuditLogInterceptor extends HandlerInterceptorAdapter {
+
+	@Autowired
+	private AuditLogService auditLogService;
+
+	private String username;
+
+	private ObjectMapper objMapper = new ObjectMapper();
+
+	public AuditLogInterceptor(String username) {
+		this.username = username;
+	}
+
+	@Override
+	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
+
+		return true;
+	}
+
+	@Override
+	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+
+		HandlerMethod handlerMethod = (HandlerMethod) handler;
+		AuditLogAnnotation anno = handlerMethod.getMethodAnnotation(AuditLogAnnotation.class);
+
+		if (anno != null) {
+			AuditLog auditLog = new AuditLog();
+			auditLog.setOperateName(anno.operateName());
+			auditLog.setInterfaceUrl(anno.interfaceURL());
+			auditLog.setUserIp(WebUtil.getRemoteIp(request));
+			auditLog.setInputParams(objMapper.writeValueAsString(WebUtil.getParameterMap(request)));
+			// 操作人
+			auditLog.setUsername(username);
+			auditLog.setOperateTime(new Date());
+			auditLogService.insert(auditLog);
+		}
+
+	}
+
+	public void setUsername(String username) {
+		this.username = username;
+	}
+
+}

+ 16 - 0
mec-common/common-log/src/main/java/com/ym/mec/common/log/model/AuditLogAnnotation.java

@@ -0,0 +1,16 @@
+package com.ym.mec.common.log.model;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.METHOD)
+public @interface AuditLogAnnotation {
+
+	String operateName();
+
+	String interfaceURL();
+
+}

+ 25 - 0
mec-common/common-log/src/main/java/com/ym/mec/common/log/service/AuditLogService.java

@@ -0,0 +1,25 @@
+package com.ym.mec.common.log.service;
+
+import java.util.List;
+import java.util.Map;
+
+import com.ym.mec.common.log.dal.model.AuditLog;
+
+public interface AuditLogService {
+
+	boolean insert(AuditLog auditLog);
+
+	/**
+	 * 分页查询
+	 * @param params
+	 * @return
+	 */
+	public List<AuditLog> queryPage(Map<String, Object> params);
+
+	/**
+	 * 查询记录数
+	 * @param params
+	 * @return
+	 */
+	public int queryCount(Map<String, Object> params);
+}

+ 34 - 0
mec-common/common-log/src/main/java/com/ym/mec/common/log/service/impl/AuditLogServiceImpl.java

@@ -0,0 +1,34 @@
+package com.ym.mec.common.log.service.impl;
+
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.ym.mec.common.log.dal.dao.AuditLogDao;
+import com.ym.mec.common.log.dal.model.AuditLog;
+import com.ym.mec.common.log.service.AuditLogService;
+
+@Service
+public class AuditLogServiceImpl implements AuditLogService {
+
+	@Autowired
+	private AuditLogDao auditLogDao;
+
+	@Override
+	public boolean insert(AuditLog auditLog) {
+		return auditLogDao.insert(auditLog) > 0;
+	}
+
+	@Override
+	public List<AuditLog> queryPage(Map<String, Object> params) {
+		return auditLogDao.queryPage(params);
+	}
+
+	@Override
+	public int queryCount(Map<String, Object> params) {
+		return auditLogDao.queryCount(params);
+	}
+
+}

+ 107 - 0
mec-common/common-log/src/main/resources/config/mybatis/auditLogMapper.xml

@@ -0,0 +1,107 @@
+<?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.yqh.p2p.dal.dao.AuditLogDao">
+	<resultMap id="BaseResultMap" type="com.yqh.p2p.dal.model.AuditLog">
+		<id column="id_" property="id" jdbcType="BIGINT" />
+		<result column="username_" property="username" jdbcType="VARCHAR" />
+		<result column="operate_name_" property="operateName" jdbcType="VARCHAR" />
+		<result column="interface_url_" property="interfaceUrl"
+			jdbcType="VARCHAR" />
+		<result column="operate_time_" property="operateTime" jdbcType="TIMESTAMP" />
+		<result column="input_params_" property="inputParams" jdbcType="LONGVARCHAR" />
+		<result column="user_ip_" property="userIp" jdbcType="VARCHAR" />
+	</resultMap>
+
+	<sql id="Base_Column_List">
+		id_, username_, operate_name_,
+		interface_url_,input_params_,
+		operate_time_, user_ip_
+	</sql>
+
+	<sql id="queryCondition">
+		<where>
+			<if test="operateName != null">
+				and operate_name_ = #{operateName}
+			</if>
+			<if test="username != null">
+				and username_ = #{username}
+			</if>
+			<if test="inputParams != null">
+				and input_params_ like '%' #{inputParams} '%' 
+			</if>
+			<if test="startDate != null">
+				and operate_time_ &gt;= #{startDate}
+			</if>
+			<if test="endDate != null">
+				and operate_time_ &lt;= #{endDate}
+			</if>
+		</where>
+	</sql>
+
+	<select id="get" resultMap="BaseResultMap" parameterType="java.lang.Long">
+		select
+		<include refid="Base_Column_List" />
+		from p2p_audit_log
+		where id_ = #{id,jdbcType=BIGINT}
+	</select>
+
+	<delete id="delete" parameterType="java.lang.Long">
+		delete from
+		p2p_audit_log
+		where
+		id_ = #{id,jdbcType=BIGINT}
+	</delete>
+	<insert id="insert" parameterType="com.yqh.p2p.dal.model.AuditLog">
+		insert into p2p_audit_log
+		(id_, username_, operate_name_,
+		interface_url_,
+		input_params_,operate_time_,
+		user_ip_)
+		values
+		(#{id,jdbcType=BIGINT},
+		#{username,jdbcType=VARCHAR},
+		#{operateName,jdbcType=VARCHAR},
+		#{interfaceUrl,jdbcType=VARCHAR},
+		#{inputParams,jdbcType=LONGVARCHAR},
+		#{operateTime,jdbcType=TIMESTAMP},
+		#{userIp,jdbcType=VARCHAR})
+	</insert>
+
+	<update id="update" parameterType="com.yqh.p2p.dal.model.AuditLog">
+		update p2p_audit_log
+		<set>
+			<if test="username != null">
+				username_ = #{username,jdbcType=VARCHAR},
+			</if>
+			<if test="operateName != null">
+				operate_name_ = #{operateName,jdbcType=VARCHAR},
+			</if>
+			<if test="interfaceUrl != null">
+				interface_url_ = #{interfaceUrl,jdbcType=VARCHAR},
+			</if>
+			<if test="operateTime != null">
+				operate_time_ = #{operateTime,jdbcType=TIMESTAMP},
+			</if>
+			<if test="userIp != null">
+				user_ip_ = #{userIp,jdbcType=VARCHAR},
+			</if>
+			<if test="inputParams != null">
+				input_params_ = #{inputParams,jdbcType=LONGVARCHAR},
+			</if>
+		</set>
+		where id_ = #{id,jdbcType=BIGINT}
+	</update>
+
+	<select id="queryCount" parameterType="map" resultType="int">
+		select count(*) from p2p_audit_log
+		<include refid="queryCondition" />
+	</select>
+
+	<select id="queryPage" parameterType="map" resultMap="BaseResultMap">
+		select * from p2p_audit_log
+		<include refid="queryCondition" />
+		order by operate_time_ desc
+		<include refid="global.limit" />
+	</select>
+
+</mapper>