SpringBoot⾃定义注解统⼀处理⼊参加解密脱敏等⽅法(⼀)基于HandlerMethodArgumentResolver实现,针对GET和POST请求分别作不同的处理。
1、定义需要处理的⼊参类型
public enum FieldTypeEnum {
DEFAULT,
PLATE_NUM,//车牌号
ID_CARD,//⾝份证号
PHONE;//⼿机号
}
2、定义注解
/**
* 需重新赋值的注解
*/
@Documented
@Inherited
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Reassign {
FieldTypeEnum type() default FieldTypeEnum.DEFAULT;
}
3、基于HandlerMethodArgumentResolver,实现⼊参的处理
public class ParamResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.hasParameterAnnotation(Reassign.class);
}
@Override
public Object resolveArgument(MethodParameter methodParameter,
ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest,
springboot aopWebDataBinderFactory webDataBinderFactory) throws Exception {
HttpServletRequest servletRequest = ((ServletWebRequest) nativeWebRequest).getRequest();    if ("POST".Method())) {
String body = ((RequestWrapper) servletRequest).getBody();
Object obj = JSONObject.parseObject(body, Parameter().getType());
Field[] fields = Class().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Reassign.class)) {
field.setAccessible(true);
Object value = (obj);
if (value instanceof String) {
FieldTypeEnum typeEnum = Annotation(Reassign.class).type();
field.set(obj, parse((String) value, typeEnum));
}
}
}
return obj;
}
if ("GET".Method())) {
String param = ParameterName());
if (Parameter().isAnnotationPresent(Reassign.class)) {
FieldTypeEnum typeEnum = Parameter().getAnnotation(Reassign.class)            .type();
return parse(param, typeEnum);
}
return param;
}
return null;
}
private String parse(String value, FieldTypeEnum typeEnum) {
String result = null;
switch (typeEnum) {
case PHONE:
if (value.length() >= 32) {
//TODO 解密
} else if (ains("****")) {
//TODO 脱敏⼿机号,需恢复(脱敏时,将⼿机号缓存,恢复时从缓存恢复)
}
result = value;
break;
case ID_CARD:
result = value;
break;
default:
result = value;
break;
}
return result;
}
}
4、添加WebMvcConfig
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new ParamResolver());
}
}
5、controller层使⽤⼀:GET
@GetMapping("/testGet")
@ResponseBody
public ApiResponse testGet(@Reassign(type = FieldTypeEnum.PHONE) String phone,
@Reassign(type = FieldTypeEnum.ID_CARD) String id) {
return ApiResponse.success(phone + "," + id);
}
controller层使⽤⼆:POST
@PostMapping("/testPost")
@ResponseBody
public ApiResponse testPost(@Reassign User user) {
return ApiResponse.Mobile());
}
注意User对象要对需要处理的字段加上注解:
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ⼿机号
*/
@Reassign(type = FieldTypeEnum.PHONE)
private String mobile;
}
注意:使⽤该⽅式,⾃定义注解⽆法与@RequestParam以及@RequestBody⼀起使⽤,并且,在GET请求时,只能接受String类型的参数。
在下⼀篇博客中,我将基于AOP的⽅式来实现。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。