java - JSR-303 bean 验证 - 避免绕过字段

标签 java spring spring-security bean-validation

假设我们有这个 bean

public class Bean {
   @NotBlank(groups = {CheckForEmployee.class, CheckForAdmins.class})
   private String sth1;

   @NotBlank(groups = {CheckForAdmins.class})
   private String sth2;

   //getters and setters
}

现在在员工用户界面中我们有这样的内容:

<input name="sth1" type="text" />

这是给管理员的:

<input name="sth1" type="text" />
<input name="sth2" type="text" />

我们都知道像 Spring 这样的框架可以将这些值绑定(bind)到 Bean 对象,例如在 Spring 中我们有这样的东西:

public ModelAndView method1 (@Validated({CheckForEmployee.class})@ModelAttribute Bean bean){...} 
//For Employee
AND
public ModelAndView method2 (@Validated({CheckForAdmin.class})@ModelAttribute Bean bean){...} 
//For Admin

现在问题是: 如果恶意员工知道管理员有一个名为“sth2”的字段,他可以手动生成发布请求并为sth2输入值并将其发送到服务器。

Spring 将绑定(bind)这个值,因为:

  1. 该字段位于 Bean 类内部
  2. 没有对员工的 sth2 进行验证

您对此有何解决方案?

Suppose CSRF is disabled and we still want to use spring binding

最佳答案

public class Bean {

    @NotBlank(groups = Validator.CheckForEmployee.class)
    private String sth1;


    @BlockAccess(groups = Validator.CheckForEmployee.class)
    @NotBlank(groups = Validator.CheckForAdmin.class)
    private String sth2;

    //getters and setters
}

public class Validator {
    public interface CheckForEmployee{}
    public interface CheckForAdmin{}
}


@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = BlockAccessImpl.class)
@Documented
public @interface BlockAccess {
    String message() default "Access is denied!";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

}

public class BlockAccessImpl implements ConstraintValidator<BlockAccess, Object> {

    @Override
    public void initialize(BlockAccess constraintAnnotation) {

    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        return false;
    }
}

public class Test {

    public static void main(String[] args) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Bean b = new Bean();
        b.setSth1("Hi");
        b.setSth2("Bye");

        Set<ConstraintViolation<Bean>> s = validator.validate(b, Validator.CheckForEmployee.class);

        for (ConstraintViolation<Bean> f : s) {
            System.out.println(f.getPropertyPath() + " " + f.getMessage());
        }
    }
}

sth2 Access is denied!

关于java - JSR-303 bean 验证 - 避免绕过字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54701826/

相关文章:

java - GWT:语言环境没有改变?

java - 绘制和缩放图像的好习惯?

java - Spring Security、Rest 身份验证和 CSRF

spring security 无法找到 XML 模式命名空间的 espaceHandler

java - 使用多个表单在 View 上提交表单时出现 Spring "Neither BindingResult nor plain target object for bean name"错误

spring-security - Spring 因 userinfo 端点返回已签名的 JWT 而失败

Spring security 不支持的配置属性

grails - Grails Acegi:更新用户名

java - 从网络浏览器获取图像并在应用程序中进行编辑

java - 组合上下文:property-override and PropertyPlaceholderConfigurer