JForum片段代码1

DefaultLoginAuthenticator.java
  1. package net.jforum.sso;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.Map;
  6. import net.jforum.JForumExecutionContext;
  7. import net.jforum.dao.UserDAO;
  8. import net.jforum.entities.User;
  9. import net.jforum.exceptions.ForumException;
  10. import net.jforum.util.DbUtils;
  11. import net.jforum.util.MD5;
  12. import net.jforum.util.preferences.SystemGlobals;
  13. /**
  14.  * Default login authenticator for JForum.
  15.  * This authenticator will validate the input against
  16.  * <i>jforum_users</i>. 
  17.  * 
  18.  * @author Rafael Steil
  19.  * @version $Id: DefaultLoginAuthenticator.java,v 1.10 2007/07/28 14:17:10 rafaelsteil Exp $
  20.  */
  21. public class DefaultLoginAuthenticator implements LoginAuthenticator
  22. {
  23.     private UserDAO userModel;
  24.     /**
  25.      * @see net.jforum.sso.LoginAuthenticator#setUserModel(net.jforum.dao.UserDAO)
  26.      */
  27.     public void setUserModel(UserDAO userModel)
  28.     {
  29.         this.userModel = userModel;
  30.     }
  31.     /**
  32.      * @see net.jforum.sso.LoginAuthenticator#validateLogin(String, String, java.util.Map) 
  33.      */
  34.     public User validateLogin(String username, String password, Map extraParams)
  35.     {
  36.         User user = null;
  37.         ResultSet rs=null;
  38.         PreparedStatement p=null;
  39.         
  40.         try 
  41.         {
  42.             p = JForumExecutionContext.getConnection().prepareStatement(
  43.                     SystemGlobals.getSql("UserModel.login"));
  44.             p.setString(1, username);
  45.             p.setString(2, MD5.crypt(password));
  46.             rs = p.executeQuery();
  47.             if (rs.next() && rs.getInt("user_id") > 0) {
  48.                 user = this.userModel.selectById(rs.getInt("user_id"));
  49.             }
  50.         }
  51.         catch (SQLException e)
  52.         {
  53.             throw new ForumException(e);
  54.         }
  55.         finally
  56.         {
  57.             DbUtils.close(rs, p);
  58.         }
  59.         if (user != null && !user.isDeleted() && (user.getActivationKey() == null || user.isActive())) {
  60.             return user;
  61.         }
  62.         return null;
  63.     }
  64. }

MD5.java

  1. package net.jforum.util;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. import net.jforum.exceptions.ForumException;
  5. /**
  6.  * Encodes a string using MD5 hashing 
  7.  * 
  8.  * @author Rafael Steil
  9.  * @version $Id: MD5.java,v 1.7 2006/08/23 02:13:44 rafaelsteil Exp $
  10.  */
  11. public class MD5 
  12. {
  13.     /**
  14.      * Encodes a string
  15.      * 
  16.      * @param str String to encode
  17.      * @return Encoded String
  18.      * @throws NoSuchAlgorithmException
  19.      */
  20.     public static String crypt(String str)
  21.     {
  22.         if (str == null || str.length() == 0) {
  23.             throw new IllegalArgumentException("String to encript cannot be null or zero length");
  24.         }
  25.         
  26.         StringBuffer hexString = new StringBuffer();
  27.         
  28.         try {
  29.             MessageDigest md = MessageDigest.getInstance("MD5");
  30.             md.update(str.getBytes());
  31.             byte[] hash = md.digest();
  32.             
  33.             for (int i = 0; i < hash.length; i++) {
  34.                 if ((0xff & hash[i]) < 0x10) {
  35.                     hexString.append("0" + Integer.toHexString((0xFF & hash[i])));
  36.                 }               
  37.                 else {
  38.                     hexString.append(Integer.toHexString(0xFF & hash[i]));
  39.                 }               
  40.             }
  41.         }
  42.         catch (NoSuchAlgorithmException e) {
  43.             throw new ForumException("" + e);
  44.         }
  45.         
  46.         return hexString.toString();
  47.     }
  48. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值