我有以下自定义的安全控制器,使用安全模块进行播放:
public class Security extends Secure.Security {
static boolean authenticate(String username, String password) {
validation.required(username);
validation.required(password);
if (!validation.hasErrors()) {
BetaUser user = BetaUser.find("username", username).first();
if (user != null && user.password.equals(password)) {
Session.current().put("userid", user.id);
return true;
}
return false;
}
else {
return false;
}
}
static void onAuthenticated() {
Series.userSeries();
}
static void onDisconnected() {
Application.index();
}
static boolean check(String profile) {
if ("admin".equals(profile)) {
return Security.connected().equals("admin");
}
return false;
}
}
在这种情况下,authenticate方法中的验证机制起作用。当我使用注释时,密码参数不再得到validatet:
static boolean authenticate(@Required String username, @Required String password) {
if (!validation.hasErrors()) {
BetaUser user = BetaUser.find("username", username).first();
if (user != null && user.password.equals(password)) {
Session.current().put("userid", user.id);
return true;
}
return false;
}
else {
return false;
}
}
奇怪的是,用户名验证确实有效(当用户名为空时出错)。只有密码为空时,验证没有错误。。。
我希望你能帮助我。
在我四处搜寻后,
正在通过播放验证HTTP数据
验证确保数据具有特定值或满足特定要求您可以使用验证来验证您的模型在将它们保存到数据库之前进行更正、或直接在验证简单表单的HTTP参数。
参考编号:http://www.playframework.org/documentation/1.2.4/validation
在调用实现方法之前,它首先调用Secure类中的authenticate方法。所以,这就是为什么注释它在您的实现方法中不起作用。
public static void authenticate(@Required String username, String password, boolean remember) throws Throwable {
// Check tokens
Boolean allowed = false;
try {
// This is the deprecated method name
allowed = (Boolean)Security.invoke("authentify", username, password);
} catch (UnsupportedOperationException e ) {
// This is the official method name
allowed = (Boolean)Security.invoke("authenticate", username, password);
}
if(validation.hasErrors() || !allowed) {
flash.keep("url");
flash.error("secure.error");
params.flash();
login();
}
// Mark user as connected
session.put("username", username);
// Remember if needed
if(remember) {
response.setCookie("rememberme", Crypto.sign(username) + "-" + username, "30d");
}
// Redirect to the original URL (or /)
redirectToOriginalURL();
}
您可以看到这个线程的深层原因,为什么它不起作用->使用net.sf.oval(在播放框架中)进行参数验证