79405130

Date: 2025-02-01 14:19:12
Score: 1.5
Natty:
Report link

I have tried other validations such as @Valid and @Validated, as well as field value annotations on the object, but nothing works. I am wondering if there is a way around this without having to write a custom deserializer instead of using @RequestBody or adding it as a false positive.

This has nothing to do with @RequestBody in particular.

You probably disabled (or didn't configure) CSRF in your security configuration.

more information about csrf can be found in the official spring security documentation

here is an example of a configuration file taken from the getting started page

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((requests) -> requests
                .requestMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
            )
            .formLogin((form) -> form
                .loginPage("/login")
                .permitAll()
            )
            .logout((logout) -> logout.permitAll());

        return http.build();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user =
             User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }
}

If you don't have this in any shape or form, I suggest you start there.

Reasons:
  • Blacklisted phrase (1): but nothing work
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Valid
  • User mentioned (0): @Validated
  • User mentioned (0): @RequestBody
  • Low reputation (1):
Posted by: Yuval Roth