1

如何使用 appfoundation 获取失败的登录尝试?它有一个函数 getFailedLoginAttempts() 但它没有任何意义。我正在使用 Vaadin 6 (6.8.12)

我的代码在这里:

NativeButton login = new NativeButton(Lang.getMessage("login"), new ClickListener() {

         private static final long serialVersionUID = -5577423546946890721L;

         public void buttonClick(ClickEvent event) {

             username = (String) usernameField.getValue();
             String password = (String) passwordField.getValue();
            try {
                AuthenticationUtil.authenticate(username, password);            
                startApp();    
            } catch (InvalidCredentialsException e) {
             /* I need to get failed login attempts here but how can I do that? 
I just can by doing this: AuthenticationUtil.authenticate(username, password).
getFailedLoginAttempts(),  but I need to write try and catch blocks 
(so try and catch blocks in catch block) for authenticate method and this
function will never work, because user won't be authenticated and all 
the time you will go to the other catch block; */
                feedbackLabel.setValue(Lang.getMessage("usernameOrPasswordIncorect"));                    
            } catch (AccountLockedException e) {
                feedbackLabel.setValue(Lang.getMessage("accountBlocked"));
            }
        }
    });

我在评论中写了所有问题。我想打印一些失败的登录尝试,但无法获得这个数字。有什么建议么?

4

2 回答 2

0

我写了一个函数来让我自己的类中的用户。

private User getUserForUsername(String username) {
    String query = "SELECT u FROM User u WHERE u.username = :username";
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("username", username);
    return (User) FacadeFactory.getFacade().find(query, parameters);
}

并在 catch 块中调用此方法。

try {
    AuthenticationUtil.authenticate(username, password);
    startApp();
} catch (InvalidCredentialsException e) {
    User user = getUserForUsername(username);
    feedbackLabel.setValue(Lang.getMessage("usernameOrPasswordIncorect"));
    attemptsLeftLabel.setValue(Lang.getMessage("attemptsLeft", 5 - user.getFailedLoginAttempts()));
} catch (AccountLockedException e) {
    feedbackLabel.setValue(Lang.getMessage("accountBlocked"));
}

一切正常。

于 2013-09-02T09:07:49.130 回答
0

代码AuthenticationUtil.authenticate(username, password). getFailedLoginAttempts()编译的原因是该authenticate方法在成功验证后返回一个 User 对象,然后可以访问该方法。但是,如果抛出异常,则您没有 User 对象。然后在他们的单元测试中使用 FactoryFacade 来检索用户对象并以这种方式进行检查。我不太了解 API,所以我无法告诉您是否可以通过其他方式获取用户。

https://code.google.com/p/vaadin-appfoundation/source/browse/src/org/vaadin/appfoundation/authentication/util/AuthenticationUtil.java

于 2013-09-02T07:28:35.380 回答