Search code examples
eclipse-scout

SAAS application, subdomain for each company


I have application made in Eclipse Scout. I separate tenants using company_id id my tables in database. That is working ok.

My question is, how to generate subdomain for each company (tenant).

Example: Main domain is: myapplication.com Companies (tenants): test1 test2

I would like to have URL's test1.myapplication.com, test2.myapplication.com

When I visit test1.myapplication.com I need to know that everything is going to load for test1 tenant.

On login page, i would query: SELECT username, password FROM users WHERE username = 'test' AND password = 'test' AND company_id = "Get company ID or name from URL, subdomain". Becouse now is the problem. If I have user in database with username 'test' on test1 tenant, I cannot have user with username 'test' on tenant2 becouse dont know which user/tenant to load on login. With subdomain it would be possible with another AND condition AND company_id = 1

Is this possible?

Thanks


Solution

  • I think your question is not specific to Scout applications and more related to general Java Authentication and Authorization (aka JAAS). You can use all the underlying features of JAAS in a Scout application.

    The login feature in Scout uses standard Servlet filters which you configure in your web.xml. Since the servlet filter is called with the HTTP request, you could extract the sub-domain in your own servlet filter implementation and use that information to build an SQL query with the SQL constraint you need.

    When the login is valid, you could use sub-domain and user-name as Subject, like “test1/username”. In Scout the Subject is available in each request or service-call to the backend server. There you could extract the sub-domain from the Subject and use that information to execute your SQL queries.

    The Scout widgets application contains an example on how to use servlet filters. See class WidgetsUiServletFilter.