我正在使用@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