spring aspect是指在指定的方法前後或者環繞式的加上另外的處理,當指定的方法被使用的時候aspect配置的方法會被調用和執行。就像切面一樣,從一個切入點開始往這個切面嵌入別的處理流程
比如,在登陸的時候配置了登陸的切面,切面的功能是檢查用戶的ip地址,如果ip地址不符合我們要求的ip地址,則做相應的處理,處理完成後繼續登陸方法的執行。
下面是一個簡單的aspect的代碼:
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.heiman.smarthome.exception.CustomException;
import com.heiman.smarthome.po.custom.Config;
import com.heiman.smarthome.po.custom.EnterpriseMemberCustom;
import com.heiman.smarthome.po.enumpo.LogLevel;
import com.heiman.smarthome.po.enumpo.OperationType;
import com.heiman.smarthome.po.system.SecurityAudit;
import com.heiman.smarthome.service.SecurityAuditService;
@Aspect
@Component
public class SecurityAuditAspectj {
@Autowired
private HttpSession httpSession;
@Autowired
private HttpServletRequest httpServletRequest;
@Autowired
private Config config;
// 切點<增刪改查和登陸
@Pointcut("execution(* com.heiman.xx.service..*.*update*(..)) || "
+ "execution(* com.heiman.xx.service..*.*delete*(..)) || "
+ "execution(* com.heiman.xx.service..*.*insert*(..)) || "
+ "execution(* com.heiman.xx.service..*.*add*(..)) || "
+ "execution(* com.heiman.xx.service..*.*save*(..)) || "
+ "execution(* com.heiman.xx.service..*.*modify*(..)) || "
+ "execution(* com.heiman.xx.service..*.*edit*(..)) || "
+ "execution(* com.heiman.xx.service..*.*remove*(..)) || "
+ "execution(* com.heiman.xx.service..*.*repair*(..)) || "
+ "execution(* com.heiman.xx.controller..*.*login*(..))")
public void pointcut() {
}
/**
* 切點之後
*
* @param jp
*/
@After("pointcut()")
public void after(JoinPoint jp) {
SecurityAudit audit = new SecurityAudit();
String detail = null; // 詳細信息
String methodName = jp.getSignature().getName();// 方法名
String className = jp.getTarget().getClass().getSimpleName(); // 獲取類名
EnterpriseMemberCustom custom = (EnterpriseMemberCustom) httpSession.getAttribute("memberMsg");
if (custom == null)
return;
String userName = custom.getMembername();// 登陸用戶的名字
Integer enterpriseMemberId = custom.getId();
if (methodName.equals("login")) {
detail = userName + "登陸了";
} else {
String s = className.replaceAll("ServiceImpl", "").replaceAll("Service", "");
detail = userName + "對 " + s + " 執行了操作 " + methodName + " :" + together(jp);
}
if (methodName.contains("update") || methodName.contains("modify") || methodName.contains("edit")
|| methodName.contains("repair")) {
// 修改
audit.setOperationtype(OperationType.UPDATE.getIndex());
} else if (methodName.contains("delete") || methodName.contains("remove")) {
// 刪除
audit.setOperationtype(OperationType.DELETE.getIndex());
} else if (methodName.contains("save") || methodName.contains("insert") || methodName.contains("add")) {
// 增加
audit.setOperationtype(OperationType.ADD.getIndex());
} else if (methodName.equals("login")) {
// 登陸
audit.setOperationtype(OperationType.LOGIN.getIndex());
}
audit.setOperatorip(httpServletRequest.getLocalAddr());// 操作者ip
audit.setOperationtime(new Date());// 時間
audit.setLoglevel(LogLevel.INFO.getIndex());// 日誌級別
audit.setEnterprisememberid(enterpriseMemberId); // 操作者id
audit.setLogsummary(detail);// 詳情
}
}