使用MD5注册-登录

一、简介

        Message Digest Algorithm MD5(中文名为 消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护。该 算法的文件号为RFC 1321(R.Rivest,MIT Laboratory for Computer Science and RSA Data Security Inc. April 1992)。
 
二、例子使用了Hibernate3.3和Struts2框架,链接Oracle11g数据库
 
三、实体类
package org.md5.entity;

/**
 * Users entity. @author MyEclipse Persistence Tools
 */

public class Users implements java.io.Serializable {

	// Fields

	private Integer id;
	private String name;
	private String password;

	// Constructors

	/** default constructor */
	public Users() {
	}

	/** full constructor */
	public Users(String name, String password) {
		this.name = name;
		this.password = password;
	}

	// Property accessors

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
 
四、使用MessageDigest实现MD5,SHA-1等加密
package org.md5.manage;

// 在Java中使用MessageDigest实现MD5,SHA-1等加密
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/*
 * TestEncrypt.java Description: A test progrm to encrypt a string using MD5 or SHA-1,etc.
 */

public class MessageEncrypt {

	/**
	 * @param strSrc 要加密的字符串
	 * @param encName 加密算法
	 * @return
	 */
	public static String encrypt(String strSrc, String encName) {
		// parameter strSrc is a string will be encrypted,
		// parameter encName is the algorithm name will be used.
		// encName dafault to "MD5"
		// 得到MessageDigest对象
		MessageDigest md = null;
		MessageEncrypt mept = new MessageEncrypt();
		// 加密后的字符串
		String strDes = null;
		// 要加密的字符串字节型数组
		byte[] bt = strSrc.getBytes();
		try {
			if (encName == null || encName.equals("")) {
				encName = "MD5";
			}
			md = MessageDigest.getInstance(encName);
			md.update(bt);
			// 通过执行诸如填充之类的最终操作完成哈希计算
			strDes = mept.bytes2Hex(md.digest()); // to HexString
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Invalid algorithm.\n" + e.getMessage());
			return null;
		}
		return strDes;
	}

	// 将字节数组转换成16进制的字符串
	private String bytes2Hex(byte[] bts) {
		String des = "";
		String tmp = null;

		for (int i = 0; i < bts.length; i++) {
			tmp = (Integer.toHexString(bts[i] & 0xFF));
			if (tmp.length() == 1) {
				des += "0";
			}
			des += tmp;
		}
		return des;
	}

}
  
五、dao
package org.md5.dao;

public interface UserDao {

	void login(String name, String password);

	void registerUser(String name, String pass);

}
 
六、daoImpl
package org.md5.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.md5.dao.UserDao;
import org.md5.entity.Users;
import org.md5.manage.MessageEncrypt;
import org.md5.util.HibernateSessionFactory;

public class UserDaoImpl implements UserDao {

	/**
	 * 登录
	 */
	public void login(String name, String password) {
		Session session = HibernateSessionFactory.getSession();
		session.createQuery("from Users u where u.name=? and u.password=?")
				.setParameter(0, MessageEncrypt.encrypt(name, null))
				.setParameter(1, MessageEncrypt.encrypt(password, null));
		HibernateSessionFactory.closeSession();
	}

	/**
	 * 注册
	 */
	public void registerUser(String name, String pass) {
		Transaction tx = null;
		Session session = HibernateSessionFactory.getSession();
		Users u = new Users();
		u.setName(MessageEncrypt.encrypt(name, null));
		u.setPassword(MessageEncrypt.encrypt(pass, null));
		try {
			tx = session.beginTransaction();
			session.save(u);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

}
 
七、action
package org.md5.action;

import org.md5.dao.UserDao;
import org.md5.dao.impl.UserDaoImpl;
import org.md5.entity.Users;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private UserDao userDao = new UserDaoImpl();
	
	private Users user;
	
	private String name;
	private String password;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	/**
	 * 登录
	 */
	public String login() {
		userDao.login(name, password);
		return SUCCESS;
	}
	
	/**
	 * 注册
	 */
	public String register() {
		userDao.registerUser(user.getName(), user.getPassword());
		return SUCCESS;
	}
}
 
八、struts配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

	<package name="md5" extends="struts-default" namespace="/">
		<!-- 登录 -->
		<action name="login" class="org.md5.action.UserAction" method="login">
			<result>/login_success.jsp</result>
		</action>
		
		<!-- 注册 -->
		<action name="register" class="org.md5.action.UserAction" method="register">
			<result>/success.jsp</result>
			<result name="input">/register.jsp</result>
		</action>
	</package>
	
</struts>    
 

你可能感兴趣的:(Hibernate,MD5,struts2)