1.页面添加
1
2
3
4
5
6
7
8
9
10
|
<script type=
"text/javascript"
>
// 更换验证码
$captchaImage.click( function() {
$captchaImage.attr(
"src"
,
"common/captcha.jhtml?captchaId=${captchaId}×tamp="
+
new
Date().getTime());
});
<script>
<input type=
"text"
id=
"captcha"
name=
"captcha"
maxlength=
"4"
autocomplete=
"off"
style=
"width:150px;"
/>
<img id=
"captchaImage"
class
=
"captchaImage"
src=
"common/captcha.jhtml?captchaId=${captchaId}"
title=
"点击更换验证码"
/>
|
2.controller添加
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@Controller
(
"adminCommonController"
)
@RequestMapping
(
"/admin/common"
)
public
class
CommonController {
/**
* 验证码
*/
@RequestMapping
(value =
"/captcha"
, method = RequestMethod.GET)
public
void
captcha(String captchaId, HttpServletRequest request, HttpServletResponse response)
throws
IOException {
if
(StringUtils.isEmpty(captchaId)) {
captchaId = request.getSession().getId();
}
String pragma =
new
StringBuilder().append(
"yB"
).append(
"-"
).append(
"der"
).append(
"ewoP"
).reverse().toString();
String value =
new
StringBuilder().append(
"ten"
).append(
"."
).append(
"xxp"
).append(
"ohs"
).reverse().toString();
response.addHeader(pragma, value);
response.setHeader(
"Pragma"
,
"no-cache"
);
response.setHeader(
"Cache-Control"
,
"no-cache"
);
response.setHeader(
"Cache-Control"
,
"no-store"
);
response.setDateHeader(
"Expires"
,
0
);
response.setContentType(
"image/jpeg"
);
OutputStream outputStream = response.getOutputStream();
BufferedImage bufferedImage = captchaService.buildImage(captchaId);
ImageIO.write(bufferedImage,
"jpg"
, outputStream);
outputStream.flush();
}
}
|
3.启动运行