注解类
import java.lang.annotation.*;
/**
* @author CJJ
* @version 1.0
* @createDate 2021/08/24 18:44
* @see com.suncnpap.framework.web.annotation
*/
@Documented
@Inherited
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Unescape {
boolean enable() default true;
}
aspect类环绕通知
import cn.hutool.http.HtmlUtil;
import com.suncnpap.framework.web.annotation.Unescape;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
/**
* Unescape Annotation 处理
* @author CJJ
* @version 1.0
* @createDate 2021/08/25 10:07
* @see com.suncnpap.framework.web.aspect
*/
@Aspect
@Component
@Slf4j
@Order(-99)
public class UnescapeAspect {
//com.suncnpap.datacenter.datamart.pojo.vo.EscapeVo.getRawContent
@Pointcut("execution(* com.suncnpap.datacenter.*.controller.*.*(..))")
public void pointcutGet() {
}
@SneakyThrows
@Around("pointcutGet()")
public Object unescape(ProceedingJoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
Class<?> aClass = arg.getClass();
String name = aClass.getName();
if (name.contains(".pojo.")) {
Field[] declaredFields = aClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
Unescape unescape = declaredField.getDeclaredAnnotation(Unescape.class);
if (null != unescape && unescape.enable()) {
log.info("field:{} with Unescape annotation!", declaredField.getName());
declaredField.setAccessible(true);
Object rawValue = declaredField.get(arg);
declaredField.set(arg, null == rawValue ? null : HtmlUtil.unescape(rawValue.toString()));
}
}
}
}
Object proceed = joinPoint.proceed(args);
return proceed;
}
}
切点表达式拦截方法
在接收参数前,将有注解@Unescape的字段内容进行html解码