• spring mvc自定义注解--登录时密码加密注解


    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) {
    
      //判断逻辑  
    }
    
  • 相关阅读:
    禅道
    centos7 安装redis 出现cc: command not found错误解决
    Linux 安装 redis
    vuex store modules
    vuex store 改造
    vuex store
    Vue axios
    Vue keep-alive
    vue 路由守卫
    vue-router 参数传递
  • 原文地址:https://www.cnblogs.com/yimiyan/p/4334762.html
Copyright © 2020-2023  润新知