Spring Security @PreAuthorize将validator bean设置为null,同时将其添加到WebDataBinder

时间:2018-06-12 00:47:01

标签: spring spring-mvc spring-boot spring-security

我正在使用@EnableGlobalMethodSecurity,但是当我使用@PreAuthorize注释任何控制器方法时,在将验证器添加到数据绑定器时,bean验证器bean变为null。这是我的代码:

@EnableGlobalMethodSecurity(prePostEnabled=true) //to enable method level security


@RestController
@RequestMapping("/capacityDelegations")
public class CapacityDelegationController implements Serializable{


/**
 * 
 */
private static final long serialVersionUID = 6878225451248023554L;


private CapacityDelegationHandler capacityDelegationHandler;
private CapacityDelegationChangeStatusRequestValidator capacityDelegationChangeStatusRequestValidator;


public CapacityDelegationController(CapacityDelegationHandler capacityDelegationHandler, CapacityDelegationChangeStatusRequestValidator capacityDelegationChangeStatusRequestValidator) {
    // checked here in debug mode to make sure that validator's bean injected properly
    this.capacityDelegationChangeStatusRequestValidator = capacityDelegationChangeStatusRequestValidator; 
    this.capacityDelegationHandler = capacityDelegationHandler;

}

@InitBinder("capacityDelegationChangeStatusRequest")
private void initCapacityDelegationChangeStatusRequestBinder(WebDataBinder binder) {
    //in debug mode when a PUT request received the value of capacityDelegationChangeStatusRequestValidator turns to null if @PreAuthorize applied and not null if removed
binder.addValidators(this.capacityDelegationChangeStatusRequestValidator); 
}



@PutMapping(path = "/serial={serial}/submission")
@PreAuthorize("hasRole('ROLE_OFFICER')")
public ResponseEntity<?> submit(@Valid @RequestBody CapacityDelegationChangeStatusRequest capacityDelegationChangeStatusRequest) throws JudicialWarrantException {
    return capacityDelegationHandler.submit(null, capacityDelegationChangeStatusRequest);
}



}

我正在使用Spring boot 5.0.5

0 个答案:

没有答案