自定義Spring的Aop切面類攔截業務請求,並獲取到請求的引數名和引數值
阿新 • • 發佈:2019-01-02
/**
* @author 劉俊重
* @Description 稽核校驗
* 所有的service業務方法都會先走這個方法,
* 先判斷本操作需不需要稽核,如果需要稽核則插入稽核隊列表,
* 不需要稽核則直接插入相關業務表
* @date 2017年7月5日
*/
@Component
@Aspect
public class ReviewValidate {
@Autowired
private ImAuthQueueDao imAuthQueueDao;
private static Logger logger = LoggerFactory.getLogger(ReviewValidate.class);
@Around(value = "execution(* com.ynet.finmall.innermanage.service.impl.*.*(..))")
public Object validate(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
String classType = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(classType);
String clazzName = clazz.getName();
String methodName = joinPoint.getSignature().getName(); //獲取方法名稱
//獲取引數名稱和值
Map<String,Object > nameAndArgs = this.getFieldsName(this.getClass(), clazzName, methodName,args);
//nameAndArgs的兩種型別,用實體類接收的類似這樣: reqParams=com.ynet [email protected]
//用Map<String,Object>接收的是這樣:menuNo=56473283,遍歷這個map區分兩種不同,使用不同的取值方式。
//根據獲取到的值所屬的不同型別通過兩種不同的方法獲取引數
boolean flag = false;
if(nameAndArgs!=null && nameAndArgs.size()>0){
for (Map.Entry<String, Object> entry : nameAndArgs.entrySet()) {
logger.info(">>>>>>>>>>>>>>型別"+entry.getValue().getClass().toString());
if(entry.getValue() instanceof String){
flag = true;
break; //跳出迴圈
}
}
}
StringBuffer sb = new StringBuffer();
if(flag){
//從Map中獲取
sb.append(JSONObject.toJSONString(nameAndArgs))
.append(",");
}else{
if (args != null) {
for (Object object : args) {
if (object != null) {
if (object instanceof MultipartFile||object instanceof ServletRequest||object instanceof ServletResponse) {
continue;
}
sb.append(JSONObject.toJSONString(object))
.append(",");
}
}
}
}
String isReview = "";
String arg = sb.toString().substring(0, sb.toString().length()-1);
JSONObject cache_json = new JSONObject();
if(arg!=null){
cache_json = JSONObject.parseObject(arg);
//是否需要稽核
isReview = cache_json.getString("isReview");
}
logger.info(">>>>>>>>>>>>>>>>>>>"+cache_json);
if(isReview!=null && isReview!="" && DbEnum.CommonReqParam.IsReview.NEEDREVIEW.equals(isReview)){
//需要稽核,執行存入稽核佇列操作
//如果本操作是需要稽核的,則將本操作快取在操作佇列中,稽核通過時才真正將新增操作員操作插入資料庫
ImAuthQueue imAuth = new ImAuthQueue();
imAuth.setIaqBatchNo(UUID.randomUUID().toString().replace("-", ""));
imAuth.setIaqPk(cache_json.toJSONString());
imAuth.setIaqBsnCode(cache_json.getString("businessCode"));
imAuth.setIaqOpType(cache_json.getString("operatorType"));
imAuth.setIaqSubmitUserId(cache_json.getString("submitUserId"));
imAuth.setIaqDescribe(cache_json.getString("describe"));
//插入稽核隊列表中
imAuthQueueDao.insert(imAuth);
return null;
}else{
//不需要稽核,直接執行普通的增刪改操作
return joinPoint.proceed();
}
}
/**
* @Description 獲取欄位名和欄位值
* @Author 劉俊重
* @date 2017年7月6日
* @return Map<String,Object>
*/
private Map<String,Object> getFieldsName(Class cls, String clazzName, String methodName, Object[] args) throws Exception {
Map<String,Object > map=new HashMap<String,Object>();
ClassPool pool = ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(cls);
pool.insertClassPath(classPath);
CtClass cc = pool.get(clazzName);
CtMethod cm = cc.getDeclaredMethod(methodName);
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
// exception
}
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < cm.getParameterTypes().length; i++){
map.put( attr.variableName(i + pos),args[i]);//paramNames即引數名
}
return map;
}
}
* @author 劉俊重
* @Description 稽核校驗
* 所有的service業務方法都會先走這個方法,
* 先判斷本操作需不需要稽核,如果需要稽核則插入稽核隊列表,
* 不需要稽核則直接插入相關業務表
* @date 2017年7月5日
*/
@Component
@Aspect
public class ReviewValidate {
@Autowired
private ImAuthQueueDao imAuthQueueDao;
private static Logger logger = LoggerFactory.getLogger(ReviewValidate.class);
@Around(value = "execution(* com.ynet.finmall.innermanage.service.impl.*.*(..))")
public Object validate(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
String classType = joinPoint.getTarget().getClass().getName();
Class<?> clazz = Class.forName(classType);
String clazzName = clazz.getName();
String methodName = joinPoint.getSignature().getName(); //獲取方法名稱
//獲取引數名稱和值
Map<String,Object > nameAndArgs = this.getFieldsName(this.getClass(), clazzName, methodName,args);
//nameAndArgs的兩種型別,用實體類接收的類似這樣: reqParams=com.ynet
//用Map<String,Object>接收的是這樣:menuNo=56473283,遍歷這個map區分兩種不同,使用不同的取值方式。
//根據獲取到的值所屬的不同型別通過兩種不同的方法獲取引數
boolean flag = false;
if(nameAndArgs!=null && nameAndArgs.size()>0){
for (Map.Entry<String, Object> entry : nameAndArgs.entrySet()) {
logger.info(">>>>>>>>>>>>>>型別"+entry.getValue().getClass().toString());
if(entry.getValue() instanceof String){
flag = true;
break; //跳出迴圈
}
}
}
StringBuffer sb = new StringBuffer();
if(flag){
//從Map中獲取
sb.append(JSONObject.toJSONString(nameAndArgs))
.append(",");
}else{
if (args != null) {
for (Object object : args) {
if (object != null) {
if (object instanceof MultipartFile||object instanceof ServletRequest||object instanceof ServletResponse) {
continue;
}
sb.append(JSONObject.toJSONString(object))
.append(",");
}
}
}
}
String isReview = "";
String arg = sb.toString().substring(0, sb.toString().length()-1);
JSONObject cache_json = new JSONObject();
if(arg!=null){
cache_json = JSONObject.parseObject(arg);
//是否需要稽核
isReview = cache_json.getString("isReview");
}
logger.info(">>>>>>>>>>>>>>>>>>>"+cache_json);
if(isReview!=null && isReview!="" && DbEnum.CommonReqParam.IsReview.NEEDREVIEW.equals(isReview)){
//需要稽核,執行存入稽核佇列操作
//如果本操作是需要稽核的,則將本操作快取在操作佇列中,稽核通過時才真正將新增操作員操作插入資料庫
ImAuthQueue imAuth = new ImAuthQueue();
imAuth.setIaqBatchNo(UUID.randomUUID().toString().replace("-", ""));
imAuth.setIaqPk(cache_json.toJSONString());
imAuth.setIaqBsnCode(cache_json.getString("businessCode"));
imAuth.setIaqOpType(cache_json.getString("operatorType"));
imAuth.setIaqSubmitUserId(cache_json.getString("submitUserId"));
imAuth.setIaqDescribe(cache_json.getString("describe"));
//插入稽核隊列表中
imAuthQueueDao.insert(imAuth);
return null;
}else{
//不需要稽核,直接執行普通的增刪改操作
return joinPoint.proceed();
}
}
/**
* @Description 獲取欄位名和欄位值
* @Author 劉俊重
* @date 2017年7月6日
* @return Map<String,Object>
*/
private Map<String,Object> getFieldsName(Class cls, String clazzName, String methodName, Object[] args) throws Exception {
Map<String,Object > map=new HashMap<String,Object>();
ClassPool pool = ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(cls);
pool.insertClassPath(classPath);
CtClass cc = pool.get(clazzName);
CtMethod cm = cc.getDeclaredMethod(methodName);
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
if (attr == null) {
// exception
}
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < cm.getParameterTypes().length; i++){
map.put( attr.variableName(i + pos),args[i]);//paramNames即引數名
}
return map;
}
}