Пример #1
0
  public static Validation checkValidate(
      List<ValidatorConfigBean> valList, Map<String, String[]> paramMap, HttpServletRequest req) {

    Validation validation = new Validation();

    if (valList == null || valList.isEmpty()) return validation;

    // 启动验证器
    for (int i = valList.size() - 1; i > -1; i--) {
      ValidatorConfigBean val = valList.get(i);
      ValidatorIF validator = null;
      if (!"".equals(val.getName())) {
        // 根据name属性实例化相关的验证器,如果找不到,就根据class属性实例化
        validator = ValidatorFactory.getValidator(val.getName());
        if (validator == null)
          try {
            validator = (ValidatorIF) Class.forName(val.getClazz()).newInstance();
          } catch (Exception e) {
            log.error(StringUtil.getExceptionString(e));
          }
      }

      if (validator == null) continue;

      Map<String, Map<String, String>> err = validator.validate(val, paramMap, req).getErrors();
      for (Entry<String, Map<String, String>> en : err.entrySet()) {
        String key = en.getKey();
        Map<String, String> value = en.getValue();
        if (validation.getErrors().containsKey(key)) validation.getErrors().get(key).putAll(value);
        else validation.getErrors().put(key, value);
      }
    }

    return validation;
  }
  public Validation validate(ValidatorConfigBean val, Context context) {
    Map<String, String> errMap = new HashMap<String, String>();

    for (FieldConfigBean fcb : val.getField()) {
      // 比如我现在验证 "name"参数值不能出现 "中国" 字眼
      String key = fcb.getName();
      String value = context.getQueryParamMap().get(key)[0]; // 这个是封装了request请求传过来的参数map
      if (value.indexOf("中国") != -1) errMap.put(key, fcb.getMessage());
    }

    Validation validation = new Validation();
    validation.getErrors().put(val.getName(), errMap);

    return validation;
  }
Пример #3
0
  @Validate({"user.authcode", "user.account", "user.password"})
  public Object doLoginAtPut(Validation val, Map<String, Object> model) {
    if (val.hasErr()) {
      model.put("valError", val.getAllErr());
      return UserCons.LOGIN_ACTION_RESULT();
    }

    List<Long> treeMenuPerms = new ArrayList<Long>();
    List<Long> navMenuPerms = new ArrayList<Long>();
    try {
      String _authCode =
          (String) MVC.ctx().getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
      User loginUser =
          userService.login(_authCode, CommonUtil.getIpAddr(MVC.ctx().getRequest()), user);

      // 登陆成功之后,将用户的权限信息查询出来放到session内存中
      /* 权限控制 */
      List<Role> roles = loginUser.getRoles();
      if (roles == null) roles = new ArrayList<Role>();

      for (Role role : roles) {
        Role _role = roleService.findTreeMenuByRoleId(role.getRoleId());
        /* 角色对应权限集合 */
        List<TreeMenu> tms = _role.getMenus();
        List<NavMenu> nms = _role.getNavMenus();

        if (tms != null) {
          for (TreeMenu tm : tms) treeMenuPerms.add(tm.getTreeMenuId());
        }
        if (nms != null) {
          for (NavMenu nm : nms) navMenuPerms.add(nm.getNavMenuId());
        }
      }

      loginUser.setTreeMenuPerms(treeMenuPerms);
      loginUser.setNavMenuPerms(navMenuPerms);

      MVC.ctx().getSession().setAttribute(UserCons.LOGIN_USER_ATTR_NAME(), loginUser);

    } catch (Exception e) {
      model.put(UserCons.LOGIN_ERR_ATTR_NAME(), e.getMessage());
      return UserCons.LOGIN_ACTION_RESULT();
    }

    return UserCons.LOGIN_SUCCESS_REDIRECT();
  }