1. Add verification code
application Controller add captcha()
public static void captcha() {
Images.Captcha captcha = Images.captcha();
renderBinary(captcha);
}
Add Route
GET /captcha Application.captcha
Visit http://localhost:9000/captcha The verification code image has been implemented. What needs to be done now is to verify that the input information is consistent with the verification code
Modify the captcha() method
public static void captcha(String id) {Images.Captcha captcha = Images.captcha();
String code = captcha.getText("#E4EAFD");Cache.set(id, code, "10mn");
renderBinary(captcha); }
Modify the show() method
Post post = Post.findById(id);
String randomID = Codec.UUID();render(post, randomID);
}
Modify the show.html page
for="content">Your message:
" " ;p>
Verify
Modify postComment method
public static void postComment(
Long postId,
@Required(message="Author is required") String author,
@Required(message="A message is required") String content,
@ Required(message="Please type the code") String code,
String randomId) {
Post post = Post.findById(postId);
validation.equals(code, Cache.get(randomId)).message("Invalid code . Please type it again");
if(validation.hasErrors()) {
render("Application/show.html", post);}
post.addComment(author, content); Flash.success( "Thanks for posting %s", author);
Cache.delete(randomId); show(postId);
}
Modify the show.html page
#{ifErrors}
error">
${errors[0]} Net (m.sbmmt.com)!