zoukankan      html  css  js  c++  java
  • (六)play之yabe项目【验证码】

    添加验证码功能

    在Application.java中添加一个action:captcha()

    Java代码  收藏代码
    1. /** 
    2.  * 添加验证码 
    3.  */  
    4. public static void captcha(String id) {  
    5.     //Images.Captcha继承了InputStream,具备流的功能  
    6.     Images.Captcha captcha = Images.captcha();  
    7.     //向客户端输出流  
    8.     renderBinary(captcha);  
    9. }  

     修改routes文件,为获得验证码添加新的路由

    Html代码  收藏代码
    1. GET     /captcha            Application.captcha  

    访问http://localhost:9000/captcha,即可显示验证码,每次刷新都显示不同的验证码



     

    服务端与客户端如何处理验证码

    验证码生成了,服务端如何对其进行校验呢?

    首先,play是一个无状态的框架,它不会去维护每个客户端的session状态

    session以cookie形式存储的,但是cookie未加密,在客户端能够获取到captcha,不安全

    说一千道一万,要验证都需要对客户端显示的验证码进行跟踪才能实现,不然丢了关联如何验证?!

    解决办法:

    在服务端使用缓存来保存验证码,为其生成一个ID,将此ID传到客户端

    再由客户端显示验证码时传回服务端,服务端拿到ID后保存验证码

    同时,客户端的form中增加一个hidden来保存ID

    当提交表单时,通过这个ID就能找到当时生成的验证码,继而验证

    集群环境下,这个缓存中的验证码怎么处理呢?

    Server A 处理请求,生成验证码,同时保存ID,返回验证码

    提交表单时,请求被分配给Server B 进行处理,Server B 的缓存里没有这个ID啊,怎么办?

    共享缓存,比如,使用Redis作为几个Server共用的一个大缓存

    改造Application的captcha(),将验证码captcha放到缓存中 

    Java代码  收藏代码
    1. /** 
    2.  * 添加验证码 
    3.  * @param id 服务端缓存中存放的验证码的id(一个uuid) 
    4.  */  
    5. public static void captcha(String id) {  
    6.     //Images.Captcha继承了InputStream,具备流的功能  
    7.     Images.Captcha captcha = Images.captcha();  
    8.       
    9.     //为验证码指定颜色并返回验证码  
    10.     String code = captcha.getText("#E4EAFD");  
    11.       
    12.     //放到缓存中,缓存有效期10mn  mn分钟?  
    13.     Cache.set(id, code, "10mn");  
    14.       
    15.     //向客户端输出流  
    16.     renderBinary(captcha);  
    17. }  

    在显示评论的窗体时,生成一个唯一的ID,返回到客户端的页面中

    Java代码  收藏代码
    1. /** 
    2.  * 显示详细的博文评论 
    3.  */  
    4. public static void show(Long id) {  
    5.     Post post = Post.findById(id);  
    6.     //生成一个唯一的ID,作为服务端保存验证码时的key  
    7.     String randomID = Codec.UUID();   
    8.     render(post, randomID);  
    9. }  

    在show.html模板中显示验证码时,传入此ID

    Html代码  收藏代码
    1. <!-- 显示一个表单,用户可以添加评论 -->  
    2. <h3>Post a comment</h3>  
    3. #{form @Application.postComment(post.id)}  
    4.       
    5.     <!-- 在这里显示提交评论时出现的错误 -->  
    6.     #{ifErrors}  
    7.         <class="error">All fields are required!</p>  
    8.     #{/ifErrors}  
    9.       
    10.     <p>  
    11.         <label for="author">Your name:</label>  
    12.         <input type="text" name="author" id="author"/>  
    13.     </p>  
    14.     <p>  
    15.         <label for="content">Your comment:</label>  
    16.         <textarea name="content" id="content"></textarea>  
    17.     </p>  
    18.       
    19.     <!-- 验证码 -->  
    20.     <p>  
    21.         <label for="code">Please type the code below:</label>  
    22.         <img alt="captcha" src="@{Application.captcha(randomID)}">  
    23.         <br/>  
    24.         <input type="text" name="code" id="code" size="18" value=""/>  
    25.         <input type="hidden" name="id" value="${randomID}">  
    26.     </p>  
    27.       
    28.     <p>  
    29.         <input type="submit" value="submit your comment"/>          
    30.     </p>  
    31. #{/form}  

    刷新页面,点击博文添加评论



     

    服务端对验证码进行校验

    客户端已经通过隐藏域保存了验证码的ID,提交表单后,服务端的postComment()接收到ID后就能进行验证操作了!

    让postComment()接收ID并从缓存中取出验证码,进行校验

    此外,具体指定了@Required标明的字段为空时,对应的提示信息

    Java代码  收藏代码
    1. /** 
    2.     * 添加评论 
    3.     * 使用@Required注解,检测author和content参数不能为空 
    4.     *  
    5.     * @param code 客户端输入的验证码 
    6.     * @param randomID 服务端保存验证码时用的ID 
    7.     */  
    8.    public static void postComment(  
    9.                             Long postId,   
    10.                             @Required(message="Author is required") String author,   
    11.                             @Required(message="A comment is required") String content,  
    12.                             @Required(message="Please type the code below") String code,  
    13.                             String randomID) {  
    14.       
    15.     Post post = Post.findById(postId);  
    16.       
    17.     //验证码校验,如果equals()返回false,则message()中的信息将传递到客户端  
    18.     validation.equals(code, Cache.get(randomID)  
    19.             ).message("Invalid code,Please type it again!");  
    20.       
    21.     //错误检测  
    22.     if(validation.hasErrors()) {  
    23.         //将post对象重新传入模板中,否则新打开的show.html中的post为null!!!  
    24.         render("Application/show.html",post);  
    25.     }  
    26.     //保存评论信息  
    27.     post.addComment(author, content);  
    28.       
    29.     //设置提交成功后的提示信息到flash作用域  
    30.     flash.success("Thanks for posting %s", author);  
    31.       
    32.     //重新显示该篇博文即其评论  
    33.     show(postId);  
    34.    }  

    修改show.html模板,如果验证失败,则显示验证失败的提示信息

    Html代码  收藏代码
    1. <!-- 显示一个表单,用户可以添加评论 -->  
    2. <h3>Post a comment</h3>  
    3. #{form @Application.postComment(post.id)}  
    4.       
    5.     <!--   
    6.         这里显示提交评论时出现的错误  
    7.         由于postComment()中已经使用@Required(message="xxx")声明了错误提示信息  
    8.         所以,这里只需要显示第一个错误即可!  
    9.     -->  
    10.     #{ifErrors}  
    11.         <class="error">${errors[0]}</p>  
    12.     #{/ifErrors}  
    13.       
    14.     <p>  
    15.         <label for="author">Your name:</label>  
    16.         <input type="text" name="author" id="author"/>  
    17.     </p>  
    18.     <p>  
    19.         <label for="content">Your comment:</label>  
    20.         <textarea name="content" id="content"></textarea>  
    21.     </p>  
    22.       
    23.     <!-- 验证码 -->  
    24.     <p>  
    25.         <label for="code">Please type the code below:</label>  
    26.         <img alt="captcha" src="@{Application.captcha(randomID)}">  
    27.         <br/>  
    28.         <input type="text" name="code" id="code" size="18" value=""/>  
    29.         <input type="hidden" name="id" value="${randomID}">  
    30.     </p>  
    31.       
    32.     <p>  
    33.         <input type="submit" value="submit your comment"/>          
    34.     </p>  
    35. #{/form}  

     验证错误的情况下,为了保持客户端输入的评论内容,在action中重传客户端输入的内容

    修改Application.postComment(),这里有很多需要注意的地方!!!

    Java代码  收藏代码
    1. /** 
    2.      * 添加评论 
    3.      * 使用@Required注解,检测author和content参数不能为空 
    4.      *  
    5.      * @param code 客户端输入的验证码 
    6.      * @param randomID 服务端保存验证码时用的ID 
    7.      */  
    8.     public static void postComment(  
    9.                                 Long postId,   
    10.                                 String author,   
    11.                                 @Required(message="A comment is required") String content,  
    12.                                 @Required(message="Please type the code below") String code,  
    13.                                 String randomID) {  
    14.           
    15.         System.out.println("Application.postComment()");  
    16.         Post post = Post.findById(postId);  
    17.           
    18.         //验证码校验,如果equals()返回false,则message()中的信息将传递到客户端  
    19.         Logger.info("提交评论,randomID="+randomID);  
    20.         Logger.info("提交评论,验证码="+code);  
    21.         validation.equals(code, Cache.get(randomID)  
    22.                 ).message("Invalid code,Please type it again!");  
    23.           
    24.         //错误检测  
    25.         if(validation.hasErrors()) {  
    26.             /** 
    27.              * 将post对象重新传入模板中,否则新打开的show.html中的post为null!!! 
    28.              * 如果出现错误,则将客户端输入的评论内容重新返回到客户端进行回显。 
    29.              * 必须将randomID重新传回到客户端,不然客户端的然的randomID将取不到值 
    30.              * 最后导致提价评论后,验证码的ID为空,无法进行验证码的校验操作--总是校验错误,这里很关键! 
    31.              * 而且,这里render("Application/show.html")直接调用模板,不会再调用show()进行验证码ID的生成 
    32.              * 所以,一个客户端在未验证成功之前,都将使用这个ID作为服务端缓存的key进行存储 
    33.             */  
    34.             render("Application/show.html",post,randomID,author,content);  
    35.         }  
    36.         //保存评论信息  
    37.         post.addComment(author, content);  
    38.           
    39.         //设置提交成功后的提示信息到flash作用域  
    40.         flash.success("Thanks for posting %s", author);  
    41.           
    42.         //清除指定验证码的缓存  
    43.         Cache.delete(randomID);  
    44.           
    45.         //重新显示该篇博文即其评论  
    46.         show(postId);  
    47.     }  

     在show.html中,为评论的2个输入域增加value属性,用来显示回显的内容

    Html代码  收藏代码
    1. <p>  
    2.     <label for="author">Your name:</label>  
    3.     <!-- ${author}:回填数据 -->  
    4.     <input type="text" name="author" id="author" value="${author}"/>  
    5. </p>  
    6. <p>  
    7.     <label for="content">Your comment:</label>  
    8.     <!-- ${content}:回填数据 -->  
    9.     <textarea name="content" id="content">${content}</textarea>  
    10. </p>  

    刷新页面,重新评论



     

    后台校验发现验证码错误,刷新show.html,回显上一次输入的评论并重新生成验证码



     

    输入正确的验证码,提交评论,成功!



     

    输入正确的验证码之后,评论提交成功!

     

     另外,现在对验证码的要求是,严格区分大小写!

    要实现IgnoreCase,也简单,保存验证码到缓存的时候,校验的时候做点手脚就行了!

  • 相关阅读:
    springboot 整合 pagehelper
    Linux maven安装
    linux 查看端口状态
    mysql执行顺序
    Java int/int 保留2位小数
    【每日一题】30.储物点的距离 (区间处理,前缀和/线段树//树状数组)
    【每日一题】29.maze (BFS 进阶)
    2016年第七届 蓝桥杯C组 C/C++决赛题解
    2016年第七届 蓝桥杯A组 C/C++决赛题解
    第六届蓝桥杯C++A组 A~F题题解
  • 原文地址:https://www.cnblogs.com/zhiji6/p/4445091.html
Copyright © 2011-2022 走看看