gpt4 book ai didi

java - Spring不进入自定义 validator

转载 作者:行者123 更新时间:2023-12-04 07:56:55 24 4
gpt4 key购买 nike

我添加了 4 个自定义 validator 来在我的 Controller 中进行一些检查。其中 3 个工作但 ValidObjectIdentity一个不起作用。
这是我的constraint-mapping.xml

<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.1.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">

<bean class="org.springframework.security.acls.domain.AccessControlEntryImpl" ignore-annotations="true">
<field name="sid">
<constraint annotation="javax.validation.constraints.NotNull"/>
<constraint annotation="package.validation.SidExists"/>
<constraint annotation="package.validation.NonMatchingSid"/>
</field>
<field name="acl">
<constraint annotation="javax.validation.constraints.NotNull"/>
</field>
</bean>

<bean class="org.springframework.security.acls.domain.AclImpl">
<field name="objectIdentity">
<constraint annotation="javax.validation.constraints.NotNull"/>
<constraint annotation="package.validation.ValidObjectIdentity"/>
</field>
</bean>

<bean class="package.AccessControlEntryController">
<method name="create">
<parameter type="org.springframework.security.acls.model.AccessControlEntry">
<constraint annotation="package.validation.ValidEntry"/>
</parameter>
</method>
</bean>
</constraint-mappings>
@RestController
@RequestMapping(value = "/acl")
@Validated
public class AccessControlEntryController {

@ResponseBody
@RequestMapping(value = "/entry", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public AccessControlEntry create(@Valid @RequestBody AccessControlEntry entry){
return aclService.createAccessControlEntry(entry);
}
}
作为引用,我将把 AccessControlEntryImpl 的结构放在这里和 AclImpl
public class AccessControlEntryImpl implements AccessControlEntry, AuditableAccessControlEntry {

private final Acl acl;
private Permission permission;
private final Serializable id;
private final Sid sid;
private boolean auditFailure = false;
private boolean auditSuccess = false;
private final boolean granting;

}
public class AclImpl implements Acl, MutableAcl, AuditableAcl, OwnershipAcl {
private Acl parentAcl;
private transient AclAuthorizationStrategy aclAuthorizationStrategy;
private transient PermissionGrantingStrategy permissionGrantingStrategy;
private final List<AccessControlEntry> aces = new ArrayList<>();
private ObjectIdentity objectIdentity;
private Serializable id;
private Sid owner; // OwnershipAcl
private List<Sid> loadedSids = null;
}
我可以看到工作 validator 与 AccessControlEntry 直接相关,但 ObjectIdentity与类(class)相关 Acl .即使 AccessControlEntryAcl类本身它不会去 validator 。有没有办法验证 ObjectIdentity像其他人一样?
这是您想查看的 validator
@Target(value = ElementType.FIELD)
@Retention(value = RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ValidObjectIdentityValidator.class)
public @interface ValidObjectIdentity {

String message() default "{package.ValidObjectIdentity.message}";

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

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

public class ValidObjectIdentityValidator implements ConstraintValidator<ValidObjectIdentity, ObjectIdentity> {

@Override
public boolean isValid(ObjectIdentity identity, ConstraintValidatorContext context) {
// Logic
}
}

最佳答案

我知道您希望 ValidObjectIdentity验证时触发 AccessControlEntryImpl ?
默认情况下不会发生级联验证。您需要一个 @Valid AccessControlEntryImpl.acl上的注释或 <valid/>对应 XML 映射的节点:

<field name="acl">
<valid/>
</field>

关于java - Spring不进入自定义 validator ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66655777/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com