Java handle invalid request Mapping urls including decodings

你说的曾经没有我的故事 提交于 2021-02-08 11:29:41

问题


in my apiController, I have

@Controller
@RequestMapping("api/v1/myservice")
@Slf4j
public class APIController {
    @RequestMapping(value = "/validAPI1", method = RequestMethod.GET)
    @ResponseBody
    public String validAPI1() {
        return "success";
    }
}

I want to catch invalid incoming API requests, such as /api/v1/myservice/random124 , and this can be done by adding a method at the end:

@RequestMapping(value = "/**", method = RequestMethod.GET)
@ResponseBody
public String handleInvalidAPIReq() {
    return "watched and handled";
}

This has 2 problems, does not catch special characters such as % in URL, and does not take care of input URLs. An example I'm trying to fight against: /api/v1/myservice/%uff0e%uff0e%u2215%7bFILE%7d/abc12

Are there existing libraries or methods I can use to catch invalid incoming API requests please? Could you help with examples? Appreiate tons.


回答1:


The common solution for handling bad requests is to set throwExceptionIfNoHandlerFound in org.springframework.web.servlet.DispatcherServlet class. You didn't mentioned which type of application you wrote, simple war archive with spring-mvc dependency or spring boot styled application.

For simple spring-mvc application, you can set that property in web.xml file:

    ...
    <servlet>
        <servlet-name>spring</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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    ...

For spring boot application, you just need to set config property:

spring.mvc.throw-exception-if-no-handler-found=true

In both cases, DispatcherServlet will throw NoHandlerFoundException exception, in order to handle this exception, you need to add the ExceptionHandler class as follows::

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(NoHandlerFoundException.class)
    public ResponseEntity<String> handleNoHandlerFoundException(NoHandlerFoundException ex) {
        return ResponseEntity.status(404).body("Error occurred");
    }

}

Note: When you enable throwExceptionIfNoHandlerFound property, you should remember about static resources, and add custom mappings for them, otherwise the resources will not be found. Configure static resources (Spring Boot):

spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/resources/static/

or if there are no static resources:

spring.resources.add-mappings=false


来源:https://stackoverflow.com/questions/65677104/java-handle-invalid-request-mapping-urls-including-decodings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!