前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
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();@Resourceprivate AuthAdminMapper _authAdminMapper;@Resourceprivate IAuthRoleService _authRoleService;// 從配置properties文件中讀取以下3項@Value("${password.algorithmName}")private String algorithmName; // 加密方式:md5@Value("${password.hashIterations}")private int hashIterations; // 次數:2@Value("${init.password}")private String initPwd; // 初始化密碼:adminLogger _logger = Logger.getLogger(AuthAdminServiceImpl.class);//新增用戶@Overridepublic 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);}/*** 修改密碼*/@Overridepublic 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; // 初始化密碼:adminLogger _logger = Logger.getLogger(AuthAdminServiceImpl.class);//新增用戶@Overridepublic 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);}/*** 修改密碼*/@Overridepublic 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屬性賦值就行了。
存入數據庫的是這樣的值;
?
?
?