2014-03-05 11:56:45

java简单添加discuz用户

朋友需要这么个功能,非常简单的写了个。有用到的朋友自己加用户是否存在等判断、这里未做同步只是实现了下逻辑。Discuz直接插入到pre_ucenter_members(默认前缀pre,根据自己实际情况)。
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Date;
import java.util.Random;
import org.apache.commons.codec.digest.DigestUtils;
public class Discuz {
	public String getRandomString(int length) {
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}
	public String Encryptions(String pwd, String salt) {
		return DigestUtils.md5Hex(DigestUtils.md5Hex(pwd) + salt).toString();
	}
	public void addUser() {
		try{
			Class.forName("com.mysql.jdbc.Driver");
			String salt = getRandomString(6);//随机的salt
			String pwd = Encryptions("123456", salt);//混合salt加密后的密码
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root");
			int i = conn.prepareStatement("insert into pre_ucenter_members (username,password,email,regip,regdate,salt) values ('test','"+pwd+"','admin@163.com','127.0.0.1','"+Math.round(new Date().getTime()/1000)+"','"+salt+"')").executeUpdate();
			System.out.println(i>0?"添加成功!":"添加失败.");
		}catch(Exception e){
			System.out.println(e.toString());
		}
	}
	public static void main(String[] args) {
		new Discuz().addUser();
	}
}
发表回复