1

I need to get in logs the URI which triggered the [NOT FOUND 404] error. I use .getHeader(HttpHeaders.REFERER) and i'm getting null. The same i tried to use getRequestURL() and i'm getting the path of @RequestMapping method "/error".

For example: If a user enters: mywebsite.com/fdsfihdif and there is no such address, i should see this in logs like this [NOT FOUND 404] User [john@yahoo.com] tried to access this URI : [mywebsite.com/fdsfihdif]

Photo example

Here is my errorControler:

@Controller
public class CustomErrorController implements ErrorController {

private static final Logger log = LogManager.getLogger(CustomErrorController.class);


@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
    Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (status != null) {
        Integer statusCode = Integer.valueOf(status.toString());

        if (statusCode == HttpStatus.NOT_FOUND.value()) {
            //404 with the wrong URI entered by user

            log.error("[NOT FOUND 404] User [" + auth.getName() + "] tried to access this URL : ["+ request.getRequestURL() +"] : [" + request.getHeader(HttpHeaders.REFERER) + "]";

            return "errors/404";

        } else {
            //something else
            return "errors/error";
        }
    }
    return "error";
}

@Override
public String getErrorPath() {
    return "/error";
}
}

Here is the output:

2018-06-22 18:48:59.507 ERROR [nio-9090-exec-7] c.b.g.c.CustomErrorController
MSG ==> [NOT FOUND 404] User [dumi7ru] tried to access this URL : [http://localhost:9090/error] : [null]

dumi7ru
  • 55
  • 3
  • 11
  • And what is your question? What is the most probable reason for it to be null? Have you read the javadoc? https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getHeader-java.lang.String- – JB Nizet Jun 22 '18 at 17:47
  • @JB Nizet i want to obtain the Uri like i mentioned in my example.How can i do this? Thank you – dumi7ru Jun 22 '18 at 17:56
  • The way you did. Just don't expect every request to have such a header. Sometimes, there just isn't one because people go directly to a URL. Sometimes there isn't one because people don't want to tell you where they come from (for very good privacy reasons). Sometimes they could be removed by proxies. – JB Nizet Jun 22 '18 at 18:00
  • @JB Nizet, i think i did not ask the question correctly... So i don't need to know where they are coming from, i need the URL they are going to... how you mentioned _**people go directly to a URL**_ , so i need this URL. Thanks in advance. – dumi7ru Jun 22 '18 at 18:14
  • Then don't get the referrer URL from the request. Get the URL of the request. https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getRequestURI-- – JB Nizet Jun 22 '18 at 18:44
  • I tried before and i'm getting /path from @RequestMapping method. I added the output upper. – dumi7ru Jun 22 '18 at 18:50

1 Answers1

1

You can get original URI as

String originalUri = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);

Similar query on stack web.xml 404 redirect to servlet, how to get the original URI?

Noman Khan
  • 920
  • 5
  • 12