Class JDBCUserDatabase
- java.lang.Object
-
- org.apache.wiki.auth.user.AbstractUserDatabase
-
- org.apache.wiki.auth.user.JDBCUserDatabase
-
- All Implemented Interfaces:
UserDatabase
public class JDBCUserDatabase extends AbstractUserDatabase
Implementation of UserDatabase that persists
DefaultUserProfileobjects to a JDBC DataSource, as might typically be provided by a web container. This implementation looks up the JDBC DataSource using JNDI. The JNDI name of the datasource, backing table and mapped columns used by this class can be overridden by adding settings injspwiki.properties.Configurable properties are these:
Property Default Definition jspwiki.userdatabase.datasourcejdbc/UserDatabaseThe JNDI name of the DataSource jspwiki.userdatabase.tableusersThe table that stores the user profiles jspwiki.userdatabase.attributesattributesThe CLOB column containing the profile's custom attributes, stored as key/value strings, each separated by newline. jspwiki.userdatabase.createdcreatedThe column containing the profile's creation timestamp jspwiki.userdatabase.emailemailThe column containing the user's e-mail address jspwiki.userdatabase.fullNamefull_nameThe column containing the user's full name jspwiki.userdatabase.loginNamelogin_nameThe column containing the user's login id jspwiki.userdatabase.passwordpasswordThe column containing the user's password jspwiki.userdatabase.modifiedmodifiedThe column containing the profile's last-modified timestamp jspwiki.userdatabase.uiduidThe column containing the profile's unique identifier, as a long integer jspwiki.userdatabase.wikiNamewiki_nameThe column containing the user's wiki name jspwiki.userdatabase.lockExpirylock_expiryThe column containing the date/time when the profile, if locked, should be unlocked. jspwiki.userdatabase.roleTablerolesThe table that stores user roles. When a new user is created, a new record is inserted containing user's initial role. The table will have an ID column whose name and values correspond to the contents of the user table's login name column. It will also contain a role column (see next row). jspwiki.userdatabase.roleroleThe column in the role table that stores user roles. When a new user is created, this column will be populated with the value Authenticated. Once created, JDBCUserDatabase does not use this column again; it is provided strictly for the convenience of container-managed authentication services.This class hashes passwords using SHA-1. All of the underying SQL commands used by this class are implemented using prepared statements, so it is immune to SQL injection attacks.
This class is typically used in conjunction with a web container's JNDI resource factory. For example, Tomcat provides a basic JNDI factory for registering DataSources. To give JSPWiki access to the JNDI resource named by
, you would declare the datasource resource similar to this:<Context ...>
...
<Resource name="jdbc/UserDatabase" auth="Container"
type="javax.sql.DataSource" username="dbusername" password="dbpassword"
driverClassName="org.hsql.jdbcDriver" url="jdbc:HypersonicSQL:database"
maxActive="8" maxIdle="4"/>
...
</Context>To configure JSPWiki to use JDBC support, first create a database with a structure similar to that provided by the HSQL and PostgreSQL scripts in src/main/config/db. If you have different table or column names you can either alias them with a database view and have JSPWiki use the views, or alter the WEB-INF/jspwiki.properties file: the jspwiki.userdatabase.* and jspwiki.groupdatabase.* properties change the names of the tables and columns that JSPWiki uses.
A JNDI datasource (named jdbc/UserDatabase by default but can be configured in the jspwiki.properties file) will need to be created in your servlet container. JDBC driver JARs should be added, e.g. in Tomcat's
libdirectory. For more Tomcat JNDI configuration examples, see http://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html. Once done, restart JSPWiki in the servlet container for it to read the new properties and switch to JDBC authentication.JDBCUserDatabase commits changes as transactions if the back-end database supports them. Changes are made immediately (during the
save(UserProfile)method).- Since:
- 2.3
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_DB_ATTRIBUTESstatic java.lang.StringDEFAULT_DB_CREATEDstatic java.lang.StringDEFAULT_DB_EMAILstatic java.lang.StringDEFAULT_DB_FULL_NAMEstatic java.lang.StringDEFAULT_DB_JNDI_NAMEstatic java.lang.StringDEFAULT_DB_LOCK_EXPIRYstatic java.lang.StringDEFAULT_DB_LOGIN_NAMEstatic java.lang.StringDEFAULT_DB_MODIFIEDstatic java.lang.StringDEFAULT_DB_PASSWORDstatic java.lang.StringDEFAULT_DB_ROLEstatic java.lang.StringDEFAULT_DB_ROLE_TABLEstatic java.lang.StringDEFAULT_DB_TABLEstatic java.lang.StringDEFAULT_DB_UIDstatic java.lang.StringDEFAULT_DB_WIKI_NAMEstatic java.lang.StringPROP_DB_ATTRIBUTESstatic java.lang.StringPROP_DB_CREATEDstatic java.lang.StringPROP_DB_DATASOURCEstatic java.lang.StringPROP_DB_EMAILstatic java.lang.StringPROP_DB_FULL_NAMEstatic java.lang.StringPROP_DB_LOCK_EXPIRYstatic java.lang.StringPROP_DB_LOGIN_NAMEstatic java.lang.StringPROP_DB_MODIFIEDstatic java.lang.StringPROP_DB_PASSWORDstatic java.lang.StringPROP_DB_ROLEstatic java.lang.StringPROP_DB_ROLE_TABLEstatic java.lang.StringPROP_DB_TABLEstatic java.lang.StringPROP_DB_UIDstatic java.lang.StringPROP_DB_WIKI_NAME-
Fields inherited from class org.apache.wiki.auth.user.AbstractUserDatabase
log, SHA_PREFIX, SHA256_PREFIX, SSHA_PREFIX
-
-
Constructor Summary
Constructors Constructor Description JDBCUserDatabase()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddeleteByLoginName(java.lang.String loginName)Looks up and deletes the firstUserProfilein the user database that matches a profile having a given login name.UserProfilefindByEmail(java.lang.String index)Looks up and returns the firstUserProfilein the user database that matches a profile having a given e-mail address.UserProfilefindByFullName(java.lang.String index)Looks up and returns the firstUserProfilein the user database that matches a profile having a given full name.UserProfilefindByLoginName(java.lang.String index)Looks up and returns the firstUserProfilein the user database that matches a profile having a given login name.UserProfilefindByUid(java.lang.String uid)Looks up and returns the firstUserProfilein the user database that matches a profile having a given unique ID (uid).UserProfilefindByWikiName(java.lang.String index)Looks up and returns the firstUserProfilein the user database that matches a profile having a given wiki name.java.security.Principal[]getWikiNames()Returns all WikiNames that are stored in the UserDatabase as an array of WikiPrincipal objects.voidinitialize(Engine engine, java.util.Properties props)Initializes the user database based on values from a Properties object.voidrename(java.lang.String loginName, java.lang.String newName)Renames aUserProfilein the user database by changing the profile's login name.voidsave(UserProfile profile)Saves aUserProfileto the user database, overwriting the existing profile if it exists.-
Methods inherited from class org.apache.wiki.auth.user.AbstractUserDatabase
find, generateUid, getHash, getPrincipals, newProfile, parseLong, validatePassword
-
-
-
-
Field Detail
-
DEFAULT_DB_ATTRIBUTES
public static final java.lang.String DEFAULT_DB_ATTRIBUTES
- See Also:
- Constant Field Values
-
DEFAULT_DB_CREATED
public static final java.lang.String DEFAULT_DB_CREATED
- See Also:
- Constant Field Values
-
DEFAULT_DB_EMAIL
public static final java.lang.String DEFAULT_DB_EMAIL
- See Also:
- Constant Field Values
-
DEFAULT_DB_FULL_NAME
public static final java.lang.String DEFAULT_DB_FULL_NAME
- See Also:
- Constant Field Values
-
DEFAULT_DB_JNDI_NAME
public static final java.lang.String DEFAULT_DB_JNDI_NAME
- See Also:
- Constant Field Values
-
DEFAULT_DB_LOCK_EXPIRY
public static final java.lang.String DEFAULT_DB_LOCK_EXPIRY
- See Also:
- Constant Field Values
-
DEFAULT_DB_MODIFIED
public static final java.lang.String DEFAULT_DB_MODIFIED
- See Also:
- Constant Field Values
-
DEFAULT_DB_ROLE
public static final java.lang.String DEFAULT_DB_ROLE
- See Also:
- Constant Field Values
-
DEFAULT_DB_ROLE_TABLE
public static final java.lang.String DEFAULT_DB_ROLE_TABLE
- See Also:
- Constant Field Values
-
DEFAULT_DB_TABLE
public static final java.lang.String DEFAULT_DB_TABLE
- See Also:
- Constant Field Values
-
DEFAULT_DB_LOGIN_NAME
public static final java.lang.String DEFAULT_DB_LOGIN_NAME
- See Also:
- Constant Field Values
-
DEFAULT_DB_PASSWORD
public static final java.lang.String DEFAULT_DB_PASSWORD
- See Also:
- Constant Field Values
-
DEFAULT_DB_UID
public static final java.lang.String DEFAULT_DB_UID
- See Also:
- Constant Field Values
-
DEFAULT_DB_WIKI_NAME
public static final java.lang.String DEFAULT_DB_WIKI_NAME
- See Also:
- Constant Field Values
-
PROP_DB_ATTRIBUTES
public static final java.lang.String PROP_DB_ATTRIBUTES
- See Also:
- Constant Field Values
-
PROP_DB_CREATED
public static final java.lang.String PROP_DB_CREATED
- See Also:
- Constant Field Values
-
PROP_DB_EMAIL
public static final java.lang.String PROP_DB_EMAIL
- See Also:
- Constant Field Values
-
PROP_DB_FULL_NAME
public static final java.lang.String PROP_DB_FULL_NAME
- See Also:
- Constant Field Values
-
PROP_DB_DATASOURCE
public static final java.lang.String PROP_DB_DATASOURCE
- See Also:
- Constant Field Values
-
PROP_DB_LOCK_EXPIRY
public static final java.lang.String PROP_DB_LOCK_EXPIRY
- See Also:
- Constant Field Values
-
PROP_DB_LOGIN_NAME
public static final java.lang.String PROP_DB_LOGIN_NAME
- See Also:
- Constant Field Values
-
PROP_DB_MODIFIED
public static final java.lang.String PROP_DB_MODIFIED
- See Also:
- Constant Field Values
-
PROP_DB_PASSWORD
public static final java.lang.String PROP_DB_PASSWORD
- See Also:
- Constant Field Values
-
PROP_DB_UID
public static final java.lang.String PROP_DB_UID
- See Also:
- Constant Field Values
-
PROP_DB_ROLE
public static final java.lang.String PROP_DB_ROLE
- See Also:
- Constant Field Values
-
PROP_DB_ROLE_TABLE
public static final java.lang.String PROP_DB_ROLE_TABLE
- See Also:
- Constant Field Values
-
PROP_DB_TABLE
public static final java.lang.String PROP_DB_TABLE
- See Also:
- Constant Field Values
-
PROP_DB_WIKI_NAME
public static final java.lang.String PROP_DB_WIKI_NAME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JDBCUserDatabase
public JDBCUserDatabase()
-
-
Method Detail
-
deleteByLoginName
public void deleteByLoginName(java.lang.String loginName) throws NoSuchPrincipalException, WikiSecurityException
Looks up and deletes the firstUserProfilein the user database that matches a profile having a given login name. If the user database does not contain a user with a matching attribute, throws aNoSuchPrincipalException. This method is intended to be atomic; results cannot be partially committed. If the commit fails, it should roll back its state appropriately. Implementing classes that persist to the file system may wish to make this methodsynchronized.- Parameters:
loginName- the login name of the user profile that shall be deleted- Throws:
NoSuchPrincipalExceptionWikiSecurityException
-
findByEmail
public UserProfile findByEmail(java.lang.String index) throws NoSuchPrincipalException
Description copied from class:AbstractUserDatabaseLooks up and returns the firstUserProfilein the user database that matches a profile having a given e-mail address. If the user database does not contain a user with a matching attribute, throws aNoSuchPrincipalException.- Specified by:
findByEmailin interfaceUserDatabase- Specified by:
findByEmailin classAbstractUserDatabase- Parameters:
index- the e-mail address of the desired user profile- Returns:
- the user profile
- Throws:
NoSuchPrincipalException- See Also:
UserDatabase.findByEmail(java.lang.String)
-
findByFullName
public UserProfile findByFullName(java.lang.String index) throws NoSuchPrincipalException
Description copied from class:AbstractUserDatabaseLooks up and returns the firstUserProfilein the user database that matches a profile having a given full name. If the user database does not contain a user with a matching attribute, throws aNoSuchPrincipalException.- Specified by:
findByFullNamein interfaceUserDatabase- Specified by:
findByFullNamein classAbstractUserDatabase- Parameters:
index- the fill name of the desired user profile- Returns:
- the user profile
- Throws:
NoSuchPrincipalException- See Also:
UserDatabase.findByFullName(java.lang.String)
-
findByLoginName
public UserProfile findByLoginName(java.lang.String index) throws NoSuchPrincipalException
Description copied from class:AbstractUserDatabaseLooks up and returns the firstUserProfilein the user database that matches a profile having a given login name. If the user database does not contain a user with a matching attribute, throws aNoSuchPrincipalException.- Specified by:
findByLoginNamein interfaceUserDatabase- Specified by:
findByLoginNamein classAbstractUserDatabase- Parameters:
index- the login name of the desired user profile- Returns:
- the user profile
- Throws:
NoSuchPrincipalException- See Also:
UserDatabase.findByLoginName(java.lang.String)
-
findByUid
public UserProfile findByUid(java.lang.String uid) throws NoSuchPrincipalException
Description copied from interface:UserDatabaseLooks up and returns the firstUserProfilein the user database that matches a profile having a given unique ID (uid). If the user database does not contain a user with a unique ID, it throws aNoSuchPrincipalException.- Parameters:
uid- the unique identifier of the desired user profile- Returns:
- the user profile
- Throws:
NoSuchPrincipalException- See Also:
UserDatabase.findByWikiName(String)
-
findByWikiName
public UserProfile findByWikiName(java.lang.String index) throws NoSuchPrincipalException
Description copied from class:AbstractUserDatabaseLooks up and returns the firstUserProfilein the user database that matches a profile having a given wiki name. If the user database does not contain a user with a matching attribute, throws aNoSuchPrincipalException.- Specified by:
findByWikiNamein interfaceUserDatabase- Specified by:
findByWikiNamein classAbstractUserDatabase- Parameters:
index- the wiki name of the desired user profile- Returns:
- the user profile
- Throws:
NoSuchPrincipalException- See Also:
UserDatabase.findByWikiName(String)
-
getWikiNames
public java.security.Principal[] getWikiNames() throws WikiSecurityException
Returns all WikiNames that are stored in the UserDatabase as an array of WikiPrincipal objects. If the database does not contain any profiles, this method will return a zero-length array.- Returns:
- the WikiNames
- Throws:
WikiSecurityException
-
initialize
public void initialize(Engine engine, java.util.Properties props) throws NoRequiredPropertyException, WikiSecurityException
Description copied from class:AbstractUserDatabaseInitializes the user database based on values from a Properties object.- Specified by:
initializein interfaceUserDatabase- Specified by:
initializein classAbstractUserDatabase- Throws:
NoRequiredPropertyExceptionWikiSecurityException- See Also:
UserDatabase.initialize(org.apache.wiki.api.core.Engine, java.util.Properties)
-
rename
public void rename(java.lang.String loginName, java.lang.String newName) throws NoSuchPrincipalException, DuplicateUserException, WikiSecurityException
Description copied from interface:UserDatabaseRenames a
UserProfilein the user database by changing the profile's login name. Because the login name is the profile's unique identifier, implementations should verify that the identifier is "safe" to change before actually changing it. Specifically: the profile with the supplied login name must already exist, and the proposed new name must not be in use by another profile.This method is intended to be atomic; results cannot be partially committed. If the commit fails, it should roll back its state appropriately. Implementing classes that persist to the file system may wish to make this method
synchronized.- Parameters:
loginName- the existing login name for the profilenewName- the proposed new login name- Throws:
NoSuchPrincipalException- if the user profile identified byloginNamedoes not existDuplicateUserException- if another user profile with the proposed new login name already existsWikiSecurityException- if the profile cannot be renamed for any reason, such as an I/O error, database connection failure or lack of support for renames.- See Also:
UserDatabase.rename(String, String)
-
save
public void save(UserProfile profile) throws WikiSecurityException
Description copied from class:AbstractUserDatabaseSaves a
UserProfileto the user database, overwriting the existing profile if it exists. The user name under which the profile should be saved is returned by the supplied profile'sUserProfile.getLoginName()method.The database implementation is responsible for detecting potential duplicate user profiles; specifically, the login name, wiki name, and full name must be unique. The implementation is not required to check for validity of passwords or e-mail addresses. Special case: if the profile already exists and the password is null, it should retain its previous value, rather than being set to null.
Implementations are required to time-stamp the creation or modification fields of the UserProfile./p>
This method is intended to be atomic; results cannot be partially committed. If the commit fails, it should roll back its state appropriately. Implementing classes that persist to the file system may wish to make this method
synchronized.- Specified by:
savein interfaceUserDatabase- Specified by:
savein classAbstractUserDatabase- Parameters:
profile- the user profile to save- Throws:
WikiSecurityException- if the profile cannot be saved- See Also:
UserDatabase.save(org.apache.wiki.auth.user.UserProfile)
-
-