1,定义注解名称接口
/** * 使用该注解不用再MD5转换了 * * @author adonis * */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Password { String value() default "password"; }
2,实现HandlerMethodArgumentResolver 接口
public class PasswordAnnotationResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.getParameterAnnotation(Password.class) != null; } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest request = (HttpServletRequest) webRequest .getNativeRequest(); Password passwordAnno = parameter .getParameterAnnotation(Password.class); String password = request.getParameter(passwordAnno.value()); return MD5Util.MD5(password); } }
3,自定义注解驱动注册
<mvc:annotation-driven > <mvc:argument-resolvers> <bean class="xxx.xxxx.xxxx.PasswordAnnotationResolver"/> </mvc:argument-resolvers> </mvc:annotation-driven>
4,使用
public Map<String, Object> login(
HttpServletRequest request,//
@RequestParam("username") String userName,//
@Password String password, //自定义的注解。此时password已经是加密的了
@RequestParam("randCheckCode") String randCheckCode) {
//判断逻辑
}