Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - how do I get the base url of a request

I'm running a spring boot and a few of my domain services need to know the domain name.

I know that I could capture it at the application layer in the controller method like this:

 @RequestMapping(value="/myMapping",method = RequestMethod.POST)
 public ModelandView myAction(HttpServletRequest request) {

 }

OR that, if I were running a traditional web application with a war that I could configure a listener like this:

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

And access it like this:

((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()

But I do not want to pass around arguments like I would if I captured it at the controller, and I am not running a war with a web.xml.

How can I have my cake and eat it too?

like image 722
Lurk21 Avatar asked Jan 20 '14 16:01

Lurk21


2 Answers

If you use Spring Security, you could store this info in SecurityContext. The trick is getDetails() method, you can put anything you want in there. I personally use a custom object that stores basic info I need for the current user. This example just puts a simple string representing your domain:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Component
public class DomainInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String domain = null; // TODO extra domain from request here
        SecurityContext context = SecurityContextHolder.getContext();
        AbstractAuthenticationToken authentication = (AbstractAuthenticationToken) context.getAuthentication();
        authentication.setDetails(domain);
        return true;
    }
}

Then to retrieve the domain (for the current request) anywhere in your app you'd do this:

String domain = SecurityContextHolder.getContext().getAuthentication().getDetails().toString();
like image 152
SergeyB Avatar answered Sep 20 '22 18:09

SergeyB


This is what I did:

import javax.servlet.http.HttpServletRequest;

import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class RequestFetcher {
    public static HttpServletRequest getCurrentRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        Assert.state(requestAttributes != null, "Could not find current request via RequestContextHolder");
        Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
        HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
        Assert.state(servletRequest != null, "Could not find current HttpServletRequest");
        return servletRequest;
    }
}
like image 34
Lurk21 Avatar answered Sep 19 '22 18:09

Lurk21