Java Code Examples for org.apache.shiro.crypto.hash.SimpleHash#toHex()

The following examples show how to use org.apache.shiro.crypto.hash.SimpleHash#toHex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserController.java    From jboot-admin with Apache License 2.0 6 votes vote down vote up
/**
 * 修改密码提交
 */
@Before( {POST.class, ChangePwdValidator.class} )
public void postChangepwd() {
    User sysUser = getBean(User.class, "user");
    if (!sysUser.getId().equals(AuthUtils.getLoginUser().getId())) {
        throw new BusinessException("无权操作");
    }

    String pwd = getPara("newPwd");


    String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
    SimpleHash hash = new SimpleHash("md5", pwd, salt2, 2);
    pwd = hash.toHex();
    sysUser.setPwd(pwd);
    sysUser.setSalt2(salt2);
    sysUser.setLastUpdAcct(AuthUtils.getLoginUser().getName());
    sysUser.setLastUpdTime(new Date());
    sysUser.setNote("用户修改密码");

    if (!userService.update(sysUser)) {
        throw new BusinessException("修改密码失败");
    }

    renderJson(RestResult.buildSuccess());
}
 
Example 2
Source File: UserServiceImpl.java    From songjhh_blog with Apache License 2.0 6 votes vote down vote up
@Override
public void insertUser(UserCustom userCustom) {
    String algorithmName = "md5";
    String username = userCustom.getUsername();
    String password = userCustom.getPassword();
    String salt1 = username;
    String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
    int hashIterations = 3;
    SimpleHash hash = new SimpleHash(algorithmName, password,
            salt1 + salt2, hashIterations);
    String encodedPassword = hash.toHex();
    userCustom.setSalt(salt2);
    userCustom.setPassword(encodedPassword);
    userCustom.setCreatetime(new Date());
    userMapper.insertSelective(userCustom);
}
 
Example 3
Source File: EncryptUtils.java    From parker with MIT License 5 votes vote down vote up
/**
 * Shiro的MD5加密,加密方式是对字符串salt+password进行加密
 * @param salt 盐
 * @param password 密码
 * @return
 */
public static String shiroMd5(String salt, String password){
    String algorithmName = "MD5";
    ByteSource byteSalt = ByteSource.Util.bytes(salt);
    SimpleHash simpleHash = new SimpleHash(algorithmName, password, byteSalt, DEFAULT_ITERATIONS);
    return simpleHash.toHex();
}
 
Example 4
Source File: UserServiceImpl.java    From jboot-admin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean saveUser(User user, Long[] roles) {
    String pwd = user.getPwd();

    if (StrKit.notBlank(pwd)) {
        String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
        SimpleHash hash = new SimpleHash("md5", pwd, salt2, 2);
        pwd = hash.toHex();
        user.setPwd(pwd);
        user.setSalt2(salt2);
    }

    user.setOnlineStatus(UserOnlineStatus.OFFLINE);
    user.setCreatedate(new Date());
    user.setLastUpdTime(new Date());
    user.setNote("保存系统用户");

    return Db.tx(new IAtom() {
        @Override
        public boolean run() throws SQLException {
            if (!user.save()) {
                return false;
            }

            if (roles != null) {
                List<UserRole> list = new ArrayList<UserRole>();
                for (Long roleId : roles) {
                    UserRole userRole = new UserRole();
                    userRole.setUserId(user.getId());
                    userRole.setRoleId(roleId);
                    list.add(userRole);
                }
                int[] rets = userRoleService.batchSave(list);

                for (int ret : rets) {
                    if (ret < 1) {
                        return false;
                    }
                }
            }
            return true;
        }
    });
}
 
Example 5
Source File: UserServiceImpl.java    From jboot-admin with Apache License 2.0 4 votes vote down vote up
@Override
public boolean updateUser(User user, Long[] roles) {
    String pwd = user.getPwd();
    if (StrKit.notBlank(pwd)) {
        String salt2 = new SecureRandomNumberGenerator().nextBytes().toHex();
        SimpleHash hash = new SimpleHash("md5", pwd, salt2, 2);
        pwd = hash.toHex();
        user.setPwd(pwd);
        user.setSalt2(salt2);
    } else {
        user.remove("pwd");
    }

    user.setLastUpdTime(new Date());
    user.setNote("修改系统用户");

    return Db.tx(new IAtom() {
        @Override
        public boolean run() throws SQLException {
            if (!user.update()) {
                return false;
            }

            userRoleService.deleteByUserId(user.getId());

            if (roles != null) {
                List<UserRole> list = new ArrayList<UserRole>();
                for (Long roleId : roles) {
                    UserRole userRole = new UserRole();
                    userRole.setUserId(user.getId());
                    userRole.setRoleId(roleId);
                    list.add(userRole);
                }

                int[] rets = userRoleService.batchSave(list);
                for (int ret : rets) {
                    if (ret < 1) {
                        return false;
                    }
                }
            }
            return true;
        }
    });
}
 
Example 6
Source File: PasswordUtil.java    From ElementVueSpringbootCodeTemplate with Apache License 2.0 2 votes vote down vote up
/**
 *  重新计算md5值
 * @param password
 * @param salt
 * @return
 */
public static String renewPassword(String password, String salt){
    SimpleHash md5hash = new SimpleHash(
            ALGORITHM_NAME, password, salt, HASH_ITERATIONS);
    return md5hash.toHex();
}