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

📄 hbrealm.java

📁 valvae的方法实现类,有效处理较严
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.apache.catalina.realm;import java.sql.PreparedStatement;import java.security.Principal;import java.sql.ResultSet;import java.sql.SQLException;import org.apache.commons.logging.LogFactory;import org.apache.commons.logging.Log;import java.util.ArrayList;import java.security.NoSuchAlgorithmException;import java.security.MessageDigest;import org.apache.catalina.realm.JDBCRealm;public class HbRealm extends JDBCRealm {    public HbRealm() {        super();    }    private static Log log = LogFactory.getLog(HbRealm.class);    public synchronized Principal authenticate(String username,                                               String credentials) {        // Number of tries is the numebr of attempts to connect to the database        // during this login attempt (if we need to open the database)        // This needs rewritten wuth better pooling support, the existing code        // needs signature changes since the Prepared statements needs cached        // with the connections.        // The code below will try twice if there is a SQLException so the        // connection may try to be opened again. On normal conditions (including        // invalid login - the above is only used once.        try {            // Ensure that we have an open database connection            open();            // Look up the user's credentials            String dbCredentials = getPassword(username);            // Validate the user's credentials            boolean validated = false;            if(credentials==null){                return null;            }            validated=credentials.equals(dbCredentials);            if (!validated) {                dbCredentials = this.getInitPassword(username);                 validated=credentials.equals(dbCredentials);            }            if (validated) {                ArrayList roles = getRoles(username);                // Create and return a suitable Principal for this user                return (new GenericPrincipal(this, username, credentials,                                             roles));            }            return null;        } catch (SQLException e) {            // Close the connection so that it gets reopened next time            if (dbConnection != null) {                close(dbConnection);            }        }        // Worst case scenario        return null;    }    private ArrayList getRoles_initUsername(String username) {        PreparedStatement stmt = null;        ResultSet rs = null;        // Number of tries is the numebr of attempts to connect to the database        // during this login attempt (if we need to open the database)        // This needs rewritten wuth better pooling support, the existing code        // needs signature changes since the Prepared statements needs cached        // with the connections.        // The code below will try twice if there is a SQLException so the        // connection may try to be opened again. On normal conditions (including        // invalid login - the above is only used once.        int numberOfTries = 2;        String sql = this.createRoleQueryString_initUsername(roleNameCol);        while (numberOfTries > 0) {            try {                // Ensure that we have an open database connection                open();                try {                    stmt = dbConnection.prepareStatement(sql);                    stmt.setString(1, username);                    rs = stmt.executeQuery();                    ArrayList roleList = new ArrayList();                    while (rs.next()) {                        String roles = rs.getString(1);                        if (null != roles) {                            roleList.add(roles.trim());                        }                    }                    if (roleList.size() == 0) {                        return (null);                    }                    return roleList;                } finally {                    if (rs != null) {                        try {                            rs.close();                        } catch (SQLException e) {                            if (log.isDebugEnabled()) {                                log.debug(e);                            }                        }                    }                    if (stmt != null) {                        try {                            stmt.close();                        } catch (SQLException e) {                            if (log.isDebugEnabled()) {                                log.debug(e);                            }                        }                    }                    dbConnection.commit();                }            } catch (SQLException e) {                System.out.println(e);                // Log the problem for posterity                if (log.isDebugEnabled()) {                    log.debug(e);                }                // Close the connection so that it gets reopened next time                if (dbConnection != null) {                    close(dbConnection);                }            }            numberOfTries--;        }        return (null);    }    private ArrayList getRoles(String username) {        PreparedStatement stmt = null;        ResultSet rs = null;        // Number of tries is the numebr of attempts to connect to the database        // during this login attempt (if we need to open the database)        // This needs rewritten wuth better pooling support, the existing code        // needs signature changes since the Prepared statements needs cached        // with the connections.        // The code below will try twice if there is a SQLException so the        // connection may try to be opened again. On normal conditions (including        // invalid login - the above is only used once.        int numberOfTries = 2;        String sql = this.createRoleQueryString(roleNameCol);        while (numberOfTries > 0) {            try {                // Ensure that we have an open database connection                open();                try {                    stmt = dbConnection.prepareStatement(sql);                    stmt.setString(1, username);                    rs = stmt.executeQuery();                    ArrayList roleList = new ArrayList();                    while (rs.next()) {                        String roles = rs.getString(1);                        if (null != roles) {                            roleList.add(roles.trim());                        }                    }                    if (roleList.size() == 0) {                        return (null);                    }                    return roleList;                } finally {                    if (rs != null) {                        try {                            rs.close();                        } catch (SQLException e) {                            if (log.isDebugEnabled()) {                                log.debug(e);                            }                        }                    }                    if (stmt != null) {                        try {                            stmt.close();                        } catch (SQLException e) {                            if (log.isDebugEnabled()) {                                log.debug(e);                            }                        }                    }                    dbConnection.commit();                }            } catch (SQLException e) {                System.out.println(e);                // Log the problem for posterity                if (log.isDebugEnabled()) {                    log.debug(e);                }                // Close the connection so that it gets reopened next time                if (dbConnection != null) {                    close(dbConnection);                }            }            numberOfTries--;        }        return (null);    }    private String getInitPassword(String username) {        return getDbPassword(username,"init_username", "init_password");    }    private String createQueryString(String username_Column,String password_Column) {        StringBuffer sb = new StringBuffer("SELECT ");        sb.append(password_Column);        sb.append(" FROM ");        sb.append(userTable);        sb.append(" WHERE ");        sb.append(username_Column);        sb.append(" = ?");        return sb.toString();    }    private String createRoleQueryString(String role_Column) {        StringBuffer sb = new StringBuffer("SELECT ");        sb.append(role_Column);        sb.append(" FROM ");

⌨️ 快捷键说明

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