前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shiro管理下MD5加密的使用

shiro管理下MD5加密的使用

作者头像
微风-- 轻许--
发布2022-04-13 09:07:32
5050
发布2022-04-13 09:07:32
举报
文章被收录于专栏:java 微风java 微风

1. 正文:

代码语言:javascript
复制
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. 配置文件中配置:

代码语言:javascript
复制
#MD5
password.algorithmName=md5
password.hashIterations=2
#initpwd
init.password=admin

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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-02-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档