shiro管理下MD5加密的使用

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

1. 正文:

 

package com.service.impl;

import java.util.ArrayList;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.entity.AuthAdmin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.AuthAdminMapper;
import com.service.IAuthAdminService;
import com.service.IAuthRoleService;

/**
 * 用户管理
 * @author JiangYu
 */
@Service
public class AuthAdminServiceImpl implements IAuthAdminService {
	
	private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	
	@Resource
	private AuthAdminMapper _authAdminMapper;
	@Resource
	private IAuthRoleService _authRoleService;
	// 从配置properties文件中读取以下3项
	@Value("${password.algorithmName}")
    	private String algorithmName; // 加密方式:md5
   	@Value("${password.hashIterations}")
   	private int hashIterations;   // 次数:2
   	@Value("${init.password}")
   	private String initPwd;       // 初始化密码:admin
	
	Logger _logger = Logger.getLogger(AuthAdminServiceImpl.class);
	
	//新增用户
	@Override
	public void insertAuthAdmin(AuthAdmin authAdmin) throws Exception {
		if ( StringUtils.isBlank(authAdmin.getAccount())
				|| StringUtils.isBlank(authAdmin.getRoleIds())) {
			_logger.error("---------添加用户数据出错-------------");
			throw new Exception();
		}
		// 密码加盐
		authAdmin.setSalt(randomNumberGenerator.nextBytes().toHex());
        String newPassword = new SimpleHash(
                algorithmName,
                initPwd,
                ByteSource.Util.bytes(authAdmin.getSalt()),
                hashIterations).toHex();

        authAdmin.setPassword(newPassword);
		authAdmin.setRoleName(_authRoleService.selectRoleNameByIds(authAdmin.getRoleIds()));
		authAdmin.setLocked("否");
		_authAdminMapper.insertAuthAdmin(authAdmin);
	}

	/**
	 * 修改密码
	 */
	@Override
	public Integer updatePwd(AuthAdmin admin) throws Exception {
		
		// 密码加盐
		admin.setSalt(randomNumberGenerator.nextBytes().toHex());
        String newPassword = new SimpleHash(
                algorithmName,
                admin.getPassword(),
                ByteSource.Util.bytes(admin.getSalt()),
                hashIterations).toHex();
        admin.setPassword(newPassword);
		Integer resullt  = _authAdminMapper.updateAuthAdmin(admin);
		return resullt;
	}
	
	... ...	private String algorithmName; // 加密方式:md5
   	@Value("${password.hashIterations}")
   	private int hashIterations;   // 次数:2
   	@Value("${init.password}")
   	private String initPwd;       // 初始化密码:admin
	
	Logger _logger = Logger.getLogger(AuthAdminServiceImpl.class);
	
	//新增用户
	@Override
	public void insertAuthAdmin(AuthAdmin authAdmin) throws Exception {
		if ( StringUtils.isBlank(authAdmin.getAccount())
				|| StringUtils.isBlank(authAdmin.getRoleIds())) {
			_logger.error("---------添加用户数据出错-------------");
			throw new Exception();
		}
		// 密码加盐
		authAdmin.setSalt(randomNumberGenerator.nextBytes().toHex());
        String newPassword = new SimpleHash(
                algorithmName,
                initPwd,
                ByteSource.Util.bytes(authAdmin.getSalt()),
                hashIterations).toHex();

        authAdmin.setPassword(newPassword);
		authAdmin.setRoleName(_authRoleService.selectRoleNameByIds(authAdmin.getRoleIds()));
		authAdmin.setLocked("否");
		_authAdminMapper.insertAuthAdmin(authAdmin);
	}

	/**
	 * 修改密码
	 */
	@Override
	public Integer updatePwd(AuthAdmin admin) throws Exception {
		
		// 密码加盐
		admin.setSalt(randomNumberGenerator.nextBytes().toHex());
        String newPassword = new SimpleHash(
                algorithmName,
                admin.getPassword(),
                ByteSource.Util.bytes(admin.getSalt()),
                hashIterations).toHex();
        admin.setPassword(newPassword);
		Integer resullt  = _authAdminMapper.updateAuthAdmin(admin);
		return resullt;
	}
	
	... ...

 

 

 

2. 配置文件中配置:

 

#MD5
password.algorithmName=md5
password.hashIterations=2
#initpwd
init.password=admin

 

 

3. 说明:加密工作主要是 对管理员对象admin  的salt属性、password属性赋值就行了。

存入数据库的是这样的值;

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值