Spring自定义方法注释不起作用

时间:2018-09-03 11:29:57

标签: java spring-boot spring-security annotations constraints

我创建了一个自定义的SecurityAnnotation注释类

@Constraint(validatedBy = SecurityAnnotationValidator.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface SecurityAnnotation {

    EmployeeRoles[] employeeRoles();

    String message();

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

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

我有一个约束验证器类

public class SecurityAnnotationValidator implements ConstraintValidator<SecurityAnnotation, String> {

private EmployeeRoles[] employeeRoles;

@Autowired
EmployeeService employeeService;

@Override
public void initialize(SecurityAnnotation securityAnnotation) {
    employeeRoles = securityAnnotation.employeeRoles();
}

@Override
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
    //logic goes here
    return false;
}
}

在我的控制器中,我注释了一个方法

@RequestMapping( method = RequestMethod.POST)
@SecurityAnnotation(employeeRoles = {EmployeeRoles.HR}, message = "Invalid User")
public HrmsResponse create(@Valid @RequestBody CreateEmployee createEmployee){
    Employee employee = employeeService.createEmployee(createEmployee);
    return HrmsResponse.buildSuccessResponse(employee);
}

但是我的注释的isValid函数没有被调用,被谷歌搜索并且没有找到任何结果。 任何帮助,将不胜感激。预先感谢。

0 个答案:

没有答案