DigestUtils

2019-10-06  本文已影响0人  ssttIsme

使用spring的DigestUtils为密码加密

        String password=entity.getPassword();
        if(StringUtils.isEmpty(password)){
            throw new ServiceException("密码不能为空");
        }
        String newPassword = DigestUtils.md5DigestAsHex(password.getBytes());
    public int updateObject(SysUser entity, String roleIds) {
        //参数验证
        if(entity==null){
            throw new ServiceException("更新对象不能为空");
        }
        if(entity.getId()==null){
            throw new ServiceException("更新用户时id不能为空");
        }
        if(StringUtils.isEmpty(roleIds)){
            throw new ServiceException("用户角色不能为空");
        }
        //更新数据
        
        String password = entity.getPassword();
        if(!StringUtils.isEmpty(password)){
            String newPassword = DigestUtils.md5DigestAsHex(password.getBytes());
            entity.setPassword(newPassword);
            System.out.println(newPassword);
        }
        int rows=sysUserDao.updateObject(entity);
        sysUserRoleDao.deleteObject(entity.getId());
        sysUserRoleDao.insertObject(entity.getId(), roleIds.split(","));
        return rows;
    }
    public int saveObject(SysUser entity, String roleIds) {
        entity.setValid(1);
        System.out.println("id="+entity.getId());
        String password=entity.getPassword();
        if(StringUtils.isEmpty(password)){
            throw new ServiceException("密码不能为空");
        }
        String newPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        entity.setPassword(newPassword);
        int rows=sysUserDao.insertObject(entity);
        sysUserRoleDao.insertObject(entity.getId(), roleIds.split(","));
        return rows;
    }

校验密码时 password为前端用户输入的明文

    @Override
    public SysUser login(String username, String password) {
        SysUser user = sysUserDao.findObjectByUserName(username);
        if(user==null) throw new ServiceException("用户不存在");
        //判断密码是否正确
        String md5Pwd=DigestUtils.md5DigestAsHex(password.getBytes());
        if(!user.getPassword().equals(md5Pwd)) {
            System.out.println("get password "+md5Pwd+" real password "+user.getPassword());
            throw new ServiceException("密码错误");
        }
        
        return user;
    }

实体类

package com.school.entity;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.school.common.DateJsonSerializer;

public class SysUser implements Serializable{
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String username;
    private String password;
    private String salt;
    private String email;
    private String mobile;
    private Integer valid=1;
    private Date created;
    private Date updated;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSalt() {
        return salt;
    }
    public void setSalt(String salt) {
        this.salt = salt;
    }
    public String getMobile() {
        return mobile;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public Integer getValid() {
        return valid;
    }
    public void setValid(Integer valid) {
        this.valid = valid;
    }
    @JsonSerialize(using=DateJsonSerializer.class)
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    @JsonSerialize(using=DateJsonSerializer.class)
    public Date getUpdated() {
        return updated;
    }
    public void setUpdated(Date updated) {
        this.updated = updated;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "SysUser [id=" + id + ", username=" + username + ", password=" + password + ", salt=" + salt + ", email="
                + email + ", mobile=" + mobile + ", valid=" + valid + ", created=" + created + ", updated=" + updated
                + "]";
    }
    
    

}
package com.school.entity;

import java.io.Serializable;
import java.util.Date;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.school.common.DateJsonSerializer;

public class Role implements Serializable {

    private static final long serialVersionUID = 1965384786782405073L;
    public Role() {
    }
    private Integer id;
    private String name;
    private String note;
    private Date created;
    private Date updated;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }
    @JsonSerialize(using=DateJsonSerializer.class)
    public Date getCreated() {
        return created;
    }
    public void setCreated(Date created) {
        this.created = created;
    }
    @JsonSerialize(using=DateJsonSerializer.class)
    public Date getUpdated() {
        return updated;
    }
    public void setUpdated(Date updated) {
        this.updated = updated;
    }
    @Override
    public String toString() {
        return "Role [id=" + id + ", name=" + name + ", note=" + note + ", created=" + created + ", updated=" + updated
                + "]";
    }

}

上一篇 下一篇

猜你喜欢

热点阅读