本文介绍了理解围绕JXLoginPane示例的框架的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在在WindowBuilder中可用,这似乎是我尝试做的一个很好的起点,但我需要更多关于如何使用它的信息。到目前为止,我发现的唯一真正帮助我的是。

它确实让我大部分都在那里,但有几个引用,如是.IS_Statics is.MD5Hash isquote.Main 作者未提供。我猜这些是他或她为登录创建的类,但我不知道如何为自己创建它们。

It does get me most of the way there, but there are several references to things like is.IS_Statics, is.MD5Hash, and isquote.Main that the author does not provide. I am guessing that these are classes he or she has created just for logins, but I've no idea how to create them for myself.

我怎样才能充实什么Ruxton已经开始在他的博客上?或者,就此而言,有人可以推荐一个更好的替代 JXLoginPane

How can I flesh out what Ruxton has started on his blog? Or, for that matter, can someone recommend an even better alternative to JXLoginPane?

推荐答案

所有答案都取决于您的要求,但最重要的是您了解 JXLoginPane 是什么以及它是如何工作的。然后,您将能够理解:

All answers will depend on your requirements, but most important thing is you understand what JXLoginPane is and how it works. Then you will be able to understand that 9 years old post:


  • :创建一个带控件的面板,以在基于登录的应用程序中对用户进行身份验证。

  • JXLoginPane: creates a panel with controls to authenticate users in a login based application.

:抽象基类,必须执行登录逻辑并确定用户身份验证是否有效。它保留了<$ href =http://javadoc.geotoolkit.org/external/swingx/org/jdesktop/swingx上通知的 LoginListener 对象的内部列表。整个登录过程中的/auth/LoginEvent.html\">LoginEvents :登录开始,取消,成功和失败。

LoginService: abstract base class that has to do the log-in logic and determine if user authentication is valid or not. It keeps an internal list of LoginListeners object that are notified on LoginEvents during the whole login process: login started, canceled, succeeded and failed.

:旨在安全存储用户在某种缓存中输入的密码的抽象类,为了帮助 LoginService 来验证用户。用于存储密码的实际机制由实现完成。

PasswordStore: abstract class intended to safely store passwords typed by users in some kind of cache, in order to help LoginService to authenticate users. The actual mechanism used to store the passwords is left up to the implementation.

:与 PasswordStore 相同,但是对于用户名。

UserNameStore: same as PasswordStore but for user names.

:此界面提供合同以监听当前的登录过程并因此采取行动。例如,如果同一用户的登录尝试失败5次,您可以阻止该用户假设某人正在尝试破解该帐户,或者如果登录过程成功,那么您可以在数据库中创建新的会话条目,如果您有兴趣保留用户会话的日志表。

LoginListener: this interface provides a contract to "listen" the current login process and act in consequence. For example if a login attempt fails for the same user 5 times you could block that user assuming someone is trying to hack that account, or if login process succeeded then you could create a new session entry in your database if you are interested in keep a log table with users sessions.

现在快速浏览一下那篇文章,我认为这是一个基于数据库服务的实现与基于用户名缓存 UserNameStore

Now taking a quick look to that post, I think it's a database service based implementation combined with Preferences based users names cache for UserNameStore:


  • Class is.ISLoginService LoginService 抽象基类实现。

Class is.ISUserNameStore UserNameStore 抽象类实现。

Class is.ISUserNameStore is the UserNameStore abstract class implementation.

PasswordStore 抽象类没有实现。

Class is.ISL oginListener 是一个 LoginListener 接口实现。

Class is.ISLoginListener is a LoginListener interface implementation.

最后,关于 is.IS_Statics is.MD5Hash isquote。 Main 似乎是该特定项目的实用程序类,与 JXLoginPane 的基本要素无关。您可能拥有自己的课程来帮助您进行登录过程。

Finally, regarding is.IS_Statics, is.MD5Hash, and isquote.Main seem to be utility classes of that specific project and have nothing to do with the essentials of JXLoginPane. You will probably have your own classes that help you with login process.

请考虑这个简单的例子来说明上述概念。你会发现它并不困难。

Please consider this simple example that illustrates the concepts above described. You will see is not that difficult to make it work.

注意: 没有用户名或密码存储实施。

注2: 参见

import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import org.jdesktop.swingx.JXLoginPane;
import org.jdesktop.swingx.auth.LoginAdapter;
import org.jdesktop.swingx.auth.LoginEvent;
import org.jdesktop.swingx.auth.LoginListener;
import org.jdesktop.swingx.auth.LoginService;

public class Demo {

    private JFrame frame;
    private String userName;
    private int failedAttemptsCount = 0;

    private void showLoginDialog() {

        frame = new JFrame("Welcome!");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JXLoginPane loginPane = new JXLoginPane();

        LoginListener loginListener = new LoginAdapter() {
            @Override
            public void loginFailed(LoginEvent source) {
                failedAttemptsCount++;
                String message;
                switch(failedAttemptsCount) {
                    case 1: message = "Come on buddy! What happened?"; break;
                    case 2: message = "Did you just fail again?"; break;
                    case 3: message = "This is embarrassing..."; break;
                        default: message = "You should probably go home and get some sleep...";
                }
                loginPane.setErrorMessage(message);
            }

            @Override
            public void loginSucceeded(LoginEvent source) {
                Demo.this.userName = loginPane.getUserName();
                Demo.this.createAndShowGui();
            }
        };

        LoginService loginService = new LoginService() {
            @Override
            public boolean authenticate(String name, char[] password, String server) throws Exception {
                return name.equals("Sturm") 
                    && String.valueOf(password).equals("StackOverflow") ;
            }
        };

        loginService.addLoginListener(loginListener);
        loginPane.setLoginService(loginService);

        JXLoginPane.JXLoginDialog dialog = new JXLoginPane.JXLoginDialog(frame, loginPane);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);

        // if loginPane was cancelled or closed then its status is CANCELLED
        // and still need to dispose main JFrame to exiting application
        if(loginPane.getStatus() == JXLoginPane.Status.CANCELLED) {
            frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
        }
    }

    private void createAndShowGui() {
        String welcomeMessage = String.format("Welcome %s ! You have successfuly loged in.", userName);
        JPanel panel = new JPanel();
        panel.add(new JLabel(welcomeMessage));

        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().showLoginDialog();
            }
        });
    }
}

这篇关于理解围绕JXLoginPane示例的框架的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 17:24