Java Code Examples for cn.hutool.core.util.RandomUtil#randomString()

The following examples show how to use cn.hutool.core.util.RandomUtil#randomString() . 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: LoginController.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * 后台生成图形验证码 :有效
 * @param response
 * @param key
 */
@ApiOperation("获取验证码")
@GetMapping(value = "/randomImage/{key}")
public Result<String> randomImage(HttpServletResponse response,@PathVariable String key){
	Result<String> res = new Result<String>();
	try {
		String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
		String lowerCaseCode = code.toLowerCase();
		String realKey = MD5Util.MD5Encode(lowerCaseCode+key, "utf-8");
		redisUtil.set(realKey, lowerCaseCode, 60);
		String base64 = RandImageUtil.generate(code);
		res.setSuccess(true);
		res.setResult(base64);
	} catch (Exception e) {
		res.error500("获取验证码出错"+e.getMessage());
		e.printStackTrace();
	}
	return res;
}
 
Example 2
Source File: LoginController.java    From jeecg-boot-with-activiti with MIT License 6 votes vote down vote up
/**
 * 获取校验码
 */
@ApiOperation("获取验证码")
@GetMapping(value = "/getCheckCode")
public Result<Map<String,String>> getCheckCode(){
	Result<Map<String,String>> result = new Result<Map<String,String>>();
	Map<String,String> map = new HashMap<String,String>();
	try {
		String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
		String key = MD5Util.MD5Encode(code+System.currentTimeMillis(), "utf-8");
		redisUtil.set(key, code, 60);
		map.put("key", key);
		map.put("code",code);
		result.setResult(map);
		result.setSuccess(true);
	} catch (Exception e) {
		e.printStackTrace();
		result.setSuccess(false);
	}
	return result;
}
 
Example 3
Source File: LoginController.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
 * 获取校验码
 */
@ApiOperation("获取验证码")
@GetMapping(value = "/getCheckCode")
public Result<Map<String,String>> getCheckCode(){
	Result<Map<String,String>> result = new Result<Map<String,String>>();
	Map<String,String> map = new HashMap<String,String>();
	try {
		String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
		String key = MD5Util.MD5Encode(code+System.currentTimeMillis(), "utf-8");
		redisUtil.set(key, code, 60);
		map.put("key", key);
		//update-begin-author:taoyan date:20200210 for:TASK #3391 【bug】安全问题,返回验证码不安全
		String encode = java.util.Base64.getEncoder().encodeToString(code.getBytes("UTF-8"));
		map.put("code",encode);
		//update-end-author:taoyan date:20200210 for:TASK #3391 【bug】安全问题,返回验证码不安全
		result.setResult(map);
		result.setSuccess(true);
	} catch (Exception e) {
		e.printStackTrace();
		result.setSuccess(false);
	}
	return result;
}
 
Example 4
Source File: LoginController.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
 * 后台生成图形验证码
 * @param response
 * @param key
 */
@ApiOperation("获取验证码2")
@GetMapping(value = "/randomImage/{key}")
public Result<String> randomImage(HttpServletResponse response,@PathVariable String key){
	Result<String> res = new Result<String>();
	try {
		String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
		String lowerCaseCode = code.toLowerCase();
		String realKey = MD5Util.MD5Encode(lowerCaseCode+key, "utf-8");
		redisUtil.set(realKey, lowerCaseCode, 60);
		String base64 = RandImageUtil.generate(code);
		res.setSuccess(true);
		res.setResult(base64);
	} catch (Exception e) {
		res.error500("获取验证码出错"+e.getMessage());
		e.printStackTrace();
	}
	return res;
}
 
Example 5
Source File: LoginController.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 后台生成图形验证码 :有效
 * @param response
 * @param key
 */
@ApiOperation("获取验证码")
@GetMapping(value = "/randomImage/{key}")
public Result<String> randomImage(HttpServletResponse response,@PathVariable String key){
	Result<String> res = new Result<String>();
	try {
		String code = RandomUtil.randomString(BASE_CHECK_CODES,4);
		String lowerCaseCode = code.toLowerCase();
		String realKey = MD5Util.MD5Encode(lowerCaseCode+key, "utf-8");
		redisUtil.set(realKey, lowerCaseCode, 60);
		String base64 = RandImageUtil.generate(code);
		res.setSuccess(true);
		res.setResult(base64);
	} catch (Exception e) {
		res.error500("获取验证码出错"+e.getMessage());
		e.printStackTrace();
	}
	return res;
}
 
Example 6
Source File: SMSServiceImpl.java    From spring-cloud-shop with MIT License 6 votes vote down vote up
/**
 * 根据短信渠道获取真实发送的短信内容
 *
 * @param smsCodeEnums 短信渠道枚举
 * @param content      短信模板内容
 * @param phone        手机号码
 * @return 短信发送内容
 */
private String getContent(SMSCodeEnums smsCodeEnums, String content, String phone) {

    switch (smsCodeEnums) {
        case LOGIN_SMS:
            String code = RandomUtil.randomString(RandomUtil.BASE_NUMBER, 6);
            shopRedisTemplate.opsForValue().set("sms:login:code" + phone, code, 10 * 60);
            content = String.format(content, code);
            break;
        case REGISTER_SMS:
            break;
        case FORGET_SMS:
            break;
        case INITIAL_CIPHER_SMS:
            break;
    }
    return content;
}
 
Example 7
Source File: OrderController.java    From simple-microservice with Apache License 2.0 5 votes vote down vote up
/**
 * 下单方法
 *
 * @return
 */
@PostMapping("/submitOrder")
public R submitOrder(HttpServletRequest request,@RequestParam("productId") Long productId, @RequestParam("orderProductName") String orderProductName,@RequestParam("orderPrice") Double orderPrice, @RequestParam("count") int count) {
	String random = RandomUtil.randomString(16);
	//保存订单
	int orderId = orderMapper.submitOrder(random, orderProductName, orderPrice, count, DateUtil.date());
	//再扣除商品库存
	R<Boolean> result = stockServiceClient.deductionStock(productId, count);
	if (!result.getResult()) {
		return ResResultManager.setResultError("库存扣除异常,下单失败");
	}

	//返回结果
	return ResResultManager.setResultSuccess("下单成功!下单商品为:{}"+orderProductName);
}
 
Example 8
Source File: TokenServiceImpl.java    From iot-dc3 with Apache License 2.0 5 votes vote down vote up
@Override
public String generateSalt(String username) {
    String redisSaltKey = Common.Cache.USER + Common.Cache.SALT + Common.Cache.SEPARATOR + username;
    String salt = redisUtil.getKey(redisSaltKey);
    if (StringUtils.isBlank(salt)) {
        salt = RandomUtil.randomString(8);
        redisUtil.setKey(redisSaltKey, salt, Common.Cache.TOKEN_CACHE_TIMEOUT, TimeUnit.MINUTES);
    }
    return salt;
}
 
Example 9
Source File: ArticleRepositoryTest.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * 测试新增
 */
@Test
public void testSave() {
    Article article = new Article(1L, RandomUtil.randomString(20), RandomUtil.randomString(150), DateUtil.date(), DateUtil
            .date(), 0L, 0L);
    articleRepo.save(article);
    log.info("【article】= {}", JSONUtil.toJsonStr(article));
}
 
Example 10
Source File: SimpleCaptcha.java    From mall4j with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
protected void generateCode() {
	this.code = RandomUtil.randomString(CAPTCHA_CODE,this.generator.getLength());
}
 
Example 11
Source File: SysUserController.java    From jeecg-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * 用户注册接口
 * 
 * @param jsonObject
 * @param user
 * @return
 */
@PostMapping("/register")
public Result<JSONObject> userRegister(@RequestBody JSONObject jsonObject, SysUser user) {
	Result<JSONObject> result = new Result<JSONObject>();
	String phone = jsonObject.getString("phone");
	String smscode = jsonObject.getString("smscode");
	Object code = redisUtil.get(phone);
	String username = jsonObject.getString("username");
	//未设置用户名,则用手机号作为用户名
	if(oConvertUtils.isEmpty(username)){
           username = phone;
       }
       //未设置密码,则随机生成一个密码
	String password = jsonObject.getString("password");
	if(oConvertUtils.isEmpty(password)){
           password = RandomUtil.randomString(8);
       }
	String email = jsonObject.getString("email");
	SysUser sysUser1 = sysUserService.getUserByName(username);
	if (sysUser1 != null) {
		result.setMessage("用户名已注册");
		result.setSuccess(false);
		return result;
	}
	SysUser sysUser2 = sysUserService.getUserByPhone(phone);
	if (sysUser2 != null) {
		result.setMessage("该手机号已注册");
		result.setSuccess(false);
		return result;
	}

	if(oConvertUtils.isNotEmpty(email)){
           SysUser sysUser3 = sysUserService.getUserByEmail(email);
           if (sysUser3 != null) {
               result.setMessage("邮箱已被注册");
               result.setSuccess(false);
               return result;
           }
       }

	if (!smscode.equals(code)) {
		result.setMessage("手机验证码错误");
		result.setSuccess(false);
		return result;
	}

	try {
		user.setCreateTime(new Date());// 设置创建时间
		String salt = oConvertUtils.randomGen(8);
		String passwordEncode = PasswordUtil.encrypt(username, password, salt);
		user.setSalt(salt);
		user.setUsername(username);
		user.setRealname(username);
		user.setPassword(passwordEncode);
		user.setEmail(email);
		user.setPhone(phone);
		user.setStatus(CommonConstant.USER_UNFREEZE);
		user.setDelFlag(CommonConstant.DEL_FLAG_0);
		user.setActivitiSync(CommonConstant.ACT_SYNC_0);
		sysUserService.addUserWithRole(user,"ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 test
		result.success("注册成功");
	} catch (Exception e) {
		result.error500("注册失败");
	}
	return result;
}
 
Example 12
Source File: SysUserController.java    From teaching with Apache License 2.0 4 votes vote down vote up
/**
 * 用户注册接口
 * 
 * @param jsonObject
 * @param user
 * @return
 */
@PostMapping("/register")
public Result<JSONObject> userRegister(@RequestBody JSONObject jsonObject, SysUser user) {
	Result<JSONObject> result = new Result<JSONObject>();
	String phone = jsonObject.getString("phone");
	String smscode = jsonObject.getString("smscode");
	Object code = redisUtil.get(phone);
	String username = jsonObject.getString("username");
	//未设置用户名,则用手机号作为用户名
	if(oConvertUtils.isEmpty(username)){
           username = phone;
       }
       //未设置密码,则随机生成一个密码
	String password = jsonObject.getString("password");
	if(oConvertUtils.isEmpty(password)){
           password = RandomUtil.randomString(8);
       }
	String email = jsonObject.getString("email");
	SysUser sysUser1 = sysUserService.getUserByName(username);
	if (sysUser1 != null) {
		result.setMessage("用户名已注册");
		result.setSuccess(false);
		return result;
	}
	SysUser sysUser2 = sysUserService.getUserByPhone(phone);
	if (sysUser2 != null) {
		result.setMessage("该手机号已注册");
		result.setSuccess(false);
		return result;
	}

	if(oConvertUtils.isNotEmpty(email)){
           SysUser sysUser3 = sysUserService.getUserByEmail(email);
           if (sysUser3 != null) {
               result.setMessage("邮箱已被注册");
               result.setSuccess(false);
               return result;
           }
       }

	if (!smscode.equals(code)) {
		result.setMessage("手机验证码错误");
		result.setSuccess(false);
		return result;
	}

	try {
		user.setCreateTime(new Date());// 设置创建时间
		String salt = oConvertUtils.randomGen(8);
		String passwordEncode = PasswordUtil.encrypt(username, password, salt);
		user.setSalt(salt);
		user.setUsername(username);
		user.setRealname(username);
		user.setPassword(passwordEncode);
		user.setEmail(email);
		user.setPhone(phone);
		user.setStatus(1);
		user.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
		user.setActivitiSync(CommonConstant.ACT_SYNC_1);
		sysUserService.addUserWithRole(user,"ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 test
		result.success("注册成功");
	} catch (Exception e) {
		result.error500("注册失败");
	}
	return result;
}
 
Example 13
Source File: SysUserController.java    From jeecg-boot with Apache License 2.0 4 votes vote down vote up
/**
 * 用户注册接口
 * 
 * @param jsonObject
 * @param user
 * @return
 */
@PostMapping("/register")
public Result<JSONObject> userRegister(@RequestBody JSONObject jsonObject, SysUser user) {
	Result<JSONObject> result = new Result<JSONObject>();
	String phone = jsonObject.getString("phone");
	String smscode = jsonObject.getString("smscode");
	Object code = redisUtil.get(phone);
	String username = jsonObject.getString("username");
	//未设置用户名,则用手机号作为用户名
	if(oConvertUtils.isEmpty(username)){
           username = phone;
       }
       //未设置密码,则随机生成一个密码
	String password = jsonObject.getString("password");
	if(oConvertUtils.isEmpty(password)){
           password = RandomUtil.randomString(8);
       }
	String email = jsonObject.getString("email");
	SysUser sysUser1 = sysUserService.getUserByName(username);
	if (sysUser1 != null) {
		result.setMessage("用户名已注册");
		result.setSuccess(false);
		return result;
	}
	SysUser sysUser2 = sysUserService.getUserByPhone(phone);
	if (sysUser2 != null) {
		result.setMessage("该手机号已注册");
		result.setSuccess(false);
		return result;
	}

	if(oConvertUtils.isNotEmpty(email)){
           SysUser sysUser3 = sysUserService.getUserByEmail(email);
           if (sysUser3 != null) {
               result.setMessage("邮箱已被注册");
               result.setSuccess(false);
               return result;
           }
       }

	if (!smscode.equals(code)) {
		result.setMessage("手机验证码错误");
		result.setSuccess(false);
		return result;
	}

	try {
		user.setCreateTime(new Date());// 设置创建时间
		String salt = oConvertUtils.randomGen(8);
		String passwordEncode = PasswordUtil.encrypt(username, password, salt);
		user.setSalt(salt);
		user.setUsername(username);
		user.setRealname(username);
		user.setPassword(passwordEncode);
		user.setEmail(email);
		user.setPhone(phone);
		user.setStatus(CommonConstant.USER_UNFREEZE);
		user.setDelFlag(CommonConstant.DEL_FLAG_0);
		user.setActivitiSync(CommonConstant.ACT_SYNC_0);
		sysUserService.addUserWithRole(user,"ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 test
		result.success("注册成功");
	} catch (Exception e) {
		result.error500("注册失败");
	}
	return result;
}