Playframework 按批注播放自定义安全控制器验证不起作用

Playframework 按批注播放自定义安全控制器验证不起作用,playframework,Playframework,我有以下定制的安全控制器,使用安全模块进行播放: public class Security extends Secure.Security { static boolean authenticate(String username, String password) { validation.required(username); validation.required(password); if (!validation.hasE

我有以下定制的安全控制器,使用安全模块进行播放:

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方法中的验证机制起作用。当我使用注释时,password参数不再获得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;
    }
}
奇怪的是,用户名验证确实有效(用户名为空时出错)。只有密码为空时,验证没有错误

我希望你能帮助我。

在我四处挖掘之后

使用play验证HTTP数据 验证可确保数据具有特定值或满足特定要求 要求您可以使用验证来验证您的模型是否正确 在将它们保存到数据库之前进行更正,或直接在上使用它们 验证简单表单的HTTP参数

参考:

在调用您的实现方法之前,它首先调用安全类中的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();
    }
你可以看到这条线为什么不起作用的深层原因->

在我仔细研究之后

使用play验证HTTP数据 验证可确保数据具有特定值或满足特定要求 要求您可以使用验证来验证您的模型是否正确 在将它们保存到数据库之前进行更正,或直接在上使用它们 验证简单表单的HTTP参数

参考:

在调用您的实现方法之前,它首先调用安全类中的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();
    }
您可以看到此线程,了解其无法正常工作的深层原因->