Top Description Fields Constructors Methods
org.mortbay.jetty.security

public Class JDBCUserRealm

extends HashUserRealm
implements UserRealm
Class Inheritance
All Implemented Interfaces
org.mortbay.jetty.UserRealm
Imports
java.io.IOException, java.security.Principal, java.sql.Connection, .DriverManager, .PreparedStatement, .ResultSet, .SQLException, java.util.Properties, org.mortbay.jetty.Request, .UserRealm, org.mortbay.log.Log, org.mortbay.resource.Resource, org.mortbay.util.Loader

HashMapped User Realm with JDBC as data source. JDBCUserRealm extends HashUserRealm and adds a method to fetch user information from database. The authenticate() method checks the inherited HashMap for the user. If the user is not found, it will fetch details from the database and populate the inherited HashMap. It then calls the HashUserRealm authenticate() method to perform the actual authentication. Periodically (controlled by configuration parameter), internal hashes are cleared. Caching can be disabled by setting cache refresh interval to zero. Uses one database connection that is initialized at startup. Reconnect on failures. authenticate() is 'synchronized'. An example properties file for configuration is in $JETTY_HOME/etc/jdbcRealm.properties
Authors
Arkadi Shishlov (arkadi), Fredrik Borgh, Greg Wilkins (gregw), Ben Alex
Version
$Id: JDBCUserRealm.java 3736 2008-10-04 22:19:26Z gregw $

Field Summary

Modifier and TypeField and Description
private int
private Connection
private String
private long
private String
private String
private String
private String
private String
private String
private String
private String
private String
private String
private String
private String
private String
private String
private String
Inherited from org.mortbay.jetty.security.HashUserRealm:
__SSO_roles_users

Constructor Summary

AccessConstructor and Description
public
JDBCUserRealm()

Constructor.

public
JDBCUserRealm(String name)

Constructor.

public
JDBCUserRealm(String
Realm name
name
,
String
Filename or url of JDBC connection properties file.
config
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public Principal
authenticate(String
The username.
username
,
Object
The user credentials, normally a String password.
credentials
,
Request
The request to be authenticated. Additional parameters may be extracted or set on this request as needed for the authentication mechanism (none required for BASIC and FORM authentication).
request
)

Overrides org.mortbay.jetty.security.HashUserRealm.authenticate.

Implements org.mortbay.jetty.UserRealm.authenticate.

Authenticate a users credentials.
public void
connectDatabase()

(re)Connect to database with parameters setup by loadConfig()

public synchronized boolean

Returns:

True if the user can act in the role.
isUserInRole
(Principal
The user, which must be from this realm
user
,
String roleName)

Overrides org.mortbay.jetty.security.HashUserRealm.isUserInRole.

Implements org.mortbay.jetty.UserRealm.isUserInRole.

Check if a user is in a role.
protected void
loadConfig()

Overrides org.mortbay.jetty.security.HashUserRealm.loadConfig.

Load JDBC connection configuration from properties file.
private void
loadUser(String username)

public void
logout(Principal
A Principal previously returned from this realm
user
)

Overrides org.mortbay.jetty.security.HashUserRealm.logout.

Implements org.mortbay.jetty.UserRealm.logout.

logout a user Principal.
Inherited from org.mortbay.jetty.security.HashUserRealm:
addUserToRoleclearSingleSignOndisassociatedoStartdoStopdumpgetConfiggetConfigResourcegetNamegetPrincipalgetRefreshIntervalgetSingleSignOngetSSORealmpopRolepushRoleputreauthenticatesetConfigsetNamesetRefreshIntervalsetSingleSignOnsetSSORealmtoString

Field Detail

_cacheTimeback to summary
private int _cacheTime
_conback to summary
private Connection _con
_jdbcDriverback to summary
private String _jdbcDriver
_lastHashPurgeback to summary
private long _lastHashPurge
_passwordback to summary
private String _password
_roleSqlback to summary
private String _roleSql
_roleTableback to summary
private String _roleTable
_roleTableKeyback to summary
private String _roleTableKey
_roleTableRoleFieldback to summary
private String _roleTableRoleField
_urlback to summary
private String _url
_userNameback to summary
private String _userName
_userRoleTableback to summary
private String _userRoleTable
_userRoleTableRoleKeyback to summary
private String _userRoleTableRoleKey
_userRoleTableUserKeyback to summary
private String _userRoleTableUserKey
_userSqlback to summary
private String _userSql
_userTableback to summary
private String _userTable
_userTableKeyback to summary
private String _userTableKey
_userTablePasswordFieldback to summary
private String _userTablePasswordField
_userTableUserFieldback to summary
private String _userTableUserField

Constructor Detail

JDBCUserRealmback to summary
public JDBCUserRealm()

Constructor.

JDBCUserRealmback to summary
public JDBCUserRealm(String name)

Constructor.

JDBCUserRealmback to summary
public JDBCUserRealm(String name, String config) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException

Constructor.

Parameters
name:String

Realm name

config:String

Filename or url of JDBC connection properties file.

Method Detail

authenticateback to summary
public Principal authenticate(String username, Object credentials, Request request)

Overrides org.mortbay.jetty.security.HashUserRealm.authenticate.

Implements org.mortbay.jetty.UserRealm.authenticate.

Doc from org.mortbay.jetty.UserRealm.authenticate.

Authenticate a users credentials. Implementations of this method may adorn the calling context to assoicate it with the authenticated principal (eg ThreadLocals). If such context associations are made, they should be considered valid until a UserRealm.deAuthenticate(UserPrincipal) call is made for this UserPrincipal.

Parameters
username:String

The username.

credentials:Object

The user credentials, normally a String password.

request:Request

The request to be authenticated. Additional parameters may be extracted or set on this request as needed for the authentication mechanism (none required for BASIC and FORM authentication).

Returns:Principal

The authenticated UserPrincipal.

connectDatabaseback to summary
public void connectDatabase()

(re)Connect to database with parameters setup by loadConfig()

isUserInRoleback to summary
public synchronized boolean isUserInRole(Principal user, String roleName)

Overrides org.mortbay.jetty.security.HashUserRealm.isUserInRole.

Implements org.mortbay.jetty.UserRealm.isUserInRole.

Check if a user is in a role.

Parameters
user:Principal

The user, which must be from this realm

Returns:boolean

True if the user can act in the role.

loadConfigback to summary
protected void loadConfig() throws IOException

Overrides org.mortbay.jetty.security.HashUserRealm.loadConfig.

Load JDBC connection configuration from properties file.

loadUserback to summary
private void loadUser(String username)
logoutback to summary
public void logout(Principal user)

Overrides org.mortbay.jetty.security.HashUserRealm.logout.

Implements org.mortbay.jetty.UserRealm.logout.

Doc from org.mortbay.jetty.UserRealm.logout.

logout a user Principal. Called by authentication mechanisms (eg FORM) that can detect logout.

Parameters
user:Principal

A Principal previously returned from this realm