Force context classloader when loggin in to Jackrabbit
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 18 Jun 2023 06:17:35 +0000 (08:17 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 18 Jun 2023 06:17:35 +0000 (08:17 +0200)
org.argeo.cms.jcr/src/org/argeo/security/jackrabbit/ArgeoAuthContext.java

index d679c45f96ff8fcc59c99d06e191983ed3256884..97dfdf5dcdd39a56597e4ef463f5de45f2c4b1d9 100644 (file)
@@ -11,17 +11,32 @@ import org.apache.jackrabbit.core.security.authentication.AuthContext;
 class ArgeoAuthContext implements AuthContext {
        private LoginContext lc;
 
+       private String loginContextName;
+
        public ArgeoAuthContext(String appName, Subject subject, CallbackHandler callbackHandler) {
+               this.loginContextName = appName;
+               // Context class loader for login context is set when it is created.
+               // we make sure that it uses our won class loader
+               ClassLoader currentContextCl = Thread.currentThread().getContextClassLoader();
                try {
-                       lc = new LoginContext(appName, subject, callbackHandler);
+                       Thread.currentThread().setContextClassLoader(SystemJackrabbitLoginModule.class.getClassLoader());
+                       lc = new LoginContext(loginContextName, subject, callbackHandler);
                } catch (LoginException e) {
                        throw new IllegalStateException("Cannot configure Jackrabbit login context", e);
+               } finally {
+                       Thread.currentThread().setContextClassLoader(currentContextCl);
                }
        }
 
        @Override
        public void login() throws LoginException {
-               lc.login();
+               try {
+                       lc.login();
+               } catch (LoginException e) {
+                       // we force a runtime exception since Jackrabbit swallows LoginException
+                       // and still create a session
+                       throw new IllegalStateException("Login context " + loginContextName + " failed", e);
+               }
        }
 
        @Override