繁体   English   中英

将验证码添加到PlayFramework 1.2.x登录页面

[英]Adding a Captcha to a PlayFramework 1.2.x login page

我的任务是维护一个用PlayFramework 1.2.x编写的小型应用程序。 需要完成的一项:将验证码添加到登录页面。 这显然比人们认为的要难,因为登录页面是由安全模块半自动处理的。

当处理普通的HTTP表单时,PlayFramework将表单变量作为参数传递给控制器​​方法。 使用标准的安全模块,登录表单仅包含用户名和密码,并将它们传递给authenticate方法。

现在,我可以创建一个包含验证码的自定义登录表单,并且还可以覆盖“安全”模块中的authenticate方法。 问题是这样的:我无法将authenticate方法的参数更改为包含验证码,否则它将不再覆盖超类中的方法(因此永远不会调用该方法)。

这是一个简单的登录表单,验证码改编自“ yabe”示例:

#{extends 'main.html' /} #{set title:'Login' /}

#{form @authenticate()}

<p>
    <label for="username">User name: </label> <input type="text"
        name="username" id="username" maxlength="30"
        value="${params.username}" />
</p>
<p>
    <label for="password">Password: </label> <input type="password"
        name="password" id="password" maxlength="30" " />
</p>
<p>
    <label for="code">Please type the code below: </label> <img
        src="@{Captcha.captcha(randomID)}" /> <br /> <input type="text"
        name="code" id="code" size="18" value="" /> <input type="hidden"
        name="randomID" value="${randomID}" />
</p>
<p>
    <input type="submit" id="signin" value="Log in" />
</p>

#{/form}

这是一个不起作用的简单身份验证方法,因为未定义“代码”和“ randomID”。 同样,我不能只是添加参数来进行身份验证,否则它不再覆盖超类中的方法,因此不会被调用。

package controllers;

import play.cache.Cache;
import play.data.validation.Required;
import models.*;

public class Security extends Secure.Security {


    static boolean authenticate(String username, String password) {
        boolean auth = false;
        if (code.equals(Cache.get(randomID))) {
            auth = (User.connect(username, password) != null);
        }
        return auth;
    }
}

问题:如何从表单到authenticate方法获取值“ code”和“ randomID”? 还有其他从表单访问值的方法吗?

您正在玩游戏! 控制器,因此您可以访问对象request.params ,可以从其中获取参数,如request.params.get("code")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM