0

how to configure the application so that each client has its context?

Example:

  1. Main
  • client1.domain.com --> domain.com/client1
  • client2.domain.com --> domain.com/client2
  • ....
  1. Login
  • client1.domain.com/Login --> domain.com/client1/Login
  • client2.domain.com/Login --> domain.com/client2/Login
  • ....

My current application uses the configuration of subdomains because springboot itself identifies that it is in different contexts, but I can't understand how to do this through the path

  • not sure if you are looking for path like domain.com/client/1 which should return client 1 data. You can check this link: https://www.baeldung.com/spring-boot-start and search for following method: @GetMapping("/{id}") public Book findOne(@PathVariable Long id) { return bookRepository.findById(id) .orElseThrow(BookNotFoundException::new); } – Rakmo Aug 10 '21 at 23:46
  • @Rakmo thanks for the reply, i actually need the application to have an independent context for each tenant based on the first subdirectory of uri. for example: mydomain.com/tenant1/* mydomain.com/tenant2/* where the * would be all consumption urls – Airton Cardoso Aug 10 '21 at 23:55
  • This is not available out of the box. If your URL structure is identical for each client, you could do something clever with a custom argument resolver. – chrylis -cautiouslyoptimistic- Aug 11 '21 at 00:00
  • Any reason you can't do this in DNS records? Or use a LoadBalancer to route to the right endpoint on your server? – Yash Digant Joshi Aug 11 '21 at 00:41
  • @YashDigantJoshi Thanks for the answer. Currently the system is developed exactly as you described, but this idea of ​​modification has the principle of reducing costs – Airton Cardoso Aug 11 '21 at 00:56
  • Ah got it. Just thinking more about it. You could use a custom filter to handle the requests. Here is an example of how you can get this to work. https://stackoverflow.com/questions/27318439/how-to-convert-a-subdomain-to-a-path-with-embedded-tomcat-8-and-spring-boot – Yash Digant Joshi Aug 11 '21 at 01:01
  • @YashDigantJoshi thanks for the idea mentioned in the link, after some testing we found that we would have to make a lot of changes for the system to work and we abandoned this idea. even so thank you very much – Airton Cardoso Aug 12 '21 at 01:49

1 Answers1

0

You can always get the hostname form the request using annotation:

  @GetMapping(path = "/secret")
  @ResponseBody public String getSecret(@RequestHeader("Host") String host ) {
    var client = getClientFromHost(host);
    // ...
  }

If there is a load balancer in front of this service, you can use the X-Forwarded-From header instead of the Host header.

And then you could simply parse the host URL, to extract the subdomain which identifies your client.

Luke 10X
  • 1,071
  • 2
  • 14
  • 30