2012-07-18 64 views
0

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

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

现在,我可以创建一个包含验证码的自定义登录表单,并且我也可以重写安全模块中的验证方法。问题是这样的:我无法更改authenticate方法的参数以包含验证码,否则它不再覆盖超类中的方法(并且该方法从未被调用)。

下面是一个简单的登录表单,以适于从“矢部”的例子的验证码:

#{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} 

下面是不起作用简单authenticate方法,因为“代码”和“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; 
    } 
} 

问题:如何从窗体获取值“authenticated”和“randomID”到authenticate方法?有没有其他方式来访问表单中的值?

回答

0

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