?web get請(qǐng)求獲取圖片
<div class="p2">
<img id="imgId" src="/get/code">
<a href="#">看不清,換一張</a>
</div>
后臺(tái)代碼:
/*獲取動(dòng)態(tài)驗(yàn)證碼*/
@ResponseBody
@RequestMapping(value = "/get/code", method = {RequestMethod.POST, RequestMethod.GET})
public void getCode(HttpServletResponse response) {
creatImg(response);
}
private void creatImg(HttpServletResponse response) {
int width = 120;
int height = 30;
// 在內(nèi)存中生成圖片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 先獲取畫筆對(duì)象
Graphics2D g = (Graphics2D) image.getGraphics();
// 設(shè)置灰色
g.setColor(Color.GRAY);
// 畫填充的矩形
g.fillRect(0, 0, width, height);
// 設(shè)置顏色
g.setColor(Color.BLUE);
// 畫邊框
g.drawRect(0, 0, width - 1, height - 1);
// 準(zhǔn)備數(shù)據(jù),隨機(jī)獲取4個(gè)字符
String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
// 設(shè)置顏色
g.setColor(Color.white);
// 設(shè)置字體
g.setFont(new Font("隸書", Font.BOLD, 20));
String code = "";
//構(gòu)造存儲(chǔ)字符的數(shù)組
char[] a = {};
//構(gòu)造存儲(chǔ)字符串的集合
List<String> list = new ArrayList<String>();
Random random = new Random();
int x = 20;
int y = 20;
for (int i = 0; i < 4; i++) {
// void rotate(double theta, double x, double y)
// theta 弧度
// hudu = jiaodu * Math.PI / 180;
// 獲取正負(fù)30之間的角度
int jiaodu = random.nextInt(60) - 30;
double hudu = jiaodu * Math.PI / 180;
g.rotate(hudu, x, y);
// 獲取下標(biāo)
int index = random.nextInt(words.length());
// 返回指定下標(biāo)位置的字符,隨機(jī)獲取下標(biāo)
char ch = words.charAt(index);
//將字符存入字符數(shù)組中
char[] chc = {ch};
//使用字符數(shù)組構(gòu)造字符串
String string = new String(chc);
//將構(gòu)造好的字符串添加進(jìn)list集合中
list.add(string);
// 寫字符串
g.drawString("" + ch, x, y);
g.rotate(-hudu, x, y);
x += 20;
}
for (int i = 0; i < list.size(); i++) {
code += list.get(i);
}
//將驗(yàn)證碼存入上下文中
servletContext.setAttribute("code", code);
// 設(shè)置顏色
g.setColor(Color.GREEN);
int x1, x2, y1, y2;
// 畫干擾線
for (int i = 0; i < 4; i++) {
x1 = random.nextInt(width);
y1 = random.nextInt(height);
x2 = random.nextInt(width);
y2 = random.nextInt(height);
g.drawLine(x1, y1, x2, y2);
}
// 輸出到客戶端
try {
ImageIO.write(image, "jpg", response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
登錄驗(yàn)證:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-794485.html
@RequestMapping(value = "/user/login", method = {RequestMethod.POST, RequestMethod.GET})
public void Login(@RequestBody UserBean userBean, HttpSession session, HttpServletResponse response) throws Exception {
String code = userBean.getCode();
//從上下文獲取存儲(chǔ)的驗(yàn)證碼
String code1 = (String) servletContext.getAttribute("code");
//賬號(hào)密碼為admin.且驗(yàn)證碼(忽略大小寫)輸入正確,則跳轉(zhuǎn)到登陸成功頁(yè)面
if ("".equals(code) || code == null || !code.equalsIgnoreCase(code1)) {
HttpServletResponseUtil.back(response, 202, "驗(yàn)證碼錯(cuò)誤!", null);
return;
}
QueryWrapper<UserBean> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", userBean.getUsername());
queryWrapper.eq("password", userBean.getPassword());
List<UserBean> beans = mapper.selectList(queryWrapper);
if (beans != null && beans.size() > 0) {
log.info("登陸成功,用戶名:" + userBean.getUsername());
log.info("登陸成功,密碼:" + userBean.getPassword());
session.setAttribute("loginUser", userBean);
HttpServletResponseUtil.back(response, 200, "登錄成功!", null);
} else {
HttpServletResponseUtil.back(response, -1, "賬號(hào)密碼錯(cuò)誤", null);
}
}
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-794485.html
到了這里,關(guān)于后臺(tái)生成隨機(jī)驗(yàn)證碼驗(yàn)證登錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!