如何在Wicket中的SignInPanel中制作自定义错误反馈消息

时间:2014-09-08 12:05:16

标签: passwords ldap wicket feedback

我正在使用Wicket实现一个LoginPage,我没有得到它,如何编写自定义反馈消息,例如,“密码错误”,“用户名错误”或“Accound被锁定”(最后一个)示例应该有点困难,因为它与我认为的Ldap / Ldap错误消息有关..但我认为对于后两个有一个更简单的方法,使用我的LoginPage的属性文件或类似的东西..我试图更改默认的Wicket“登录失败”消息,这通过我的页面的属性文件,我刚刚添加了“signFailed =登录失败的TEST”,并且它已被更改..但是没有得到它如何告诉用户为什么!通行证或用户名错误!

这里的实施:

public class LoginPage extends SampleManagementPage {

private static final long serialVersionUID = -8585718500226951823L;
private SignInPanel signIn;


public LoginPage() {
    signIn = new SignInPanel("signInPanel");
    add(signIn);
}

}

我的SampleManagementPage扩展了WebPage!

这里是LoginPage的属性文件:

page.title =登录样品管理

signInFailed =登录失败的TEST

1 个答案:

答案 0 :(得分:0)

您只能更改signFailed错误消息的原因是wicket SignInPanel仅在其登录表单无法进行身份验证时才会抛出此特定错误。要查看该内容,您可以打开SignInPanel.java

的源代码

解决问题并生成自己的错误消息的一种方法是编写自己的登录面板。我不是说这是唯一的方式,但它对我有用:)

public class LoginPanel extends Panel {

    private static final long serialVersionUID = -1662154893824849377L;

    private static final String SIGN_IN_FORM = "signInForm";

    private boolean includeRememberMe = true;
    private boolean rememberMe = true;
    private String password;
    private String username;

    public LoginPanel(final String id, final boolean includeRememberMe) {
        super(id);
        this.includeRememberMe = includeRememberMe;
        add(new FeedbackPanel("feedback"));
        add(new SignInForm(SIGN_IN_FORM));
    }

    @Override
    protected void onConfigure() {
        if (isSignedIn() == false) {
            IAuthenticationStrategy authenticationStrategy = getApplication().getSecuritySettings().getAuthenticationStrategy();
            String[] data = authenticationStrategy.load();
            if ((data != null) && (data.length > 1)) {
                if (signIn(data[0], data[1])) {
                    username = data[0];
                    password = data[1];
                    onSignInRemembered();
                } else {
                    authenticationStrategy.remove();
                }
            }
        }
        super.onConfigure();
    }

    private boolean signIn(String username, String password) {
        return AuthenticatedWebSession.get().signIn(username, password);
    }

    private boolean userExists(String username) {
        return ((MyWebSession) AuthenticatedWebSession.get()).userExists(username);
    }

    private boolean isSignedIn() {
        return AuthenticatedWebSession.get().isSignedIn();
    }

    protected void onSignInFailed() {
        error(getLocalizer().getString("signInFailed", this, "Wrong password"));
    }

    private void onUserExistsFailed() {
        error(getLocalizer().getString("userExistsFailed", this, "User does not exist"));
    }

    protected void onSignInSucceeded() {
        continueToOriginalDestination();
        setResponsePage(getApplication().getHomePage());
    }

    protected void onSignInRemembered() {
        continueToOriginalDestination();
        throw new RestartResponseException(getApplication().getHomePage());
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public LoginPanel(final String id) {
        this(id, true);
    }

    public final class SignInForm extends StatelessForm<LoginPanel> {
        private static final long serialVersionUID = 1L;

        public SignInForm(final String id) {
            super(id);

            setModel(new CompoundPropertyModel<LoginPanel>(LoginPanel.this));

            add(new TextField<String>("username"));
        add(new PasswordTextField("password"));

            WebMarkupContainer rememberMeRow = new WebMarkupContainer("rememberMeRow");
            add(rememberMeRow);

            rememberMeRow.add(new CheckBox("rememberMe"));

            rememberMeRow.setVisible(includeRememberMe);
        }

        @Override
        public final void onSubmit() {
            IAuthenticationStrategy strategy = getApplication().getSecuritySettings().getAuthenticationStrategy();

            if (!userExists(username)) {
                onUserExistsFailed();
                strategy.remove();
                return;
            }

            if (signIn(getUsername(), getPassword())) {
                if (rememberMe == true) {
                    strategy.save(username, password);
                } else {
                    strategy.remove();
                }

                onSignInSucceeded();
            } else {
                onSignInFailed();
                strategy.remove();
            }
        }
    }

}



public class MyWebSession extends AuthenticatedWebSession {

    private static final long serialVersionUID = -401924496527311251L;

    public MyWebSession(Request request) {
        super(request);
    }

    public boolean userExists(String username) {
        // business login
    }

    @Override
    public boolean authenticate(String username, String password) {
        // business login
    }

    @Override
    public Roles getRoles() {
        Roles roles  = new Roles();
        roles.add(Roles.USER);
        return roles;
    }

}