阅读更多
//后台只生成随机数
@GetMapping(value="findRandom")
@ApiOperation(value = "验证码接口", notes = "验证码接口", httpMethod = "GET")
public void findRandom (HttpServletResponse response,HttpSession session) throws IOException {
// 验证码字符个数
int codeCount = 4;
// char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
// 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
// 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char[] codeSequence = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
// 创建一个随机数生成器类
Random random = new Random();
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String strRand = String.valueOf(codeSequence[random.nextInt(10)]);
// 将产生的四个随机数组合在一起。
randomCode.append(strRand);
}
session.setAttribute("code",randomCode);
PrintWriter out = response.getWriter();
out.print(randomCode);
}
//后台获取
@GetMapping(value="getCode1")
@ApiOperation(value = "获取验证码接口", notes = "验证码接口", httpMethod = "GET")
public void getCode1 (HttpServletResponse response, HttpSession session) throws IOException {
System.out.println( session.getAttribute("code")+"======================");
}
https://blog.csdn.net/sinat_32133675/article/details/77247892