Top Description Fields Constructors Methods
org.h2.security.auth

public Class DefaultAuthenticator

extends Object
implements Authenticator
Class Inheritance
All Implemented Interfaces
org.h2.security.auth.Authenticator
Imports
java.io.IOException, java.net.URL, java.util.ArrayList, .Collection, .HashMap, .HashSet, .List, .Map, .Set, javax.xml.parsers.ParserConfigurationException, org.h2.api.CredentialsValidator, .UserToRolesMapper, org.h2.engine.Database, .Right, .Role, .SessionLocal, .SysProperties, .User, .UserBuilder, org.h2.message.Trace, org.h2.security.auth.impl.AssignRealmNameRole, .JaasCredentialsValidator, org.h2.util.StringUtils, org.xml.sax.SAXException

Default authenticator implementation.

When client connectionInfo contains property AUTHREALM={realName} credentials (typically user id and password) are validated by org.h2.api.CredentialsValidator configured for that realm.

When client connectionInfo doesn't contains AUTHREALM property credentials are validated internally on the database

Rights assignment can be managed through org.h2.api.UserToRolesMapper

Default 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

Modifier and TypeField and Description
private boolean
private boolean
public static final String
private boolean
private static DefaultAuthenticator
private boolean
private Map<String, CredentialsValidator>
private boolean
private List<UserToRolesMapper>

Constructor Summary

AccessConstructor and Description
public
DefaultAuthenticator()

Create the Authenticator with default configurations

public
DefaultAuthenticator(boolean
if true default initialization is skipped
skipDefaultInitialization
)

Create authenticator and optionally skip the default configuration.

Method Summary

Modifier and TypeMethod and Description
public void
addRealm(String
realm name
name
,
CredentialsValidator
credentials validator for realm
credentialsValidator
)

Add an authentication realm.

public final User
authenticate(AuthenticationInfo
authentication info.
authenticationInfo
,
Database
target database instance.
database
)

Implements org.h2.security.auth.Authenticator.authenticate.

Perform user authentication.

private void
public void
configureFromUrl(URL
URL of configuration file
configUrl
)

Configure the authenticator from a configuration file

private void
protected static final DefaultAuthenticator
public List<UserToRolesMapper>

Returns:

current UserToRoleMappers active
getUserToRolesMappers
()

UserToRoleMappers assign roles to authenticated users

public void
init(Database
where authenticator is initialized
database
)

Implements org.h2.security.auth.Authenticator.init.

Initializes the authenticator.

public boolean

Returns:

true if creation external user is allowed, otherwise returns false
isAllowUserRegistration
()

If set create external users in the database if not present.

public boolean

Returns:

true if not found roles will be created, false roles are silently skipped.
isCreateMissingRoles
()

When set create roles not found in the database.

public boolean

Returns:

true if user will be persisted, otherwise returns false
isPersistUsers
()

If set save users externals defined during the authentication.

public void
setAllowUserRegistration(boolean
true if creation external user is allowed, otherwise returns false
allowUserRegistration
)

If set totrue creates external users in the database if not present.

public void
setCreateMissingRoles(boolean
when is true not found roles are created, when is false roles are silently skipped.
createMissingRoles
)

Sets the flag that define behavior in case external roles not found in the database.

public void
setPersistUsers(boolean
true if user will be persisted, otherwise false.
persistUsers
)

If set to true saves users externals defined during the authentication.

public void
private boolean
updateRoles(AuthenticationInfo authenticationInfo, User user, Database database)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowUserRegistrationback to summary
private boolean allowUserRegistration
createMissingRolesback to summary
private boolean createMissingRoles
DEFAULT_REALMNAMEback to summary
public static final String DEFAULT_REALMNAME
initializedback to summary
private boolean initialized
instanceback to summary
private static DefaultAuthenticator instance
persistUsersback to summary
private boolean persistUsers
realmsback to summary
private Map<String, CredentialsValidator> realms
skipDefaultInitializationback to summary
private boolean skipDefaultInitialization
userToRolesMappersback to summary
private List<UserToRolesMapper> userToRolesMappers

Constructor Detail

DefaultAuthenticatorback to summary
public DefaultAuthenticator()

Create the Authenticator with default configurations

DefaultAuthenticatorback to summary
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:boolean

if true default initialization is skipped

Method Detail

addRealmback to summary
public void addRealm(String name, CredentialsValidator credentialsValidator)

Add an authentication realm. Realms are case insensitive

Parameters
name:String

realm name

credentialsValidator:CredentialsValidator

credentials validator for realm

authenticateback to summary
public final User authenticate(AuthenticationInfo authenticationInfo, Database database) throws AuthenticationException

Implements org.h2.security.auth.Authenticator.authenticate.

Doc from org.h2.security.auth.Authenticator.authenticate.

Perform user authentication.

Parameters
authenticationInfo:AuthenticationInfo

authentication info.

database:Database

target database instance.

Returns:User

valid database user or null if user doesn't exists in the database

Annotations
@Override
Exceptions
AuthenticationException:
on failure
configureFromback to summary
private void configureFrom(H2AuthConfig config) throws AuthenticationException
configureFromUrlback to summary
public void configureFromUrl(URL configUrl) throws AuthenticationException, SAXException, IOException, ParserConfigurationException

Configure the authenticator from a configuration file

Parameters
configUrl:URL

URL of configuration file

Exceptions
AuthenticationException:
on failure
SAXException:
on failure
IOException:
on failure
ParserConfigurationException:
on failure
defaultConfigurationback to summary
private void defaultConfiguration()
getInstanceback to summary
protected static final DefaultAuthenticator getInstance()
getUserToRolesMappersback to summary
public List<UserToRolesMapper> getUserToRolesMappers()

UserToRoleMappers assign roles to authenticated users

Returns:List<UserToRolesMapper>

current UserToRoleMappers active

initback to summary
public void init(Database database) throws AuthConfigException

Implements org.h2.security.auth.Authenticator.init.

Initializes the authenticator. this method is skipped if skipDefaultInitialization is set Order of initialization is

  1. Check h2.authConfigFile system property.
  2. Use the default configuration hard coded
Parameters
database:Database

where authenticator is initialized

Annotations
@Override
isAllowUserRegistrationback to summary
public boolean isAllowUserRegistration()

If set create external users in the database if not present.

Returns:boolean

true if creation external user is allowed, otherwise returns false

isCreateMissingRolesback to summary
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:boolean

true if not found roles will be created, false roles are silently skipped.

isPersistUsersback to summary
public boolean isPersistUsers()

If set save users externals defined during the authentication.

Returns:boolean

true if user will be persisted, otherwise returns false

setAllowUserRegistrationback to summary
public void setAllowUserRegistration(boolean allowUserRegistration)

If set totrue creates external users in the database if not present.

Parameters
allowUserRegistration:boolean

true if creation external user is allowed, otherwise returns false

setCreateMissingRolesback to summary
public void setCreateMissingRoles(boolean createMissingRoles)

Sets the flag that define behavior in case external roles not found in the database.

Parameters
createMissingRoles:boolean

when is true not found roles are created, when is false roles are silently skipped.

setPersistUsersback to summary
public void setPersistUsers(boolean persistUsers)

If set to true saves users externals defined during the authentication.

Parameters
persistUsers:boolean

true if user will be persisted, otherwise false.

setUserToRolesMappersback to summary
public void setUserToRolesMappers(UserToRolesMapper... userToRolesMappers)
updateRolesback to summary
private boolean updateRoles(AuthenticationInfo authenticationInfo, User user, Database database) throws AuthenticationException