import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target( { java.lang.annotation.ElementType.METHOD,
java.lang.annotation.ElementType.TYPE })
public @interface AuditLogger {
public abstract String log();
}
///
import java.lang.reflect.Method;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.test.model.AuditLog;
public class AuditLogInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation actioninvocation)
throws Exception {
AuditLog auditLog = new AuditLog();
auditLog.setStartTime(new Date());// 设置开始时间
String result = actioninvocation.invoke();// 递归调用拦截器
auditLog.setEndTime(new Date());// 设置结束时间
String userId = (String) ServletActionContext.getRequest().getSession()
.getAttribute("userId");
auditLog.setUserId(userId);// 设置登录用户的Id,在用户登录时把id保存到session中,这里可扩展判断用户是否登录的验证和权限验证
/*String name = actioninvocation.getInvocationContext().getName();
String methodName = "";
// struts.xml中配置:
// <package name="user" namespace="/user" extends="default">
// <action name="user_*" class="com.tgb.lk.action.UserAction" method="{1}">
//
// 访问地址: http://127.0.0.1:8080/AuditLogDemo/user/user_add
// http://127.0.0.1:8080/AuditLogDemo/user/user_del
if (name != null && name.contains("_")) {
methodName = name.substring(name.indexOf("_") + 1, name.length());
}*/
String methodName = actioninvocation.getProxy().getMethod();
if (methodName.length() > 0) {
Object action = actioninvocation.getAction();
Class clazz = action.getClass();
// 如果设置了注解则读取注解的内容,如果没有设置注解则记录登录的class名
if (clazz.isAnnotationPresent(AuditLogger.class)) {
AuditLogger talClazz = (AuditLogger) clazz
.getAnnotation(AuditLogger.class);
auditLog.setClazz(talClazz.log());
} else {
auditLog.setClazz(clazz.getSimpleName());
}
Method method = action.getClass().getMethod(methodName, null);
// 如果设置了注解则读取注解的内容,如果没有设置注解则记录登录的method名
if (method.isAnnotationPresent(AuditLogger.class)) {
AuditLogger alm = (AuditLogger) method
.getAnnotation(AuditLogger.class);
auditLog.setMethod(alm.log());
} else {
auditLog.setMethod(methodName);
}
String ip = ServletActionContext.getRequest().getRemoteAddr();
auditLog.setIp(ip);// 记录登录的IP,这里还可以对IP进行鉴权功能(允许或限制某些IP)
auditLog.setResult(result);// 记录登录时返回的结果.
System.out.println(auditLog);
// auditLogService.save(auditLog); //保存入库
}
return result; // 跳转
}
}
///
import java.util.Date;
public class AuditLog {
private int id;
private String userId;
private Date startTime;
private Date endTime;
private String ip;
private String clazz;
private String method;
private String result;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public String toString() {
return "AuditLog [id=" + id + ", userId=" + userId + ", ip=" + ip
+ ", startTime=" + startTime + ", endTime=" + endTime
+ ", clazz=" + clazz + ", method=" + method + ", result="
+ result + "]";
}
}
/
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<!-- 日志审计拦截器 -->
<interceptor name="auditlog"
class="com.test.auditlog.AuditLogInterceptor" />
<interceptor-stack name="myStack">
<interceptor-ref name="auditlog">
<!--
配置到excludeMethods中的方法将不记录日志
-->
<param name="excludeMethods">testExclude</param>
</interceptor-ref>
<!--
struts默认的拦截器
-->
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack" />
</package>
<!-- 注意extends="default" -->
<package name="user" namespace="/user" extends="default">
<action name="user_*" class="com.tgb.lk.action.UserAction" method="{1}">
<result name="add">/index.jsp</result>
<result name="del">/index.jsp</result>
<result name="modify">/index.jsp</result>
<result name="view">/index.jsp</result>
</action>
</package>
</struts>
转载于:https://my.oschina.net/huichen/blog/415939