码迷,mamicode.com
首页 > 其他好文 > 详细

(五)密码加密

时间:2017-10-08 13:37:16      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:通过   ice   注册页面   try   etc   UI   dom   mon   term   

package com.louis.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    /**
     * 使用md5加密
     */
    public static String md5(String plainText) {
        byte[] secretBytes = null;
        try {
            //普通的字符串转化成数组
            secretBytes = MessageDigest.getInstance("md5").digest(
                    plainText.getBytes());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("找不到md5实例");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
/*    //和数据库的加密一样,同一个字符串通过md5加密,结果是一样的
    public static void main(String[] args) {
        System.out.println(md5("123"));
    }*/
}
package com.louis.web.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

import com.louis.domain.User;
import com.louis.myconvertor.MyConverter;
import com.louis.service.UserService;
import com.louis.service.impl.UserServiceImpl;
import com.louis.utils.MD5Utils;
import com.louis.utils.UUIDUtils;

/**
 * 和用户相关的servlet
 */

public class UserServlet extends BaseServlet {
    
    
    public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("userServlet的add方法执行了");
        return null;
    }
    //调转到注册页面
    public String registUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/jsp/register.jsp";
    }
    
    //完成注册
    public String regist(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //1封装数据
        User user = new User();
        
        //注册自定义转化器
        ConvertUtils.register(new MyConverter(), Date.class);
        
        BeanUtils.populate(user, request.getParameterMap());
        
        //1.1设置用户id
        user.setUid(UUIDUtils.getId());
        
        //1.2设置激活码
        user.setCode(UUIDUtils.getCode());
        
        //1.3加密密码
        user.setPassword(MD5Utils.md5(user.getPassword()));
        
        //2调用service完成注册
        UserService service = new UserServiceImpl();
        service.regist(user);
        
        
        //页面请求转发
        request.setAttribute("msg", "用户已注册,请去邮箱激活");
        return "/jsp/msg.jsp";
    }

}

技术分享

 

(五)密码加密

标签:通过   ice   注册页面   try   etc   UI   dom   mon   term   

原文地址:http://www.cnblogs.com/Michael2397/p/7637055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!