Get Referer URL in Spring MVC

ⅰ亾dé卋堺 提交于 2019-12-02 20:17:20

It's available as HTTP request header with the name referer (yes, with the misspelling which should have been referrer).

String referrer = request.getHeader("referer");
// ...

Here the request is the HttpServletRequest which is available in Spring beans in several ways, among others by an @AutoWired.

Please keep in mind that this is a client-controlled value which can easily be spoofed/omitted by the client.

In Spring MVC 3 you can get it from request, as @BalusC already said:

public ModelAndView doSomething(final HttpServletRequest request) {
    final String referer = request.getHeader("referer");
    ...
}

but there also exists special annotation @RequestHeader which allow to simplify your code to

public ModelAndView doSomething(@RequestHeader(value = "referer", required = false) final String referer) {
    ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!