Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Spring Security and Error Handling

I'm using ResponseEntityExceptionHandler for global handling the error and almost working normal, except I want to handle wrong request with spring. By any logic overriding handleNoSuchRequestHandlingMethod should handle this, but insted of handling always get

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

Apache Tomcat/7.0.37

I just got this when enable debuging in console:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI

just to clarify by handling I mean I'm returning JSON.

any idea how to handle this?

like image 493
Zemzela Avatar asked Aug 19 '13 20:08

Zemzela


People also ask

Can we use Spring Security with Spring MVC?

To enable Spring Security integration with Spring MVC add the @EnableWebSecurity annotation to your configuration. Spring Security provides the configuration using Spring MVC's WebMvcConfigurer.

How do you handle authentication and exception handling in Spring Boot and Spring MVC applications?

Spring MVC provides exception handling for your web application to make sure you are sending your own exception page instead of the server-generated exception to the user. The @ExceptionHandler annotation is used to detect certain runtime exceptions and send responses according to the exception.

How do you handle security exceptions in Spring?

Spring security exceptions can be directly handled by adding custom filters and constructing the response body. To handle these exceptions at a global level via @ExceptionHandler and @ControllerAdvice, we need a custom implementation of AuthenticationEntryPoint.

What is the difference between ControllerAdvice and RestControllerAdvice?

The differences between @RestControllerAdvice and @ControllerAdvice is : @RestControllerAdvice = @ControllerAdvice + @ResponseBody . - we can use in REST web services. @ControllerAdvice - We can use in both MVC and Rest web services, need to provide the ResponseBody if we use this in Rest web services.


1 Answers

The reason is right there, in the DispatcherServlet class; it sends error response without bothering to call exception handler (by default).

Since 4.0.0.RELEASE this behaviour can be simply changed with throwExceptionIfNoHandlerFound parameter:

Set whether to throw a NoHandlerFoundException when no Handler was found for this request. This exception can then be caught with a HandlerExceptionResolver or an @ExceptionHandler controller method.

XML configuration:

<servlet>
    <servlet-name>rest-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>throwExceptionIfNoHandlerFound</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>

Java-based configuration:

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setInitParameter("throwExceptionIfNoHandlerFound", "true");
    }
    ...
}

Then NoHandlerFoundException can be handled like this:

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    ResponseEntity handleNoHandlerFoundException(NoHandlerFoundException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {
        // return whatever you want
    }
}
like image 169
Jakub Jirutka Avatar answered Oct 15 '22 14:10

Jakub Jirutka