1. 程式人生 > >Java使用aop實現操作日誌

Java使用aop實現操作日誌

/**  
* <p>Title: LogAopAction.java</p>  
* <p>Description: 操作日誌Controller</p>  
* <p>Company: </p>  
* @author Saffichan 
* @date 2018-06-01 15:38
* @version 1.0  
*/import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import
java.util.Date; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import com.saffi.web.BaseAction; import com.saffi.common.util.MapperUtil; import com.saffi.system.bo.LogBO;
import com.saffi.service.LogService; import com.saffi.user.bo.AdminBO; import com.saffi.web.annotation.PermssionMethodCode; @Aspect public class LogAopAction { // 注入service,用來將日誌資訊儲存在資料庫 @Resource(name = "logService") private LogService logService; // 配置接入點,即為所要記錄的action操作目錄 @Pointcut("execution(* com.saffi.*.action..*.*(..))") private void controllerAspect() { } @Around("controllerAspect()") public Object around(ProceedingJoinPoint pjp) throws Throwable { //日誌實體物件 LogBO logBo = new LogBO(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); // 從session獲取使用者名稱 String username = request.getSession().getAttribute("user"); logBo.setUsername(username);    // 獲取系統當前時間 logBo.setDate(new Date()); // 獲取訪問真實IP String ipAddress = request.getHeader("x-forwarded-for"); if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("Proxy-Client-IP"); } if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getHeader("WL-Proxy-Client-IP"); } if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){ //根據網絡卡取本機配置的IP InetAddress inet=null; try { inet = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } ipAddress= inet.getHostAddress(); } } //對於通過多個代理的情況,第一個IP為客戶端真實IP,多個IP按照','分割 if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15 if(ipAddress.indexOf(",")>0){ ipAddress = ipAddress.substring(0,ipAddress.indexOf(",")); } } logBo.setIp(ipAddress); // 攔截的實體類,就是當前正在執行的controller Object target = pjp.getTarget(); // 攔截的方法名稱。當前正在執行的方法 String methodName = pjp.getSignature().getName(); // 攔截的方法引數 Object[] args = pjp.getArgs(); //獲取請求路徑 String actionUrl = request.getRequestURI(); // 攔截的放參數型別 Signature sig = pjp.getSignature(); MethodSignature msig = null; if (!(sig instanceof MethodSignature)) { throw new IllegalArgumentException("該註解只能用於方法"); } msig = (MethodSignature) sig; Class[] parameterTypes = msig.getMethod().getParameterTypes(); Object object = null; // 獲得被攔截的方法 Method method = null; try { method = target.getClass().getMethod(methodName, parameterTypes); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } catch (SecurityException e1) { e1.printStackTrace(); } if (null != method) { // 獲取方法(此為自定義註解) OptionalLog op = method.getAnnotation(OptionalLog .class); // 獲取註解的modules 設為操作模組 logBo.setModule(op.modules()); // 獲取註解的methods 設為執行方法 logBo.setMethods(op.methods()); // 將上面獲取到的請求路徑 設為請求路徑 logBo.setActionurl(actionUrl); try { object = pjp.proceed(); //接受客戶端的資料 Map<String,String[]> map = request.getParameterMap();           // 解決獲取引數亂碼 Map<String,String[]> newmap = new HashMap<String,String[]>(); for(Map.Entry<String, String[]> entry : map.entrySet()){ String name = entry.getKey(); String values[] = entry.getValue(); if(values==null){ newmap.put(name, new String[]{}); continue; } String newvalues[] = new String[values.length]; for(int i=0; i<values.length;i++){ String value = values[i]; value = new String(value.getBytes("iso8859-1"),request.getCharacterEncoding()); newvalues[i] = value; //解決亂碼後封裝到Map中 } newmap.put(name, newvalues); } logBo.setContent(MapperUtil.toJsonStr(newmap)); //1為執行成功 logBo.setCommite((byte) 1); // 新增到資料庫 logService.add(logBo); } catch (Throwable e) {          //接受客戶端的資料 Map<String,String[]> map = request.getParameterMap();        // 解決獲取引數亂碼 Map<String,String[]> newmap = new HashMap<String,String[]>(); for(Map.Entry<String, String[]> entry : map.entrySet()){ String name = entry.getKey(); String values[] = entry.getValue(); if(values==null){ newmap.put(name, new String[]{}); continue; } String newvalues[] = new String[values.length]; for(int i=0; i<values.length;i++){ String value = values[i]; value = new String(value.getBytes("iso8859-1"),request.getCharacterEncoding()); newvalues[i] = value; //解決亂碼後封裝到Map中 } newmap.put(name, newvalues); }          //MapperUtil.toJsonStr為自定義的轉換工具類 logBo.setContent(MapperUtil.toJsonStr(newmap)); //2為執行失敗 logBo.setCommite((byte) 2);          //新增到資料庫 logService.add(logBo); } } return object; } }