为了账号安全,请及时绑定邮箱和手机立即绑定

【九月打卡】第16天 Java主流框架实战-SSM开发社交网站

标签:
Java SSM

课程名称:Java工程师2022版

课程章节:SSM开发社交网站

课程内容

①Kaptcha生成验证码:kaptcha 是一个扩展自 simplecaptcha 的验证码库,默认情况下,Kaptcha 非常易于设置和使用,并且默认输出会产生一个很难验证的验证码。

https://img3.sycdn.imooc.com/632915770001ed2a19201030.jpg

课程收获

       使用Kaptcha生成验证码流程:

1. pom.xml添加依赖

<dependency>
    <
groupId>com.github.penggle</groupId>
    <
artifactId>kaptcha</artifactId>
    <
version>2.3.2</version>
</
dependency>


2. applicationContext.xml配置:

<!--配置Kaptcha-->
   
<bean id="kaptchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
        <
property name="config">
            <
bean class="com.google.code.kaptcha.util.Config">
                <
constructor-arg>
                    <
props>
                       
<!--验证码图片不生成边框-->
                        
<prop key="kaptcha.border">no</prop>
                       
<!--验证码图片宽度为120像素-->
                       
<prop key="kaptcha.image.width">120</prop>
                       
<!--验证码图片字体颜色为蓝色-->
                       
<prop key="kaptcha.textproducer.font.color">blue</prop>
                       
<!--每个字符最大占用40像素-->
                       
<prop key="kaptcha.textproducer.font.size">40</prop>
                       
<!--验证码包含4个字符-->
                       
<prop key="kaptcha.textproducer.char.length">4</prop>
                    </
props>
                </
constructor-arg>
            </
bean>
        </
property>
    </
bean>


3. 新增KaptchaController

@Controller
@RequestMapping
("/api/verify")
public class KaptchaController {
   
@Resource
   
private Producer kaptchaProducer;

   
@GetMapping("/code")
   
public void createVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
       
//响应立即过期
       
response.setDateHeader("Expires",0);
       
//不缓存任何图片数据
       
response.setHeader("Cache-Control" , "no-store,no-cache,must-revalidate");
        response.setHeader(
"Cache-Control" ,"post-check=0,pre-check=0");
        response.setHeader(
"Pragma" , "no-cache");
        response.setContentType(
"image/png");
       
//生成验证码图片
       
String text = kaptchaProducer.createText();
        request.getSession().setAttribute(
"kaptchaVerifyCode",text);
       
BufferedImage image = kaptchaProducer.createImage(text);
        
ServletOutputStream outputStream = response.getOutputStream();
       
ImageIO.write(image,"png",outputStream);
       
outputStream.flush();
       
outputStream.close();
    }
}


4. 修改前端后进行验证

String verifyCode = (String) request.getSession().getAttribute("kaptchaVerifyCode");
ResponseUtils resp = null;
if(vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)){
   
resp = new ResponseUtils("VerifyCodeError","验证码错误");
}
else {
   
resp = new ResponseUtils();
}


 

https://img1.sycdn.imooc.com/6329154a0001e92a04440454.jpg

总结:

使用Kaptcha生成验证码后将其放入服务器端的Session中,在验证时获取Session的对应值进行比对,若相符则进行下一步操作。


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消