Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Referer URL in Spring MVC

How can I get the referer URL in Spring MVC Controller?

like image 405
Mike Flynn Avatar asked Apr 07 '11 23:04

Mike Flynn


People also ask

How do I find my spring boot referrer URL?

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"); ... } public ModelAndView doSomething(@RequestHeader(value = "referer", required = false) final String referer) { ... }

How do I get referer from HTTP request?

It's available in the HTTP referer header. You can get it in a servlet as follows: String referrer = request. getHeader("referer"); // Yes, with the legendary misspelling.

What is URL referrer?

The address of the webpage where a person clicked a link that sent them to your page. The referrer is the webpage that sends visitors to your site using a link. In other words, it's the webpage that a person was on right before they landed on your page.

What is request referer in rails?

request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page (link) More information here. Regarding your question, it is simply returning 'dashboard' if found in request.referer .


2 Answers

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.

like image 45
BalusC Avatar answered Sep 22 '22 16:09

BalusC


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) {     ... } 
like image 199
Slava Semushin Avatar answered Sep 18 '22 16:09

Slava Semushin