springmvc 切面记录操作日志
生活随笔
收集整理的這篇文章主要介紹了
springmvc 切面记录操作日志
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<!-- 啟動(dòng)對(duì)@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(默認(rèn)為查)** @return*/String openType() default "3"; }操作日志記錄表
public class OperationLog {private String logId;//記錄IDprivate String time; //記錄時(shí)間private String ip; //記錄IPprivate String userName; //操作人private String logType; //操作類型private String logPage; //去往哪個(gè)頁(yè)面private String logOperateParam; //參數(shù)private String logDesc; //操作描述//省略get和set方法..... }切面操作
/*** 系統(tǒng)日志,記錄代碼運(yùn)行相關(guān)信息** @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執(zhí)行完畢后輸出執(zhí)行日志** @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();elsetoPage = 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;}/*** 獲取參數(shù)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;}/*** 獲取前臺(tái)傳過來的參數(shù)** @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]);elsevalue += "," + (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")@ResponseBodypublic Object initPwd(HttpServletRequest request, User user) {int result = 0;try {result=UserServer.updateUser(user);} catch (Exception e) {e.printStackTrace();}return result;}總結(jié)
以上是生活随笔為你收集整理的springmvc 切面记录操作日志的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python控制台输出颜色,让你的Pyt
- 下一篇: healthkit 之前的计步方案