How to route Tenant application with sub domain name

Hello all,
This may not be Jmix specific but it also has to do with it in a way.
I am using Jmix multitenancy addon for my application.
I want a situation whereby I can map sub-domains to different tenants.

For example, a tenant with url: www.domain_name.com/#login?tenantId=mod. I don’t want such a tenant to even know about the urlrouting tenantId=mod. I want to give each tenant a specific subdomain that will route to the appropriate tenant.

For example:
www.domain_name.com/#login?tenantId=mod should map to mod.domain_name.com
www.domain_name.com/#login?tenantId=sod should map to sod.domain_name.com

All that the tenant needs to do is to input mod.domain_name.com on the browser and it will be appropriately directed.

Please what is the best way to do this? I am currently using Tomcat Apache as the web server but I can’t find how to do this mapping. I have tried the DNS and Windows host file also.

Anyone who has done multitenancy application should please advise me here.

Thanks

I have done this in my day job, but it is not Jmix. For development environment I edited my C:\Windows\System32\drivers\etc\hosts file and included a line like this:

127.0.0.1 localhost t1.localhost t2.localhost

This allows me to test with URL like https://t1.localhost and then a OncePerRequestFilter intercepts the request, extracts the subdomain, and assigns it to a ThreadLocal TenantContext so it will be available to all processes handling that request.

For environments outside development, I have CNAME records in the DNS config that route each subdomain as needed. If there are a large number of tenants and they all route to the same destination, I would look to see if there is a wildcard option.

Hi Jeff,
Thanks for your input. Jmix multtenancy addon already handles the intercept process.

What I need is how to tell the application to route to the intended url setup on the DNS config.

In multitenancy addon, a user is identified by the tenandId={tenantName}

www.domain_name.com/#login?tenantId=mod should automatically map to mod.domain_name.com

I don’t know if DNS config is capable of doing this.

I see. It looks like the code responsible for extracting the tenant id from the query parameter is MultitenancyUiSupportImpl#getUsernameByLocation(String username, Location location). The source code for latest Jmix of that class is here

I would suggest looking for a way to override that. I think there is a way you can extend that class, then inject your own using the same component name.

Thanks Jeff

Hi, did you managed to make this work?

I followed Jeff’s solution above and it worked for me

1 Like