2025年springboot用户密码加密(spring boot 密码加密)

springboot用户密码加密(spring boot 密码加密)package com learn mall service impl import com learn mall exception LearnMallExc import com learn mall exception LearnMallExc import com learn mall model dao UserMapper import com learn mall model pojo User import com learn



package com.learn.mall.service.impl;

import com.learn.mall.exception.LearnMallException;
import com.learn.mall.exception.LearnMallExceptionEnum;
import com.learn.mall.model.dao.UserMapper;
import com.learn.mall.model.pojo.User;
import com.learn.mall.service.UserService;
import com.learn.mall.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void register(String username, String password) throws LearnMallException {
        //查询用户是否存在,不允许重名
        User result = userMapper.selectByName(username);
        if (result != null) {
            throw new LearnMallException(LearnMallExceptionEnum.USERNAME_EXISTED);
        }
        //用户不存在,执行插入操作
        User user = new User();
        user.setUsername(username);
        //对密码进行MD5加密并加盐
        try {
            user.setPassword(MD5Utils.getMD5Str(password));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        int count = userMapper.insertSelective(user);
        if(count == 0){
            throw new LearnMallException(LearnMallExceptionEnum.INSERT_FAILED);
        }
    }
}
编程小号
上一篇 2025-03-04 10:46
下一篇 2025-02-17 10:40

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/54518.html