自定义标签+aop解决请求参数携带 ‘*’ ‘_’ 通配符
页面传参时有时候会传一些通配符,例如* _,这些通配符如果作为模糊查询会查询出全部,这时候就需要我们对这些通配符进行拦截,每个方法进行拦截太过繁琐,拦截器又容易误杀,四处询问后发现有一种方法可以,看标题!!!
直接上代码,首先自定义一个标签
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WildcardReplace {
}
然后写一个切面类
@Slf4j
@Aspect
@Component
public class WildcardReplaceInterceptor {
@Around("execution (* com.linggong.admin.module.*..*.*(..))&&@annotation(wildcardReplace)")
public Object AroundAdvice(ProceedingJoinPoint point, WildcardReplace wildcardReplace) throws Throwable {
Object[] args = point.getArgs();
for(int i=0;i<args.length;i++){
if(args[i] instanceof String){
args[i] = args[i].toString().replace("%","\\%").replace("_","\\_");
}
}
return point.proceed(args);
}
}
使用时直接在方法上加上自定义注解即可
@WildcardReplace
@RequestMapping("/index")
public String index( @RequestParam(value = "storeName",required = false,defaultValue = "")String storeName) {
}
敲黑板,这种方法实际上是校验一个一个入参然后进行替换,所以入参一定要是基本类型,不要用实体接收。