zoukankan      html  css  js  c++  java
  • 参数注解1

    定义注解类:

    @Target({ElementType.PARAMETER})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface StrictEncryptedIdParam {
        String value();
    
        String encryptedIdType();
    
        boolean required() default true;
    
        String defaultValue() default "
    		
    		
    ue000ue001ue002
    				
    ";
    }

    定义解析器:

    public final class StrictEncryptedIdAnnotationResolver implements HandlerMethodArgumentResolver {
    
        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            return parameter.hasParameterAnnotation(StrictEncryptedIdParam.class);
        }
    
        @Override
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
            if (parameter.getParameterType() != Long.class
                    && parameter.getParameterType() != long.class) {
                throw new RuntimeException("EncryptedIdAnnotation must be on long field");
            }
            HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
            String idType = "";
            String paramName = StringUtils.EMPTY;
            boolean isRequired = false;
            String defaultValue = "";
            if (parameter.hasParameterAnnotation(StrictEncryptedIdParam.class)) {
                StrictEncryptedIdParam encryptedIdParam = parameter.getParameterAnnotation(StrictEncryptedIdParam.class);
                idType = encryptedIdParam.encryptedIdType();
                paramName = encryptedIdParam.value();
                isRequired = encryptedIdParam.required();
                defaultValue = encryptedIdParam.defaultValue();
            }
            if (StringUtils.isEmpty(paramName)) {
                paramName = parameter.getParameterName();
            }
            String parameterValue = request.getParameter(paramName);
            if (StringUtils.isEmpty(parameterValue) || StringUtils.isEmpty(idType)) {
                if (!defaultValue.equals(ValueConstants.DEFAULT_NONE)) {
                    return Long.parseLong(defaultValue);
                }
                if (isRequired) {
                    throw new MissingServletRequestParameterException(paramName,
                            parameter.getParameterType().getTypeName());
                }
                return 0L;
            }
            if (StringUtils.isNumeric(parameterValue)) {
                throw new IllegalArgumentException("参数错误");
            }
            if (parameterValue.length() < 8) {
                throw new IllegalArgumentException("参数错误");
    
            }
            return resolveLong(parameterValue, idType, paramName);
        }
    
        private long resolveLong(String parameterValue, String idType, String paramName) {
            long decryptedId = IdEncryptor.decrypt(parameterValue, EncryptedIdType.of(idType));
            if (!StringUtils.equals(String.valueOf(decryptedId), parameterValue)) {
                CommonWebScope.paramBundle().putDecryptedIdIntoParams(paramName, decryptedId);
            }
            return decryptedId;
        }
    }
  • 相关阅读:
    Ubuntu 14.04设置开机启动脚本的方法
    python 筛选
    分段压缩
    ubuntu 16.04 启用root用户方法
    Ubuntu 16.04 设置MySQL远程访问权限
    [分享]在ubuntu9.10下实现开机自动登录并运行自己的图形程序
    ubuntu live cd修复grub引导项
    安装dcm4chee-arc-light-5.4.1-mysql步骤
    数据库学习--wildfly配置postgreSQL数据源
    wildfly配置PostgreSQL数据源
  • 原文地址:https://www.cnblogs.com/koushr/p/11907842.html
Copyright © 2011-2022 走看看