Repository: keycloak/keycloak
Issue: Any tests using PhantomJS failing in some newer linux environments

### Before reporting an issue

- [X] I have searched existing issues
- [X] I have reproduced the issue with the latest release

### Area

testsuite

### Describe the bug

After setup new laptop, Any test using PhantomJS is failing for me with the error like this:

```
09:01:26,169 INFO [org.keycloak.testsuite.adapter.servlet.DemoServletsAdapterTest] [DemoServletsAdapterTest] testWithoutKeycloakConf() STARTED
Feb 03, 2023 9:01:26 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: executable: /home/mposolda/IdeaProjects/keycloak/testsuite/integration-arquillian/tests/base/target/drone/1c947d57fce2f21ce0b43fe2ed7cd361/phantomjs-2.1.1-linux-x86_64/bin/phantomjs
Feb 03, 2023 9:01:26 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: port: 1443
Feb 03, 2023 9:01:26 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--ignore-ssl-errors=true, --web-security=false, --webdriver=1443, --webdriver-logfile=/home/mposolda/IdeaProjects/keycloak/testsuite/integration-arquillian/tests/base/phantomjsdriver.log]
Feb 03, 2023 9:01:26 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: environment: {}
Auto configuration failed
139991832651712:error:25066067:DSO support routines:DLFCN_LOAD:could not load the shared library:dso_dlfcn.c:185:filename(libproviders.so): libproviders.so: cannot open shared object file: No such file or directory
139991832651712:error:25070067:DSO support routines:DSO_load:could not load the shared library:dso_lib.c:244:
139991832651712:error:0E07506E:configuration file routines:MODULE_LOAD_DSO:error loading dso:conf_mod.c:285:module=providers, path=providers
139991832651712:error:0E076071:configuration file routines:MODULE_RUN:unknown module name:conf_mod.c:222:module=providers
Feb 03, 2023 9:01:46 AM org.openqa.selenium.os.OsProcess checkForError
SEVERE: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
09:01:46,486 ERROR [org.keycloak.testsuite.adapter.servlet.DemoServletsAdapterTest] [DemoServletsAdapterTest] testWithoutKeycloakConf() FAILED 
```

I am seeing this on Ubuntu 22.04 as well as on RHEL 9.1 virtual machine. Java version is OpenJDK11 or OpenJDK17. 

### Solution

It helps to set this environment variable:
```
export OPENSSL_CONF=/dev/null
```
See for example here for more details https://github.com/nodejs/node/issues/43132

Can we eventually set this variable by default to avoid such issues?

### Version

20.0.3

### How to Reproduce?

For example run this test on Ubuntu 22.04 or on RHEL 9.1 (reproduced with RHEL 9.1 virtual machine). Java version is OpenJDK11 or OpenJDK17. 
```
mvn clean install -Dtest=DemoServletsAdapterTest
```

Changed files:
integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/KeycloakAuthenticatorValve.java
integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/AuthenticatedActionsValve.java

diff --git a/integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/AuthenticatedActionsValve.java b/integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/AuthenticatedActionsValve.java
index 9088533faa..541fc90ceb 100755
--- a/integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/AuthenticatedActionsValve.java
+++ b/integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/AuthenticatedActionsValve.java
@@ -11,6 +11,7 @@ import org.apache.catalina.Valve;
 import org.apache.catalina.connector.Request;
 import org.apache.catalina.connector.Response;
 import org.apache.catalina.valves.ValveBase;
+import org.keycloak.adapters.AdapterDeploymentContext;
 import org.keycloak.adapters.AuthenticatedActionsHandler;
 import org.keycloak.adapters.KeycloakDeployment;
 
@@ -27,10 +28,10 @@ import org.keycloak.adapters.KeycloakDeployment;
  */
 public class AuthenticatedActionsValve extends ValveBase {
     private static final Logger log = Logger.getLogger(""+AuthenticatedActionsValve.class);
-    protected KeycloakDeployment deployment;
+    protected AdapterDeploymentContext deploymentContext;
 
-    public AuthenticatedActionsValve(KeycloakDeployment deployment, Valve next, Container container, ObjectName objectName) {
-        this.deployment = deployment;
+    public AuthenticatedActionsValve(AdapterDeploymentContext deploymentContext, Valve next, Container container, ObjectName controller) {
+        this.deploymentContext = deploymentContext;
         if (next == null) throw new RuntimeException("WTF is next null?!");
         setNext(next);
         setContainer(container);
@@ -40,10 +41,17 @@ public class AuthenticatedActionsValve extends ValveBase {
     @Override
     public void invoke(Request request, Response response) throws IOException, ServletException {
         log.finer("AuthenticatedActionsValve.invoke" + request.getRequestURI());
-        AuthenticatedActionsHandler handler = new AuthenticatedActionsHandler(deployment, new CatalinaHttpFacade(request, response));
-        if (handler.handledRequest()) {
-            return;
+        CatalinaHttpFacade facade = new CatalinaHttpFacade(request, response);
+        KeycloakDeployment deployment = deploymentContext.resolveDeployment(facade);
+        if (deployment != null && deployment.isConfigured()) {
+            AuthenticatedActionsHandler handler = new AuthenticatedActionsHandler(deployment, new CatalinaHttpFacade(request, response));
+            if (handler.handledRequest()) {
+                return;
+            }
+
         }
         getNext().invoke(request, response);
     }
+
+
 }
diff --git a/integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/KeycloakAuthenticatorValve.java b/integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/KeycloakAuthenticatorValve.java
index 2dd4e97563..5ce3050d5c 100755
--- a/integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/KeycloakAuthenticatorValve.java
+++ b/integration/tomcat7/adapter/src/main/java/org/keycloak/adapters/tomcat7/KeycloakAuthenticatorValve.java
@@ -23,8 +23,10 @@ import org.apache.catalina.core.StandardContext;
 import org.apache.catalina.deploy.LoginConfig;
 import org.keycloak.KeycloakSecurityContext;
 import org.keycloak.adapters.AdapterConstants;
+import org.keycloak.adapters.AdapterDeploymentContext;
 import org.keycloak.adapters.AuthChallenge;
 import org.keycloak.adapters.AuthOutcome;
+import org.keycloak.adapters.HttpFacade;
 import org.keycloak.adapters.KeycloakDeployment;
 import org.keycloak.adapters.KeycloakDeploymentBuilder;
 import org.keycloak.adapters.PreAuthActionsHandler;
@@ -43,7 +45,7 @@ import org.keycloak.adapters.RefreshableKeycloakSecurityContext;
 public class KeycloakAuthenticatorValve extends FormAuthenticator implements LifecycleListener {
 	private final static Logger log = Logger.getLogger(""+KeycloakAuthenticatorValve.class);
 	protected CatalinaUserSessionManagement userSessionManagement = new CatalinaUserSessionManagement();
-    protected KeycloakDeployment deployment;
+    protected AdapterDeploymentContext deploymentContext;
 
     @Override
     public void lifecycleEvent(LifecycleEvent event) {
@@ -64,9 +66,17 @@ public class KeycloakAuthenticatorValve extends FormAuthenticator implements Lif
     }
 
     public void initInternal() {
-        this.deployment = KeycloakDeploymentBuilder.build(getConfigInputStream(context));
-        log.info("deployment realm:" + deployment.getRealm() + " resource:" + deployment.getResourceName());
-        AuthenticatedActionsValve actions = new AuthenticatedActionsValve(deployment, getNext(), getContainer(), getObjectName());
+        InputStream configInputStream = getConfigInputStream(context);
+        KeycloakDeployment kd = null;
+        if (configInputStream == null) {
+            log.warning("No adapter configuration.  Keycloak is unconfigured and will deny all requests.");
+            kd = new KeycloakDeployment();
+        } else {
+            kd = KeycloakDeploymentBuilder.build(configInputStream);
+        }
+        deploymentContext = new AdapterDeploymentContext(kd);
+        context.getServletContext().setAttribute(AdapterDeploymentContext.class.getName(), deploymentContext);
+        AuthenticatedActionsValve actions = new AuthenticatedActionsValve(deploymentContext, getNext(), getContainer(), getObjectName());
         setNext(actions);
     }
 
@@ -102,12 +112,12 @@ public class KeycloakAuthenticatorValve extends FormAuthenticator implements Lif
     @Override
     public void invoke(Request request, Response response) throws IOException, ServletException {
         try {
-            PreAuthActionsHandler handler = new PreAuthActionsHandler(userSessionManagement, deployment,
-                    new CatalinaHttpFacade(request, response));
+            CatalinaHttpFacade facade = new CatalinaHttpFacade(request, response);
+            PreAuthActionsHandler handler = new PreAuthActionsHandler(userSessionManagement, deploymentContext, facade);
             if (handler.handleRequest()) {
                 return;
             }
-            checkKeycloakSession(request);
+            checkKeycloakSession(request, facade);
             super.invoke(request, response);
         } finally {
         }
@@ -116,6 +126,11 @@ public class KeycloakAuthenticatorValve extends FormAuthenticator implements Lif
     @Override
     public boolean authenticate(Request request, HttpServletResponse response, LoginConfig config) throws IOException {
         CatalinaHttpFacade facade = new CatalinaHttpFacade(request, response);
+        KeycloakDeployment deployment = deploymentContext.resolveDeployment(facade);
+        if (deployment == null || !deployment.isConfigured()) {
+            return false;
+        }
+
         CatalinaRequestAuthenticator authenticator = new CatalinaRequestAuthenticator(deployment, this, userSessionManagement, facade, request);
         AuthOutcome outcome = authenticator.authenticate();
         if (outcome == AuthOutcome.AUTHENTICATED) {
@@ -132,29 +147,22 @@ public class KeycloakAuthenticatorValve extends FormAuthenticator implements Lif
     }
 
     /**
-     * Checks that access token is still valid. Will attempt refresh of token if
-     * it is not.
-     * 
+     * Checks that access token is still valid.  Will attempt refresh of token if it is not.
+     *
      * @param request
      */
-    protected void checkKeycloakSession(Request request) {
-        if (request.getSessionInternal(false) == null || request.getSessionInternal().getPrincipal() == null)
-            return;
-        RefreshableKeycloakSecurityContext session = (RefreshableKeycloakSecurityContext) request.getSessionInternal()
-                .getNote(KeycloakSecurityContext.class.getName());
-        if (session == null)
-            return;
+    protected void checkKeycloakSession(Request request, HttpFacade facade) {
+        if (request.getSessionInternal(false) == null || request.getSessionInternal().getPrincipal() == null) return;
+        RefreshableKeycloakSecurityContext session = (RefreshableKeycloakSecurityContext) request.getSessionInternal().getNote(KeycloakSecurityContext.class.getName());
+        if (session == null) return;
         // just in case session got serialized
-        session.setDeployment(deployment);
-        if (session.isActive())
-            return;
+        if (session.getDeployment() == null) session.setDeployment(deploymentContext.resolveDeployment(facade));
+        if (session.isActive()) return;
 
-        // FYI: A refresh requires same scope, so same roles will be set.
-        // Otherwise, refresh will fail and token will
+        // FYI: A refresh requires same scope, so same roles will be set.  Otherwise, refresh will fail and token will
         // not be updated
         session.refreshExpiredToken();
-        if (session.isActive())
-            return;
+        if (session.isActive()) return;
 
         request.getSessionInternal().removeNote(KeycloakSecurityContext.class.getName());
         request.setUserPrincipal(null);