Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I map validation errors? #486

Open
cj19 opened this issue Jul 8, 2024 · 0 comments
Open

How can I map validation errors? #486

cj19 opened this issue Jul 8, 2024 · 0 comments

Comments

@cj19
Copy link

cj19 commented Jul 8, 2024

I would like to override the default validation errors, but when I create a basic ControllerAdvice the handler does not get called.
My handler:


@ControllerAdvice
public class DefaultExceptionHandler implements ProblemHandling {

    @Override
    @ExceptionHandler(value = { ConstraintViolationException.class })
    public ResponseEntity<Problem> handleConstraintViolation(ConstraintViolationException exception, NativeWebRequest request) {
        return null;
    }
}

My endpoint:


    @PostMapping(value = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<JwtResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
        return authService.login(loginRequest);
    }

My security handler advice:

@ControllerAdvice
public class SecurityExceptionHandler implements SecurityAdviceTrait {
}

My websecurity config:


@Configuration
@EnableMethodSecurity
@Import(SecurityProblemSupport.class)
public class WebSecurityConfig {

    @Autowired
    private SecurityProblemSupport problemSupport;
....


    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.cors(cors -> cors.configurationSource(corsConfigurationSource()))
                .csrf(AbstractHttpConfigurer::disable)
                .exceptionHandling(exception -> exception.authenticationEntryPoint(problemSupport)
                        .accessDeniedHandler(problemSupport))
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authorizeHttpRequests(auth -> auth.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                        .requestMatchers("/api/auth/**").permitAll().anyRequest().authenticated());

        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }
}

What am I doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant