密码加密

9 篇文章 0 订阅

加密工具类

package com.manager.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * EncoderByMd5
 */
public class MD5Util {
    public static String EncoderByMd5(String str) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            // 这句是关键
            md5.update(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte b[] = md5.digest();
        int i;
        StringBuffer buf = new StringBuffer();
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        System.out.println(buf.toString());
        return buf.toString();
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.EncoderByMd5("Simon"));
    }
}

数据库插入语句,密码字段MD5加密:

INSERT INTO employees(id,uname,uloginname,upwd,urealname,role,phone) VALUES(DEFAULT,'ZHB007','admin',MD5('admin'),'尚鸿运',1,'13312341234',1,1,1,1,NOW(),1,NOW(),NULL,NULL); /*其中upwd密码值用MD5括起来*/

插入后的值加密(32位):

登录的代码:

@RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Object loginajax(HttpSession session, Employees employees) {
        Map<String, String> map = new HashMap<>();
        Employees employee = employeesService.getEmployees(employees);
        if (employee != null) {
            String pwd = MD5Util.EncoderByMd5(employees.getE_Pwd()); //调用加密工具类方法将输入的密码加密和数据库的密码字段值相对应
            if (pwd.equals(employee.getE_Pwd())) {  //判断密码是否相同
                session.setAttribute("loginEmployee", employee);//登陆成功保存会话
                employee.setE_LastLoginTime(new Date());
                employeesService.alterEmployees(employee);
            } else {
                map.put("msg", "false");
            }
        } else {
            map.put("msg", "null");
        }
        return map;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2Hhhhhh

一分也是爱 多多益善

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值