我需要记录触发NOT FOUND 404错误的URI。我使用.getHeader(HttpHeaders.REFERER),我得到的结果是空的。同样,我尝试使用getRequestURL(),并得到@RequestMapping方法"/error“的路径。
例如:如果用户输入: mywebsite.com/fdsfihdif,但没有这样的地址,我应该会在日志中看到这样的内容:NOT FOUND 404用户john@yahoo.com尝试访问此URI : mywebsite.com/fdsfihdif
这是我的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";
}
}
下面是输出:
2018-06-22 18:48:59.507错误nio-9090-exec-7 c.b.g.c.CustomErrorController
找不到消息==> 404用户dumi7ru尝试访问此:空**
发布于 2018-06-23 03:27:28
您可以获取原始URI,如下所示
String originalUri = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
堆栈web.xml 404 redirect to servlet, how to get the original URI?上的类似查询
https://stackoverflow.com/questions/50993181
复制相似问题