📄 jdbcuserdatabase.java
字号:
/* JSPWiki - a JSP-based WikiWiki clone. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package com.ecyrd.jspwiki.auth.user;import java.io.*;import java.security.Principal;import java.sql.*;import java.util.*;import java.util.Date;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;import com.ecyrd.jspwiki.NoRequiredPropertyException;import com.ecyrd.jspwiki.WikiEngine;import com.ecyrd.jspwiki.auth.NoSuchPrincipalException;import com.ecyrd.jspwiki.auth.WikiPrincipal;import com.ecyrd.jspwiki.auth.WikiSecurityException;import com.ecyrd.jspwiki.util.Serializer;/** * <p> * Implementation of UserDatabase that persists {@link DefaultUserProfile} * objects 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 are configured via settings in <code>jspwiki.properties</code>. * </p> * <p> * Configurable properties are these: * </p> * <table> * <tr> <thead> * <th>Property</th> * <th>Default</th> * <th>Definition</th> * <thead> </tr> * <tr> * <td><code>jspwiki.userdatabase.datasource</code></td> * <td><code>jdbc/UserDatabase</code></td> * <td>The JNDI name of the DataSource</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.table</code></td> * <td><code>users</code></td> * <td>The table that stores the user profiles</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.attributes</code></td> * <td><code>attributes</code></td> * <td>The CLOB column containing the profile's custom attributes, stored as key/value strings, each separated by newline.</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.created</code></td> * <td><code>created</code></td> * <td>The column containing the profile's creation timestamp</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.email</code></td> * <td><code>email</code></td> * <td>The column containing the user's e-mail address</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.fullName</code></td> * <td><code>full_name</code></td> * <td>The column containing the user's full name</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.loginName</code></td> * <td><code>login_name</code></td> * <td>The column containing the user's login id</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.password</code></td> * <td><code>password</code></td> * <td>The column containing the user's password</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.modified</code></td> * <td><code>modified</code></td> * <td>The column containing the profile's last-modified timestamp</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.uid</code></td> * <td><code>uid</code></td> * <td>The column containing the profile's unique identifier, as a long integer</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.wikiName</code></td> * <td><code>wiki_name</code></td> * <td>The column containing the user's wiki name</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.lockExpiry</code></td> * <td><code>lock_expiry</code></td> * <td>The column containing the date/time when the profile, if locked, should be unlocked.</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.roleTable</code></td> * <td><code>roles</code></td> * <td>The 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).</td> * </tr> * <tr> * <td><code>jspwiki.userdatabase.role</code></td> * <td><code>role</code></td> * <td>The column in the role table that stores user roles. When a new user is * created, this column will be populated with the value * <code>Authenticated</code>. Once created, JDBCUserDatabase does not use * this column again; it is provided strictly for the convenience of * container-managed authentication services.</td> * </tr> * </table> * <p> * 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. * </p> * <p> * This class is typically used in conjunction with a web container's JNDI * resource factory. For example, Tomcat versions 4 and higher provide a basic * JNDI factory for registering DataSources. To give JSPWiki access to the JNDI * resource named by <code></code>, you would declare the datasource resource * similar to this: * </p> * <blockquote><code><Context ...><br/> * ...<br/> * <Resource name="jdbc/UserDatabase" auth="Container"<br/> * type="javax.sql.DataSource" username="dbusername" password="dbpassword"<br/> * driverClassName="org.hsql.jdbcDriver" url="jdbc:HypersonicSQL:database"<br/> * maxActive="8" maxIdle="4"/><br/> * ...<br/> * </Context></code></blockquote> * <p> * JDBC driver JARs should be added to Tomcat's <code>common/lib</code> * directory. For more Tomcat 5.5 JNDI configuration examples, see <a * href="http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html"> * http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html</a>. * </p> * <p> * JDBCUserDatabase commits changes as transactions if the back-end database * supports them. If the database supports transactions, user profile changes * are saved to permanent storage only when the {@link #commit()} method is * called. If the database does <em>not</em> support transactions, then * changes are made immediately (during the {@link #save(UserProfile)} method), * and the {@linkplain #commit()} method no-ops. Thus, callers should always * call the {@linkplain #commit()} method after saving a profile to guarantee * that changes are applied. * </p> * * @author Andrew R. Jaquith * @since 2.3 */public class JDBCUserDatabase extends AbstractUserDatabase{ private static final String NOTHING = ""; public static final String DEFAULT_DB_ATTRIBUTES = "attributes"; public static final String DEFAULT_DB_CREATED = "created"; public static final String DEFAULT_DB_EMAIL = "email"; public static final String DEFAULT_DB_FULL_NAME = "full_name"; public static final String DEFAULT_DB_JNDI_NAME = "jdbc/UserDatabase"; public static final String DEFAULT_DB_LOCK_EXPIRY = "lock_expiry"; public static final String DEFAULT_DB_MODIFIED = "modified"; public static final String DEFAULT_DB_ROLE = "role"; public static final String DEFAULT_DB_ROLE_TABLE = "roles"; public static final String DEFAULT_DB_TABLE = "users"; public static final String DEFAULT_DB_LOGIN_NAME = "login_name"; public static final String DEFAULT_DB_PASSWORD = "password"; public static final String DEFAULT_DB_UID = "uid"; public static final String DEFAULT_DB_WIKI_NAME = "wiki_name"; public static final String PROP_DB_ATTRIBUTES = "jspwiki.userdatabase.attributes"; public static final String PROP_DB_CREATED = "jspwiki.userdatabase.created"; public static final String PROP_DB_EMAIL = "jspwiki.userdatabase.email"; public static final String PROP_DB_FULL_NAME = "jspwiki.userdatabase.fullName"; public static final String PROP_DB_DATASOURCE = "jspwiki.userdatabase.datasource"; public static final String PROP_DB_LOCK_EXPIRY = "jspwiki.userdatabase.lockExpiry"; public static final String PROP_DB_LOGIN_NAME = "jspwiki.userdatabase.loginName"; public static final String PROP_DB_MODIFIED = "jspwiki.userdatabase.modified"; public static final String PROP_DB_PASSWORD = "jspwiki.userdatabase.password"; public static final String PROP_DB_UID = "jspwiki.userdatabase.uid"; public static final String PROP_DB_ROLE = "jspwiki.userdatabase.role"; public static final String PROP_DB_ROLE_TABLE = "jspwiki.userdatabase.roleTable"; public static final String PROP_DB_TABLE = "jspwiki.userdatabase.table"; public static final String PROP_DB_WIKI_NAME = "jspwiki.userdatabase.wikiName"; private DataSource m_ds = null; private String m_deleteUserByLoginName = null; private String m_deleteRoleByLoginName = null; private String m_findByEmail = null; private String m_findByFullName = null; private String m_findByLoginName = null; private String m_findByUid = null; private String m_findByWikiName = null; private String m_renameProfile = null; private String m_renameRoles = null; private String m_updateProfile = null; private String m_findAll = null; private String m_findRoles = null; private String m_initialRole = "Authenticated"; private String m_insertProfile = null; private String m_insertRole = null; private String m_userTable = null; private String m_attributes = null; private String m_email = null; private String m_fullName = null; private String m_lockExpiry = null; private String m_loginName = null; private String m_password = null; private String m_role = null; private String m_roleTable = null; private String m_uid = null; private String m_wikiName = null; private String m_created = null; private String m_modified = null; private boolean m_supportsCommits = false; /** * Looks up and deletes the first {@link UserProfile} in 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 a * {@link NoSuchPrincipalException}. 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 <code>synchronized</code>. * * @param loginName the login name of the user profile that shall be deleted */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -