gpt4 book ai didi

java - HttpServletRequest getHeader(HttpHeaders.REFERER) 返回 NULL

转载 作者:行者123 更新时间:2023-11-30 06:07:32 39 4
gpt4 key购买 nike

我需要在日志中获取触发 [NOT FOUND 404] 错误的 URI。我使用 .getHeader(HttpHeaders.REFERER) 并且我得到了 null。同样,我尝试使用 getRequestURL() 并且我得到了 @RequestMapping 方法“/error”的路径。

例如:如果用户输入:mywebsite.com/fdsfihdif 并且没有这样的地址,我应该在日志中看到这样的 [NOT FOUND 404] 用户 [john@yahoo.com] 尝试访问此 URI: [mywebsite.com/fdsfihdif]

Photo example

这是我的错误 Controller :

@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 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]

最佳答案

您可以获取原始URI为

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

堆栈上的类似查询 web.xml 404 redirect to servlet, how to get the original URI?

关于java - HttpServletRequest getHeader(HttpHeaders.REFERER) 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50993181/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com