logindialog.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 1,189 行 · 第 1/4 页

SVN-BASE
1,189
字号

                        // Startup Spark
                        startSpark();

                        // dispose login dialog
                        loginDialog.dispose();

                        // Show ChangeLog if we need to.
                        // new ChangeLogDialog().showDialog();
                    }
                    else {
                        savePasswordBox.setEnabled(true);
                        autoLoginBox.setEnabled(true);
                        enableComponents(true);
                        setProgressBarVisible(false);
                    }
                    return Boolean.valueOf(loginSuccessfull);
                }
            };

            // Start the login process in seperate thread.
            // Disable textfields
            enableComponents(false);

            // Show progressbar
            setProgressBarVisible(true);

            loginValidationThread.start();
        }

        public JPasswordField getPasswordField() {
            return passwordField;
        }

        public Dimension getPreferredSize() {
            final Dimension dim = super.getPreferredSize();
            dim.height = 200;
            return dim;
        }

        public void useSSO(boolean use) {
            if (use) {
                usernameField.setVisible(true);
                passwordField.setVisible(false);
                savePasswordBox.setVisible(false);
                usernameLabel.setVisible(true);
                passwordLabel.setVisible(false);
                serverField.setVisible(true);
                autoLoginBox.setVisible(true);
                serverLabel.setVisible(true);

                headerLabel.setVisible(true);
                accountLabel.setVisible(true);
                accountNameLabel.setVisible(true);



                String server = localPref.getServer();
                if (ModelUtil.hasLength(server)) {
                    serverNameLabel.setText(localPref.getServer());
                    serverField.setText(localPref.getServer());
                }

                if(localPref.getDebug()) {
                    System.setProperty("java.security.krb5.debug","true");
                } else {
                    System.setProperty("java.security.krb5.debug","false");
                }
                System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
                GSSAPIConfiguration config = new GSSAPIConfiguration();
                Configuration.setConfiguration(config);

                

                LoginContext lc = null;
                String princName = localPref.getUsername();
                String princRealm = null;
                try {
                    lc = new LoginContext("GetPrincipal");
                    lc.login();
                    Subject mySubject = lc.getSubject();


                    for (Principal p : mySubject.getPrincipals()) {
                        //TODO: check if principal is a kerberos principal first...
                        String name = p.getName();
                        int indexOne = name.indexOf("@");
                        if (indexOne != -1) {
                            princName = name.substring(0, indexOne);
                            accountNameLabel.setText(princName);
                            princRealm = name.substring(indexOne+1);
                        }
                        loginButton.setEnabled(true);
                    }
                }
                catch (LoginException le) {
                    Log.debug(le.getMessage());
                    useSSO(false);
                }

                String ssoMethod = localPref.getSSOMethod();
                if(!ModelUtil.hasLength(ssoMethod)) {
                    if(System.getProperty("os.name").toLowerCase().startsWith("windows")) {
                        ssoMethod = "dns";
                    } else {
                        ssoMethod = "file";
                    }
                }

                String ssoKdc = null;
                if(ssoMethod.equals("dns")) {
                    ssoKdc = getDnsKdc(princRealm);
                    System.setProperty("java.security.krb5.realm",princRealm);
                    System.setProperty("java.security.krb5.kdc",ssoKdc);
                } else if(ssoMethod.equals("manual")) {
                    princRealm = localPref.getSSORealm();
                    ssoKdc = localPref.getSSOKDC();
                    System.setProperty("java.security.krb5.realm",princRealm);
                    System.setProperty("java.security.krb5.kdc",ssoKdc);
                } else {
                    //else do nothing, java takes care of it for us. Unset the props if they are set
                    System.clearProperty("java.security.krb5.realm");
                    System.clearProperty("java.security.krb5.kdc");
                }

                String userName = localPref.getUsername();
                if (ModelUtil.hasLength(userName)) {
                    usernameField.setText(userName);
                } else {
                    usernameField.setText(princName);
                }
            }
            else {
                autoLoginBox.setVisible(true);
                usernameField.setVisible(true);
                passwordField.setVisible(true);
                savePasswordBox.setVisible(true);
                usernameLabel.setVisible(true);
                passwordLabel.setVisible(true);
                serverLabel.setVisible(true);
                serverField.setVisible(true);

                headerLabel.setVisible(false);
                accountLabel.setVisible(false);
                serverNameLabel.setVisible(false);
                accountNameLabel.setVisible(false);

                Configuration.setConfiguration(null);

                validateDialog();
            }


        }

        /**
         * Login to the specified server using username, password, and workgroup.
         * Handles error representation as well as logging.
         *
         * @return true if login was successful, false otherwise
         */
        private boolean login() {
            final SessionManager sessionManager = SparkManager.getSessionManager();


            boolean hasErrors = false;
            String errorMessage = null;

            // Handle specifyed Workgroup
            String serverName = getServerName();

            if (!hasErrors) {
                localPref = SettingsManager.getLocalPreferences();
                if (localPref.isDebuggerEnabled()) {
                    XMPPConnection.DEBUG_ENABLED = true;
                }

                SmackConfiguration.setPacketReplyTimeout(localPref.getTimeOut() * 1000);

                // Get connection
                try {
                    int port = localPref.getXmppPort();

                    int checkForPort = serverName.indexOf(":");
                    if (checkForPort != -1) {
                        String portString = serverName.substring(checkForPort + 1);
                        if (ModelUtil.hasLength(portString)) {
                            // Set new port.
                            port = Integer.valueOf(portString).intValue();
                        }
                    }

                    boolean useSSL = localPref.isSSL();
                    boolean hostPortConfigured = localPref.isHostAndPortConfigured();

                    ConnectionConfiguration config = null;

                    if (useSSL) {
                        if (!hostPortConfigured) {
                            config = new ConnectionConfiguration(serverName, 5223);
                            config.setSocketFactory(new DummySSLSocketFactory());
                        }
                        else {
                            config = new ConnectionConfiguration(localPref.getXmppHost(), port, serverName);
                            config.setSocketFactory(new DummySSLSocketFactory());
                        }
                    }
                    else {
                        if (!hostPortConfigured) {
                            config = new ConnectionConfiguration(serverName);
                        }
                        else {
                            config = new ConnectionConfiguration(localPref.getXmppHost(), port, serverName);
                        }


                    }

                    if (config != null) {
                        config.setReconnectionAllowed(true);
                        boolean compressionEnabled = localPref.isCompressionEnabled();
                        config.setCompressionEnabled(compressionEnabled);
                        connection = new XMPPConnection(config);
                    }

                    connection.connect();

                    String resource = localPref.getResource();
                    if (!ModelUtil.hasLength(resource)) {
                        resource = "spark";
                    }
                    connection.login(getUsername(), getPassword(), resource, false);

                    sessionManager.setServerAddress(connection.getServiceName());
                    sessionManager.initializeSession(connection, getUsername(), getPassword());
                    sessionManager.setJID(connection.getUser());
                }
                catch (Exception xee) {
                    if (!loginDialog.isVisible()) {
                        loginDialog.setVisible(true);
                    }
                    if (xee instanceof XMPPException) {
                        XMPPException xe = (XMPPException)xee;
                        final XMPPError error = xe.getXMPPError();
                        int errorCode = 0;
                        if (error != null) {
                            errorCode = error.getCode();
                        }
                        if (errorCode == 401) {
                            errorMessage = SparkRes.getString(SparkRes.INVALID_USERNAME_PASSWORD);
                        }
                        else if (errorCode == 502 || errorCode == 504) {
                            errorMessage = SparkRes.getString(SparkRes.SERVER_UNAVAILABLE);
                        }
                        else if (errorCode == 409) {
                            errorMessage = Res.getString("label.conflict.error");
                        }
                        else {
                            errorMessage = SparkRes.getString(SparkRes.UNRECOVERABLE_ERROR);
                        }
                    }
                    else {
                        errorMessage = SparkRes.getString(SparkRes.UNRECOVERABLE_ERROR);
                    }

                    // Log Error
                    Log.warning("Exception in Login:", xee);
                    hasErrors = true;
                }
            }
            if (hasErrors) {
                progressBar.setVisible(false);
                //progressBar.setIndeterminate(false);

                // Show error dialog
                if (loginDialog.isVisible()) {
                    if (!localPref.isSSOEnabled()) {
                        JOptionPane.showMessageDialog(loginDialog, errorMessage, SparkRes.getString(SparkRes.ERROR_DIALOG_TITLE),
                                JOptionPane.ERROR_MESSAGE);
                    }
                    else {
                        JOptionPane.showMessageDialog(loginDialog, "Unabled to connect using Single Sign-On. Please check your principal and server settings.", SparkRes.getString(SparkRes.ERROR_DIALOG_TITLE),
                                JOptionPane.ERROR_MESSAGE);
                        useSSO(false);
                        localPref.setSSOEnabled(false);
                    }
                }
                setEnabled(true);
                return false;
            }

            // Since the connection and workgroup are valid. Add a ConnectionListener
            connection.addConnectionListener(SparkManager.getSessionManager());

            // Persist information
            localPref.setUsername(getUsername());

            // Check to see if the password should be saved.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?