1、定义切面类
package com.aa.aa.pp.interceptor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sgit.ois.framework.entity.WrappedResult;
import com.sgit.ois.pp.exception.ProcessException;
import com.sgit.ois.pp.util.CommonUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.weaver.ast.Var;
import org.springframework.context.annotation.Configuration;
import java.util.*;
import java.util.regex.Pattern;
@Aspect
@Configuration
public class AspectPP1 {
@Pointcut("execution(* com.aa.aa.pp.controller.*.*(..))")
public void all(){}
@Pointcut("execution(* com.aa.aa.pp.controller.productSupplier.ProductSupplierController.save*(..))")
public void pointCut20(){}
@Before("all()|| pointCut20() ")
public Object intercept(JoinPoint joinPoint) throws Throwable {
if (joinPoint == null){
return "";
}
Object[] args = joinPoint.getArgs();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Class[] paramTypeArray = methodSignature.getParameterTypes();
String[] parameterNames = methodSignature.getParameterNames();
Map<String,Object> params = new HashMap<>();
for (int i = 0; i < args.length; i++) {
params = judgeParam(args[i],parameterNames[i]);
modifyParameterValues(params);
args[i] = changeParam(params,args[i],parameterNames[i],paramTypeArray[i]);
}
return "text";
}
private Object changeParam(Map<String, Object> params, Object arg, String parameterName,Class paramTypeArray) {
if ((arg instanceof String) || (arg instanceof Map) || (arg instanceof List)){
arg = params.get(parameterName);
return arg;
}
arg = JSONObject.toJavaObject((JSON) JSON.toJSON(params),paramTypeArray);
return arg;
}
private Map<String, Object> judgeParam(Object arg,String parameterName) throws Throwable {
Map<String,Object> params = new HashMap<>();
if ((arg instanceof String) || (arg instanceof Map) || (arg instanceof List)){
params.put(parameterName,arg);
}else{
params = new ObjectMapper().readValue(JSON.toJSONString(arg), new TypeReference<HashMap<String, Object>>() {});
}
return params;
}
public void modifyParameterValues(Map<String,Object> params)throws Exception{
Set<String> set = params.keySet();
Iterator<String> it=set.iterator();
while(it.hasNext()){
String key= it.next();
if ("m2Data".equals(key)){
String s = CommonUtils.decryptToText((String) params.get(key));
Map<String,Object> map = (Map<String,Object>)JSON.parse(s);
recursiveReplaceCharacter(map);
}
getValueByKey(params,key);
}
}
private void getValueByKey(Map<String,Object> params,String key) throws Exception{
Object value = judgeObject(params.get(key));
params.put(key,value);
}
private Object recursiveReplaceCharacter(Map<String, Object> vo) throws Exception{
Map<String, Object> replaceMap = new HashMap<>();
for(String key : vo.keySet()){
Object replaceMapValue = vo.get(key);
Object a = judgeObject(replaceMapValue);
replaceMap.put(key,a);
}
return replaceMap;
}
private static final String PASSWORD_PATTERN = "[<>?,/《》?;':‘’”;\\{\\}\\[\\]|、~!@#$%^&*()+`——]";
Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
private Object judgeObject(Object replaceMapValue) throws Exception{
if (null == replaceMapValue) {
return null;
}
if (replaceMapValue instanceof String) {
String s = replaceMapValue.toString();
boolean b = pattern.matcher(s).find();
if (b){
throw new ProcessException("包含特殊字符");
}
return CharacterUtils.replaceBankParameter((String) replaceMapValue);
} else if (replaceMapValue instanceof Map) {
return recursiveReplaceCharacter((Map<String,Object>) replaceMapValue);
} else if (replaceMapValue instanceof List) {
List<Object> alist = (List<Object>) replaceMapValue;
for (int i = 0; i < alist.size(); i++) {
Object vol = alist.get(i);
if (vol instanceof String) {
alist.set(i, CharacterUtils.replaceBankParameter((String) vol));
} else if (vol instanceof Map) {
alist.set(i, recursiveReplaceCharacter((Map<String,Object>) vol));
}
}
return alist;
}
return replaceMapValue;
}
}
2、全局异常捕获
package com.wang.test.annotation;
import com.wang.test.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.SystemException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(SystemException.class)
public Result systemExceptionHandler(SystemException e) {
log.error("出现了异常! {}", e);
return Result.nok(e.getCause().getMessage());
}
@ExceptionHandler(Exception.class)
public Result exceptionHandler(Exception e) {
log.error("出现了异常! {}", e);
return Result.nok(e.getCause().getMessage());
}
@ExceptionHandler(BindException.class)
public Result bindExceptionHandler(BindException e) {
log.error("出现了异常! {}", e);
return Result.nok(e.getCause().getMessage());
}
}