From 2aa6cdf6c6c89684a04f2f357dabad65c2fb9627 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Mon, 3 Oct 2016 19:36:13 +0200 Subject: [PATCH] Adding flag for authenticator to avoid autowiring exceptions Adds a isValid() method to the authenticator to avoid exceptions during initialization. --- .../authentication/AbstractAuthenticator.java | 8 +++++++- .../redback/authentication/Authenticator.java | 2 ++ .../DefaultAuthenticationManager.java | 2 +- .../authentication/ldap/LdapBindAuthenticator.java | 5 +++++ .../ConfigurableLdapConnectionFactory.java | 13 ++++++++++++- .../ldap/connection/LdapConnectionFactory.java | 2 ++ 6 files changed, 29 insertions(+), 3 deletions(-) diff --git a/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/AbstractAuthenticator.java b/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/AbstractAuthenticator.java index 1c80bc3c..80d1918f 100644 --- a/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/AbstractAuthenticator.java +++ b/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/AbstractAuthenticator.java @@ -24,9 +24,15 @@ package org.apache.archiva.redback.authentication; public abstract class AbstractAuthenticator implements Authenticator { + protected boolean valid = false; + public void initialize() throws AuthenticationException { - // no op + valid = true; + } + + public boolean isValid() { + return valid; } } diff --git a/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/Authenticator.java b/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/Authenticator.java index 6efe11ca..ce997573 100644 --- a/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/Authenticator.java +++ b/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/Authenticator.java @@ -38,4 +38,6 @@ public interface Authenticator throws AccountLockedException, AuthenticationException, MustChangePasswordException; void initialize() throws AuthenticationException; + + boolean isValid(); } diff --git a/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/DefaultAuthenticationManager.java b/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/DefaultAuthenticationManager.java index d5342bff..bda32762 100644 --- a/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/DefaultAuthenticationManager.java +++ b/redback-authentication/redback-authentication-api/src/main/java/org/apache/archiva/redback/authentication/DefaultAuthenticationManager.java @@ -92,7 +92,7 @@ public class DefaultAuthenticationManager List authnResultErrors = new ArrayList(); for ( Authenticator authenticator : authenticators ) { - if ( authenticator.supportsDataSource( source ) ) + if ( authenticator.isValid() && authenticator.supportsDataSource( source ) ) { AuthenticationResult authResult = authenticator.authenticate( source ); List authenticationFailureCauses = diff --git a/redback-authentication/redback-authentication-providers/redback-authentication-ldap/src/main/java/org/apache/archiva/redback/authentication/ldap/LdapBindAuthenticator.java b/redback-authentication/redback-authentication-providers/redback-authentication-ldap/src/main/java/org/apache/archiva/redback/authentication/ldap/LdapBindAuthenticator.java index 648b2f9b..fedae347 100644 --- a/redback-authentication/redback-authentication-providers/redback-authentication-ldap/src/main/java/org/apache/archiva/redback/authentication/ldap/LdapBindAuthenticator.java +++ b/redback-authentication/redback-authentication-providers/redback-authentication-ldap/src/main/java/org/apache/archiva/redback/authentication/ldap/LdapBindAuthenticator.java @@ -208,4 +208,9 @@ public class LdapBindAuthenticator log.warn( "skip exception closing naming search result {}", e.getMessage() ); } } + + @Override + public boolean isValid() { + return connectionFactory.isValid(); + } } diff --git a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/ConfigurableLdapConnectionFactory.java b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/ConfigurableLdapConnectionFactory.java index 4aab487d..2340cc5e 100644 --- a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/ConfigurableLdapConnectionFactory.java +++ b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/ConfigurableLdapConnectionFactory.java @@ -21,6 +21,8 @@ package org.apache.archiva.redback.common.ldap.connection; import org.apache.archiva.redback.configuration.UserConfiguration; import org.apache.archiva.redback.configuration.UserConfigurationKeys; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; @@ -41,6 +43,8 @@ public class ConfigurableLdapConnectionFactory implements LdapConnectionFactory { + private final Logger log = LoggerFactory.getLogger(ConfigurableLdapConnectionFactory.class); + private String hostname; private int port; @@ -61,6 +65,8 @@ public class ConfigurableLdapConnectionFactory private LdapConnectionConfiguration ldapConnectionConfiguration; + private boolean valid = false; + @Inject @Named(value = "userConfiguration#default") @@ -90,10 +96,11 @@ public class ConfigurableLdapConnectionFactory ldapConnectionConfiguration.setAuthenticationMethod( userConf.getString( UserConfigurationKeys.LDAP_AUTHENTICATION_METHOD, authenticationMethod ) ); ldapConnectionConfiguration.setExtraProperties( extraProperties ); + valid = true; } catch ( InvalidNameException e ) { - throw new RuntimeException( "Error while initializing connection factory.", e ); + log.error("Error during initialization of LdapConnectionFactory "+e.getMessage(),e); } } @@ -266,4 +273,8 @@ public class ConfigurableLdapConnectionFactory { this.userConf = userConf; } + + public boolean isValid() { + return valid; + } } diff --git a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnectionFactory.java b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnectionFactory.java index d81b3e23..4d6ec030 100644 --- a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnectionFactory.java +++ b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnectionFactory.java @@ -51,4 +51,6 @@ public interface LdapConnectionFactory void initialize(); + public boolean isValid(); + }