Package org.h2.security.auth
Class DefaultAuthenticator
- java.lang.Object
-
- org.h2.security.auth.DefaultAuthenticator
-
- All Implemented Interfaces:
Authenticator
public class DefaultAuthenticator extends java.lang.Object implements Authenticator
Default authenticator implementation.When client connectionInfo contains property AUTHREALM={realName} credentials (typically user id and password) are validated by
CredentialsValidatorconfigured for that realm.When client connectionInfo doesn't contains AUTHREALM property credentials are validated internally on the database
Rights assignment can be managed through
UserToRolesMapperDefault configuration has a realm H2 that validate credentials through JAAS api (appName=h2). To customize configuration set h2.authConfigFile system property to refer a valid h2auth.xml config file
-
-
Field Summary
Fields Modifier and Type Field Description private booleanallowUserRegistrationprivate booleancreateMissingRolesstatic java.lang.StringDEFAULT_REALMNAMEprivate booleaninitializedprivate static DefaultAuthenticatorinstanceprivate booleanpersistUsersprivate java.util.Map<java.lang.String,CredentialsValidator>realmsprivate booleanskipDefaultInitializationprivate java.util.List<UserToRolesMapper>userToRolesMappers
-
Constructor Summary
Constructors Constructor Description DefaultAuthenticator()Create the Authenticator with default configurationsDefaultAuthenticator(boolean skipDefaultInitialization)Create authenticator and optionally skip the default configuration.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddRealm(java.lang.String name, CredentialsValidator credentialsValidator)Add an authentication realm.Userauthenticate(AuthenticationInfo authenticationInfo, Database database)Perform user authentication.private voidconfigureFrom(H2AuthConfig config)voidconfigureFromUrl(java.net.URL configUrl)Configure the authenticator from a configuration fileprivate voiddefaultConfiguration()protected static DefaultAuthenticatorgetInstance()java.util.List<UserToRolesMapper>getUserToRolesMappers()UserToRoleMappers assign roles to authenticated usersvoidinit(Database database)Initializes the authenticator.booleanisAllowUserRegistration()If set create external users in the database if not present.booleanisCreateMissingRoles()When set create roles not found in the database.booleanisPersistUsers()If set save users externals defined during the authentication.voidsetAllowUserRegistration(boolean allowUserRegistration)If set totruecreates external users in the database if not present.voidsetCreateMissingRoles(boolean createMissingRoles)Sets the flag that define behavior in case external roles not found in the database.voidsetPersistUsers(boolean persistUsers)If set totruesaves users externals defined during the authentication.voidsetUserToRolesMappers(UserToRolesMapper... userToRolesMappers)private booleanupdateRoles(AuthenticationInfo authenticationInfo, User user, Database database)
-
-
-
Field Detail
-
DEFAULT_REALMNAME
public static final java.lang.String DEFAULT_REALMNAME
- See Also:
- Constant Field Values
-
realms
private java.util.Map<java.lang.String,CredentialsValidator> realms
-
userToRolesMappers
private java.util.List<UserToRolesMapper> userToRolesMappers
-
allowUserRegistration
private boolean allowUserRegistration
-
persistUsers
private boolean persistUsers
-
createMissingRoles
private boolean createMissingRoles
-
skipDefaultInitialization
private boolean skipDefaultInitialization
-
initialized
private boolean initialized
-
instance
private static DefaultAuthenticator instance
-
-
Constructor Detail
-
DefaultAuthenticator
public DefaultAuthenticator()
Create the Authenticator with default configurations
-
DefaultAuthenticator
public DefaultAuthenticator(boolean skipDefaultInitialization)
Create authenticator and optionally skip the default configuration. This option is useful when the authenticator is configured at code level- Parameters:
skipDefaultInitialization- if true default initialization is skipped
-
-
Method Detail
-
getInstance
protected static final DefaultAuthenticator getInstance()
-
isPersistUsers
public boolean isPersistUsers()
If set save users externals defined during the authentication.- Returns:
trueif user will be persisted, otherwise returnsfalse
-
setPersistUsers
public void setPersistUsers(boolean persistUsers)
If set totruesaves users externals defined during the authentication.- Parameters:
persistUsers-trueif user will be persisted, otherwisefalse.
-
isAllowUserRegistration
public boolean isAllowUserRegistration()
If set create external users in the database if not present.- Returns:
trueif creation external user is allowed, otherwise returnsfalse
-
setAllowUserRegistration
public void setAllowUserRegistration(boolean allowUserRegistration)
If set totruecreates external users in the database if not present.- Parameters:
allowUserRegistration-trueif creation external user is allowed, otherwise returnsfalse
-
isCreateMissingRoles
public boolean isCreateMissingRoles()
When set create roles not found in the database. If not set roles not found in the database are silently skipped.- Returns:
trueif not found roles will be created,falseroles are silently skipped.
-
setCreateMissingRoles
public void setCreateMissingRoles(boolean createMissingRoles)
Sets the flag that define behavior in case external roles not found in the database.- Parameters:
createMissingRoles- when istruenot found roles are created, when isfalseroles are silently skipped.
-
addRealm
public void addRealm(java.lang.String name, CredentialsValidator credentialsValidator)Add an authentication realm. Realms are case insensitive- Parameters:
name- realm namecredentialsValidator- credentials validator for realm
-
getUserToRolesMappers
public java.util.List<UserToRolesMapper> getUserToRolesMappers()
UserToRoleMappers assign roles to authenticated users- Returns:
- current UserToRoleMappers active
-
setUserToRolesMappers
public void setUserToRolesMappers(UserToRolesMapper... userToRolesMappers)
-
init
public void init(Database database) throws AuthConfigException
Initializes the authenticator. this method is skipped if skipDefaultInitialization is set Order of initialization is- Check h2.authConfigFile system property.
- Use the default configuration hard coded
- Specified by:
initin interfaceAuthenticator- Parameters:
database- where authenticator is initialized- Throws:
AuthConfigException
-
defaultConfiguration
private void defaultConfiguration()
-
configureFromUrl
public void configureFromUrl(java.net.URL configUrl) throws AuthenticationException, org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationExceptionConfigure the authenticator from a configuration file- Parameters:
configUrl- URL of configuration file- Throws:
AuthenticationException- on failureorg.xml.sax.SAXException- on failurejava.io.IOException- on failurejavax.xml.parsers.ParserConfigurationException- on failure
-
configureFrom
private void configureFrom(H2AuthConfig config) throws AuthenticationException
- Throws:
AuthenticationException
-
updateRoles
private boolean updateRoles(AuthenticationInfo authenticationInfo, User user, Database database) throws AuthenticationException
- Throws:
AuthenticationException
-
authenticate
public final User authenticate(AuthenticationInfo authenticationInfo, Database database) throws AuthenticationException
Description copied from interface:AuthenticatorPerform user authentication.- Specified by:
authenticatein interfaceAuthenticator- Parameters:
authenticationInfo- authentication info.database- target database instance.- Returns:
- valid database user or null if user doesn't exists in the database
- Throws:
AuthenticationException- on failure
-
-