I am using spring boot and I have enabled global method protection in the WebSecurityConfigurerAdapter on
@EnableGlobalMethodSecurity(prePostEnabled = true, order = Ordered.HIGHEST_PRECEDENCE)
And below is my controller code
@PreAuthorize("hasAnyRole('admin') or principal.id == id") @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public User updateUser(@PathVariable("id") String id, @Valid @RequestBody UserDto userDto) { ....}
However, when a non-administrator user tries to execute a PUT request, the JSR303 validator will exit before @PreAuthorize. For example, a non-administrator user would get something like "name required" instead of "access denied". But after the user provided the first name variable to pass the validator, access was returned.
Does anyone know how to get @PreAuthorize to be checked before @Valid or @Validated?
And I have to use this kind of authorization at the method level instead of URL-based authorization in order to do some complicated rule checking.
source share