图形验证码---简单生成

1.前端js

1

2

3

4

5

6

7

8

9

  // 更换验证码  

  $captchaImage.click( function() {     

    $captchaImage(占位符).attr("src", "common/captcha.jhtml?captchaId=${captchaId}&timestamp=" + new Date().getTime());  

  });

 

 

          

 <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.启动运行

 

 

本站博客:www.wurao.xin

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值