Web app with multiple domains pointing to subfolde

2019-08-19 05:25发布

问题:

I’m developing an application with Spring Boot, which runs under a domain, let’s say;

parentdomain.com

From one side the users enter their content, after logging in, at parentdomain.com/admin. This content is processed and made publicly accessible at a subfolder, let’s say;

parentdomain.com/user-one-content
parentdomain.com/user-two-content and so on.

I need to point a domain for each user to his folder, for example:

userone.com should be equivalent to parentdomain.com/user-one-content

The navigation should remain in the userone.com website.

userone.com/first-page should open parentdomain.com/user-one-content/first-page.

Right now the app is installed at PWS (Pivotal Web Services), but their routing system, doesn’t provide a solution to this approach, with multiple domains pointing to subfolders of a parent one.

How could I archive this functionality?

回答1:

I would leave Spring Boot alone and put in a web server in front of it. Can you install Apache or Nginx? Domain/sub-domain rerouting is out of the box in both of them.



回答2:

At the end I could archive it, thanks to the advice from gspatel

With the rooting system of PWS, or other IaaS cloud provider I can root each domain to the whole application.

Implementing the preHandle method of a HandleInterceptor, I check whether the request is coming from my domain (parentdomain.com in the example). Or if it is coming from a domain of the sites, that the app is serving (userone.com in the example). In that case, I control that doesn't access the parentdomain.com, or the content of other users, forwarding the request to the user home page (user-one-content in the example).

That's an extract, of my preHadle implementation:

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

      ...

      String URI = request.getRequestURI();
      String rootURI = "/" + idweb + "/";

      if(URI.equals("/") || !URI.startsWith(rootURI)){ 
          URI = rootURI;

          RequestDispatcher requestDispatcher = request.getRequestDispatcher(request.getContextPath() + URI);
          requestDispatcher.forward(request, response);

        }

    return true;
}