DefaultLoginAuthenticator.java
- package net.jforum.sso;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Map;
- import net.jforum.JForumExecutionContext;
- import net.jforum.dao.UserDAO;
- import net.jforum.entities.User;
- import net.jforum.exceptions.ForumException;
- import net.jforum.util.DbUtils;
- import net.jforum.util.MD5;
- import net.jforum.util.preferences.SystemGlobals;
- /**
- * Default login authenticator for JForum.
- * This authenticator will validate the input against
- * <i>jforum_users</i>.
- *
- * @author Rafael Steil
- * @version $Id: DefaultLoginAuthenticator.java,v 1.10 2007/07/28 14:17:10 rafaelsteil Exp $
- */
- public class DefaultLoginAuthenticator implements LoginAuthenticator
- {
- private UserDAO userModel;
- /**
- * @see net.jforum.sso.LoginAuthenticator#setUserModel(net.jforum.dao.UserDAO)
- */
- public void setUserModel(UserDAO userModel)
- {
- this.userModel = userModel;
- }
- /**
- * @see net.jforum.sso.LoginAuthenticator#validateLogin(String, String, java.util.Map)
- */
- public User validateLogin(String username, String password, Map extraParams)
- {
- User user = null;
- ResultSet rs=null;
- PreparedStatement p=null;
- try
- {
- p = JForumExecutionContext.getConnection().prepareStatement(
- SystemGlobals.getSql("UserModel.login"));
- p.setString(1, username);
- p.setString(2, MD5.crypt(password));
- rs = p.executeQuery();
- if (rs.next() && rs.getInt("user_id") > 0) {
- user = this.userModel.selectById(rs.getInt("user_id"));
- }
- }
- catch (SQLException e)
- {
- throw new ForumException(e);
- }
- finally
- {
- DbUtils.close(rs, p);
- }
- if (user != null && !user.isDeleted() && (user.getActivationKey() == null || user.isActive())) {
- return user;
- }
- return null;
- }
- }
MD5.java
- package net.jforum.util;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import net.jforum.exceptions.ForumException;
- /**
- * Encodes a string using MD5 hashing
- *
- * @author Rafael Steil
- * @version $Id: MD5.java,v 1.7 2006/08/23 02:13:44 rafaelsteil Exp $
- */
- public class MD5
- {
- /**
- * Encodes a string
- *
- * @param str String to encode
- * @return Encoded String
- * @throws NoSuchAlgorithmException
- */
- public static String crypt(String str)
- {
- if (str == null || str.length() == 0) {
- throw new IllegalArgumentException("String to encript cannot be null or zero length");
- }
- StringBuffer hexString = new StringBuffer();
- try {
- MessageDigest md = MessageDigest.getInstance("MD5");
- md.update(str.getBytes());
- byte[] hash = md.digest();
- for (int i = 0; i < hash.length; i++) {
- if ((0xff & hash[i]) < 0x10) {
- hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
- }
- else {
- hexString.append(Integer.toHexString(0xFF & hash[i]));
- }
- }
- }
- catch (NoSuchAlgorithmException e) {
- throw new ForumException("" + e);
- }
- return hexString.toString();
- }
- }
1248

被折叠的 条评论
为什么被折叠?



