0

I have a Spring Boot & Spring Security application running, which should now get multi tenancy support.

I'd like to keep things as simple as possible. Though I'd like to determine the the database by path variable or request parameter. (the Apache Web Server in front of the Spring Boot app will handle that, i.e. it maps from subdomain to either path variable or request parameter).

Now I need a way to grab the first path variable (or a specific request param) before Spring calls the controller, and of course the value must be stored somewhere, so I can access it when I need to choose the right database.

So either (depending on what's possible / easier)

  1. http://localhost:8080/customer1 (which I'd prefer) or
  2. http://localhost:8080?_customer=customer1

should simply call the @Controller with @RequestMapping("") and the value customer1 should be stored somewhere for that request.

I know that 2. might be simpler, because it will already hit the right @Controller, but I'd prefer 1. somehow.

Thank you!

EDIT:

I just recognized, that HandlerInterceptor doesn't work as expected, because Spring Security always handles the requests first. Though I need an Interceptor that handles it before Spring Security kicks in.

Benjamin M
  • 23,599
  • 32
  • 121
  • 201

1 Answers1

3

You can make use of org.springframework.web.servlet.HandlerInterceptor. Implement the logic to store the values in request via preHandle method.

Configure it in spring config file as below

<mvc:interceptors>
  <bean class="com.blah.interceptor.SomeInterceptor"/>
</mvc:interceptors> 

In case requests are to be intercepted based on path, use below config instead

<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/customer1" />
        <bean class="com.blah.interceptor.SomeInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>
Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59
  • Thank your for your answer, I just tried it and found that I need some other kind of interceptor, because I use Spring Security. I need the interceptor to kick in **before anything** else. This means: even before Spring Security. Do you know a solution for this, too? – Benjamin M May 27 '15 at 12:54
  • @BenjaminM Okies. Let me get back on this. However I am curious why was this question deleted even though the problem still persists ? – Bond - Java Bond May 28 '15 at 08:59
  • Because I recognized, that the question had nothing to do with my problem. Though I will not be able to verify if it's useful for the thing I had to solve. Those Interceptors get applied somewhere inbetween, but I needed to intercept the request at the very beginning, which resulted in using this: http://stackoverflow.com/questions/30487553/spring-boot-inject-a-custom-context-path – Benjamin M May 28 '15 at 10:53