<!-- 启动对@AspectJ注解的支持 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
自定义注解
/**
* 是否添操作日志注解
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodLog {
/**
* 记录操作描述
*
* @return
*/
String remark() default "";
/**
* 增:0,删:1,改:2,查:3(默认为查)
*
* @return
*/
String openType() default "3";
}
操作日志记录表
public class OperationLog {
private String logId;//记录ID
private String time; //记录时间
private String ip; //记录IP
private String userName; //操作人
private String logType; //操作类型
private String logPage; //去往哪个页面
private String logOperateParam; //参数
private String logDesc; //操作描述
//省略get和set方法
.....
}
切面操作
/**
* 系统日志,记录代码运行相关信息
*
* @author Administrator
*/
@Component
@Aspect
@Order(0)
public class SystemOperationLogs {
private String isOpen = "0";
@Resource(name = "OperationLogService")
private OperationLogService operationLogServer ;
/**
* @param point
* @throws Throwable
*/
@Before("execution(* com.*.controller..*(..))")
public void enterController(JoinPoint point) throws Throwable {
isOpen = PublicConfig.OPEN_LOG;//获取配置文件信息是否开启日志
}
/**
* controller执行完毕后输出执行日志
*
* @param point
* @param result
*/
@AfterReturning(
pointcut = "execution(* com.*.controller..*(..))",
returning = "result"
)
public void logAfterExecution(JoinPoint point, Object result) throws Throwable {
if (isOpen.equals("1")) {
String className = point.getTarget().getClass().getName();
String methodName = point.getSignature().getName();
MethodLog methodLog = getMethodRemark(point);
Class targetClass = Class.forName(className);
Method[] method = targetClass.getMethods();
boolean isAjax = false;
for (Method m : method) {
//判断该方法是否有ResponseBody注解
if (m.getName().equals(methodName) && m.isAnnotationPresent(ResponseBody.class)) {
isAjax = true;
break;
}
}
String toPage = null;
if (result != null) {
if (!isAjax) {
if (result instanceof ModelAndView)
toPage = ((ModelAndView) result).getViewName();
else
toPage = result.toString();
} else {
toPage = "ajaxReturn";
}
} else {
toPage = "noReturn";
}
if (className != null && methodName != null && methodLog != null) {
HttpServletRequest request = this.getRequest(point);
this.insertLog(request, toPage, methodLog);
}
}
}
/**
* 获取方法的中文备注____用于记录用户的操作日志描述
*
* @param joinPoint
* @return
* @throws Exception
*/
private MethodLog getMethodRemark(JoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] method = targetClass.getMethods();
for (Method m : method) {
if (m.getName().equals(methodName)) {
Class[] tmpCs = m.getParameterTypes();
if (tmpCs.length == arguments.length) {
MethodLog methodCache = m.getAnnotation(MethodLog.class);
if (methodCache != null && !("").equals(methodCache.remark())) {
return methodCache;
}
break;
}
}
}
return null;
}
/**
* 获取参数request
*
* @param point
* @return
*/
private HttpServletRequest getRequest(JoinPoint point) {
Object[] args = point.getArgs();
for (Object obj : args) {
if (obj instanceof HttpServletRequest)
return (HttpServletRequest) obj;
}
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
return request;
}
/**
* 获取IP
* @param request
* @return
*/
private String getRequestIP(HttpServletRequest request) {
String ip = null;
if (request.getHeader("x-forwarded-for") == null) {
ip = request.getRemoteAddr();
}else{
ip = request.getHeader("x-forwarded-for");
}
return ip;
}
/**
* 获取前台传过来的参数
*
* @param request
* @return
*/
private String getParam(HttpServletRequest request) {
Map properties = request.getParameterMap();
Map returnMap = new HashMap();
Iterator entries = properties.entrySet().iterator();
Map.Entry entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
value = null;
if (null == valueObj) {
value = "";
} else if (valueObj instanceof String[]) {
String[] values = (String[]) valueObj;
for (int i = 0; i < values.length; i++) {
if (value == null)
value = (values[i] == null ? "" : values[i]);
else
value += "," + (values[i] == null ? "" : values[i]);
}
} else {
value = valueObj.toString();
}
returnMap.put(name, value);
}
return JsonUtils.mapToJson(returnMap);
}
private int insertLog(HttpServletRequest request, String toPage, MethodLog methodLog) {
OperationLog syslog = new OperationLog();
sysLog.setIp(this.getRequestIP(request));
sysLog.setUserName(UserUtils.getUserModel(request).getUserName());
sysLog.setLogType(methodLog.openType());
sysLog.setLogDesc(methodLog.remark());
sysLog.setLogPage(toPage);
sysLog.setLogOperateParam(getParam(request));
return sysLogBiz.insertLog(sysLog);
}
}
controller中使用
@RequestMapping(value = "/updateUser")
@MethodLog(remark = "修改用户信息",openType = "2")
@ResponseBody
public Object initPwd(HttpServletRequest request, User user) {
int result = 0;
try {
result=UserServer.updateUser(user);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}