Jahia 8

Jahia login redirects to custom login page

Question

In Jahia 8, when accessing any of the global URLs like /start, /jahia/dashboard, /jahia/administration etc, it redirects to a custom login page. How can we avoid this?

Cause

This can happen if you have a custom implementation of LoginURLProvider.

Till app-shell 2.7.0, we were handling login with a hardcoded redirect to /cms/login. While this was giving an impression of expected behaviour, the hardcoded redirect was not desirable. Starting with Jahia 8.1.3.1 and app-shell 2.7.1 (only available on the Cloud), this has been updated such that ErrorServlet is responsible for redirects to proper error/login pages.
With this change, when there is a 401 and a LoginURLProvider, Jahia will always redirect to the URL returned by this provider.

Solution

To bypass this, the custom LoginURLProvider implementation needs to be adapted. Here is an example code:

package org.foo.modules.customlogin;

import org.jahia.bin.Login;
import org.jahia.params.valves.LoginUrlProvider;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@Component(service = LoginUrlProvider.class)
public class CustomLoginUrlProvider implements LoginUrlProvider {

    Logger logger = LoggerFactory.getLogger(CustomLoginUrlProvider.class);
    List<String> pathsToJahiaDefaultLogin = Arrays.asList("/start","/jahia/dashboard","/jahia/administration");

    @Override
    public String getLoginUrl(HttpServletRequest request) {
        String loginURL = Login.getServletPath();
        //RequestURI should be /error as the call is done by the ErrorServlet to get original request use attribute: javax.servlet.error.request_uri
        Object originalRequestedPath = request.getAttribute("javax.servlet.error.request_uri");
        // If Jahia detected a site mapped in the requested URL there will be a siteKey attribute
        Object siteKey = request.getAttribute("siteKey");
        if (request.getRequestURI().endsWith("/error") && (originalRequestedPath != null && pathsToJahiaDefaultLogin.contains(originalRequestedPath))) {
            logger.info("Calling CustomLoginUrlProvider we found a pathsToJahiaDefaultLogin login: {}, {}, {}", loginURL, originalRequestedPath, siteKey);
            return loginURL;
        } else {
            logger.info("Calling CustomLoginUrlProvider not global login: {}, {}, {}", loginURL, originalRequestedPath, siteKey);
            //your custom code here
        }
    }

    @Override
    public boolean hasCustomLoginUrl() {
        return true;
    }
}