Why is WebDataBinder used when Jackson is/should be used for deserialization (which breaks correct order of validation/authorization)?

My original requirement is that I want to use @EnableMethodSecurity, but when I call @RestController method with invalid payload and user missing required role, the validation takes precedence. So instead of getting 403/401 I’m getting 400 revealing something about unauthorized endpoint. That’s incorrect.

I found out, that in my case it’s because validation isn’t part of AOP proxy chain, but it’s triggered by org.springframework.web.method.support.InvocableHandlerMethod#invokeForRequest inside depths of call getMethodArgumentValues.

The workaround(well if it’s even workaround) here is straightforward. Disable the validator for WebDataBinder and annotate the controller with @Validated. Doing so the binder won’t throw MethodArgumentNotValidException and bean validation will be called correctly by spring in response to processing annotation @Validated.

My first question is: 1. why is WebDataBinder even used if I’m using jackson library for deserialization? Or am I? Well at least I’d like to and expect it to be used. Is there some mistake? I don’t know if it’s correct, I think I saw some notice, that it should not be used…

minimal working example(check master branch)

Excerpts from MWE(I can publish it later, I’m missing some tests yet):

Controller class:

@RestController
@RequestMapping("/someController")
public class SomeController {

    //    @RolesAllowed(ROLE1)
    @RolesAllowed(ROLE2)
    @PostMapping("search")
    public String contactSearch(@Valid @RequestBody SearchRequest searchRequest) {
        return "OK";
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Getter
    @Setter
    public static class SearchRequest {

        @NotNull
        @PositiveOrZero
        private Long userId;

    }

app/security definition:

@SpringBootApplication
@EnableWebSecurity
@EnableMethodSecurity
public class IsWebBinderUsedToBindJacksonDtoRequestBodyApplication {

    public static final String ROLE1 = "ROLE1";
    public static final String ROLE2 = "ROLE2";

    public static void main(String[] args) {
        SpringApplication.run(IsWebBinderUsedToBindJacksonDtoRequestBodyApplication.class, args);
    }

    @Bean
    public UserDetailsService userDetailsService() {
        // Define an in-memory user with roles
        UserDetails user1 = User.withDefaultPasswordEncoder()
                .username("user1")
                .password("password")
                .roles(ROLE1)
                .build();

        UserDetails user2 = User.withDefaultPasswordEncoder()
                .username("user2")
                .password("password")
                .roles(ROLE2)
                .build();

        return new InMemoryUserDetailsManager(user1, user2);
    }

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
                .csrf(AbstractHttpConfigurer::disable)
                .authorizeHttpRequests(a -> a.anyRequest().permitAll())
                .httpBasic(conf -> conf.realmName("aaa"))
                .build();
    }
}

disabling of validation can be done using:

@InitBinder
public void initUserBinder(WebDataBinder binder) {
    //no validation whatsoever!
    binder.setExcludedValidators(e->true);
}

and I have the second question about this @InitBinder. If there isn’t better way and I have to use this, how do I configure WebDataBinder globally to disable the validator as shown (or provide some benevolet Validator implementation, or to do any other customization)? I kinda cannot find the solution for springboot 3.

5

I’m not sure if this behavior is intentional or not, or whether the binder should be used or not in this case. But I saw several threads asking for fixing this without any response, so I will add my here.

The cause of problem is binder calling the validation of input data before calling authorization proxy for method level validation( here: org.springframework.web.method.support.InvocableHandlerMethod#invokeForRequest), as demonstrated in minimal working example.

One possible solution is to use @Validated to enable spring validation using AOP proxy and either disable binder validation using @InitBinder or globally using advice:

@ControllerAdvice
@Slf4j
@AllArgsConstructor
public final class BinderShouldNotValidateAdvice {

    //global disabling data binder premature validation
    @InitBinder
    public void initUserBinder(WebDataBinder binder) {
        // customization specific to binding a User
        binder.setExcludedValidators(e->true);
    }

}

It does not seem, that spring handles ConstraintViolationException automatically. It will fail producing stacktrace to log and http 500 in response without failure description. So you need to implement your own exception handler as shown in MWE as well.

This is just one potential solution, if someone know better one, please let me know. Thank you!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật