⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbcuserdatabase.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.jdbc;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.PropertyList;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.policyframework.Principal;
import com.sslexplorer.security.AccountLockedException;
import com.sslexplorer.security.DefaultUser;
import com.sslexplorer.security.DefaultUserDatabase;
import com.sslexplorer.security.InvalidLoginCredentialsException;
import com.sslexplorer.security.Role;
import com.sslexplorer.security.User;
import com.sslexplorer.security.UserDatabaseException;

/**
 * @author lee
 * 
 * To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
public class JDBCUserDatabase extends DefaultUserDatabase {

    static Log log = LogFactory.getLog(JDBCUserDatabase.class);
    // JDBCConnection con;
    JDBCDatabaseEngine db;
    HashMap permissionModules = new HashMap();
    private PropertyList administrators;

    public JDBCUserDatabase() {
        super("JDBC", true, true, -1);
    }


    /* (non-Javadoc)
     * @see com.sslexplorer.core.Database#open(com.sslexplorer.core.CoreServlet)
     */
    public void open(CoreServlet controllingServlet) throws Exception {
        super.open(controllingServlet);
        String dbName = System.getProperty("sslexplorer.userDatabase.jdbc.dbName", "explorer_configuration");
        controllingServlet.addDatabase(dbName);
        File upgradeDir = new File("install/upgrade");
        String jdbcUser = System.getProperty("sslexplorer.jdbc.username", "sa");
        String jdbcPassword = System.getProperty("sslexplorer.jdbc.password", "");
        String vendorDB = System.getProperty("sslexplorer.jdbc.vendorClass", "com.sslexplorer.jdbc.hsqldb.HSQLDBDatabaseEngine");
        if (log.isInfoEnabled()) {
        	log.info("User database is being opened...");
        	log.info("JDBC vendor class implementation is " + vendorDB);
        }
        db = (JDBCDatabaseEngine) Class.forName(vendorDB).newInstance();
        db.init("userDatabase", dbName, jdbcUser, jdbcPassword, null);
        DBUpgrader upgrader = new DBUpgrader(ContextHolder.getContext()
                        .getVersion(), db, ContextHolder.getContext().getDBDirectory(), upgradeDir);
        upgrader.upgrade();
        administrators = new PropertyList(CoreServlet.getServlet().getPropertyDatabase().getProperty(0, null,
                        "security.administrators"));
        open = true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sshtools.enterprise.admin.UserDatabase#logon(java.lang.String,
     *      java.lang.String)
     */
    public User logon(String username, String password) throws UserDatabaseException, InvalidLoginCredentialsException,
                    AccountLockedException {
        JDBCPreparedStatement ps = null;
        try {
            ps = db.getStatement("logon.user");
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet results = ps.executeQuery();
            try {
                while (results.next()) {
                    try {
                        DefaultUser user = new DefaultUser(results.getString("username"), results.getString("email"), results
                                        .getString("fullname"), results.getDate("last_password_change"),
                                        createUserAttributes(results.getString("username")));
                        loadAttributes(user);
                        addRoles(user);
                        return user;
                    } catch (Exception e) {
                        throw new UserDatabaseException("Failed to get user attributes.");
                    }
                }
            } finally {
                results.close();
            }
        } catch (UserDatabaseException ude) {
            throw ude;
        } catch (SQLException ex) {
            throw new UserDatabaseException("Failed to execute SQL query", ex);
        } catch (ClassNotFoundException ex) {
            throw new UserDatabaseException("Failed to execute SQL query", ex);
        } finally {
            try {
                if (ps != null) {
                    ps.releasePreparedStatement();
                }
            } catch (SQLException e) {
            }
        }
        throw new InvalidLoginCredentialsException();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.sshtools.enterprise.admin.UserDatabase#logout(com.sshtools.enterprise.admin.User)
     */
    public void logout(User user) {
        // TODO Auto-generated method stub
    }

    public User[] listAllUsers(String filter) throws Exception {
        // Get the allowed roles from the database
        JDBCPreparedStatement ps = db.getStatement("select.users");        
        String wildCard = "^" + CoreUtil.replaceAllTokens(filter, "*", ".*") + "$";
        Pattern p = Pattern.compile(wildCard, Pattern.CASE_INSENSITIVE);
        try {
            ResultSet results = ps.executeQuery();
            try {
                Vector tmp = new Vector();
                while (results.next()) {
                    String username = results.getString("username");
                    Matcher matcher = p.matcher(username);
                    if (matcher.matches()) {
                        DefaultUser u = new DefaultUser(results.getString("username"), results.getString("email"), results
                                        .getString("fullname"), results.getDate("last_password_change"),
                                        createUserAttributes(results.getString("username")));
                        loadAttributes(u);
                        addRoles(u);
                        tmp.add(u);
                    }
                }
                DefaultUser[] users = new DefaultUser[tmp.size()];
                tmp.copyInto(users);
                return users;
            } finally {
                results.close();
            }
        } finally {
            ps.releasePreparedStatement();
        }
    }

    Properties createUserAttributes(String username) throws Exception {
        Properties p = new Properties();
        return p;
    }

    public User getAccount(String username) throws Exception {
        // Get the allowed roles from the database
        JDBCPreparedStatement ps = db.getStatement("select.user");
        try {
            ps.setString(1, username);
            ResultSet results = ps.executeQuery();
            try {
                while (results.next()) {
                    DefaultUser u = new DefaultUser(results.getString("username"), results.getString("email"), results
                                    .getString("fullname"), results.getDate("last_password_change"), createUserAttributes(results
                                    .getString("username")));
                    loadAttributes(u);
                    addRoles(u);
                    return u;
                }
                throw new Exception("No user exists with username " + username);
            } finally {
                results.close();
            }
        } finally {
            ps.releasePreparedStatement();
        }
    }

    public User createAccount(String username, String password, String email, String fullname, Role[] roles, Properties attributes)
                    throws Exception {
        JDBCPreparedStatement ps = db.getStatement("create.account");
        try {
            ps.setString(1, username);
            ps.setString(2, email);
            ps.setString(3, password);
            ps.setString(4, fullname);
            ps.execute();
            for (int i = 0; roles != null && i < roles.length; i++) {
                JDBCPreparedStatement ps2 = db.getStatement("assign.role");
                try {
                    ps2.setString(1, username);
                    ps2.setString(2, roles[i].getPrincipalName());
                    ps2.execute();
                } finally {
                    ps2.releasePreparedStatement();
                }
            }
        } finally {
            ps.releasePreparedStatement();
        }
        updateAttributes(username, attributes);
        return getAccount(username);
    }

    public User[] getUsersInRole(Role role) throws Exception {
        return CoreUtil.getUsersInRole(role, this);
    }

    /*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -