Search in sources :

Example 36 with PasswordAuthentication

use of java.net.PasswordAuthentication in project jdk8u_jdk by JetBrains.

the class AuthenticationInfo method readObject.

/* used for serialization of pw */
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    s.defaultReadObject();
    pw = new PasswordAuthentication(s1, s2.toCharArray());
    s1 = null;
    s2 = null;
}
Also used : PasswordAuthentication(java.net.PasswordAuthentication)

Example 37 with PasswordAuthentication

use of java.net.PasswordAuthentication in project jdk8u_jdk by JetBrains.

the class HttpsProxyStackOverflow method startServer.

static BadAuthProxyServer startServer() throws IOException {
    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("xyz", "xyz".toCharArray());
        }
    });
    BadAuthProxyServer server = new BadAuthProxyServer(new ServerSocket(0));
    Thread serverThread = new Thread(server);
    serverThread.start();
    return server;
}
Also used : ServerSocket(java.net.ServerSocket) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 38 with PasswordAuthentication

use of java.net.PasswordAuthentication in project maven-plugins by apache.

the class ProjectInfoReportUtils method getContent.

/**
     * Get the input stream from a URL.
     *
     * @param url not null
     * @param project could be null
     * @param settings not null to handle proxy settings
     * @param encoding the wanted encoding for the URL input stream. If null, UTF-8 will be used.
     * @return the input stream decoded with the wanted encoding as string
     * @throws IOException if any
     * @since 2.3
     */
public static String getContent(URL url, MavenProject project, Settings settings, String encoding) throws IOException {
    String scheme = url.getProtocol();
    if (StringUtils.isEmpty(encoding)) {
        encoding = DEFAULT_ENCODING;
    }
    if ("file".equals(scheme)) {
        InputStream in = null;
        try {
            URLConnection conn = url.openConnection();
            in = conn.getInputStream();
            final String content = IOUtil.toString(in, encoding);
            in.close();
            in = null;
            return content;
        } finally {
            IOUtil.close(in);
        }
    }
    Proxy proxy = settings.getActiveProxy();
    if (proxy != null) {
        if ("http".equals(scheme) || "https".equals(scheme) || "ftp".equals(scheme)) {
            scheme += ".";
        } else {
            scheme = "";
        }
        String host = proxy.getHost();
        if (!StringUtils.isEmpty(host)) {
            Properties p = System.getProperties();
            p.setProperty(scheme + "proxySet", "true");
            p.setProperty(scheme + "proxyHost", host);
            p.setProperty(scheme + "proxyPort", String.valueOf(proxy.getPort()));
            if (!StringUtils.isEmpty(proxy.getNonProxyHosts())) {
                p.setProperty(scheme + "nonProxyHosts", proxy.getNonProxyHosts());
            }
            final String userName = proxy.getUsername();
            if (!StringUtils.isEmpty(userName)) {
                final String pwd = StringUtils.isEmpty(proxy.getPassword()) ? "" : proxy.getPassword();
                Authenticator.setDefault(new Authenticator() {

                    /** {@inheritDoc} */
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(userName, pwd.toCharArray());
                    }
                });
            }
        }
    }
    InputStream in = null;
    try {
        URLConnection conn = getURLConnection(url, project, settings);
        in = conn.getInputStream();
        final String string = IOUtil.toString(in, encoding);
        in.close();
        in = null;
        return string;
    } finally {
        IOUtil.close(in);
    }
}
Also used : Proxy(org.apache.maven.settings.Proxy) InputStream(java.io.InputStream) Properties(java.util.Properties) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 39 with PasswordAuthentication

use of java.net.PasswordAuthentication in project tdi-studio-se by Talend.

the class SforceBasicBulkConnection method setProxyToConnection.

private void setProxyToConnection(ConnectorConfig conn) {
    Proxy socketProxy = null;
    if (!useProxy) {
        proxyHost = System.getProperty("https.proxyHost");
        if (proxyHost != null && System.getProperty("https.proxyPort") != null) {
            proxyPort = Integer.parseInt(System.getProperty("https.proxyPort"));
            proxyUsername = System.getProperty("https.proxyUser");
            proxyPassword = System.getProperty("https.proxyPassword");
            useProxy = true;
        } else {
            proxyHost = System.getProperty("http.proxyHost");
            if (proxyHost != null && System.getProperty("http.proxyPort") != null) {
                proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
                proxyUsername = System.getProperty("http.proxyUser");
                proxyPassword = System.getProperty("http.proxyPassword");
                useProxy = true;
            } else {
                proxyHost = System.getProperty("socksProxyHost");
                if (proxyHost != null && System.getProperty("socksProxyPort") != null) {
                    proxyPort = Integer.parseInt(System.getProperty("socksProxyPort"));
                    proxyUsername = System.getProperty("java.net.socks.username");
                    proxyPassword = System.getProperty("java.net.socks.password");
                    useProxy = true;
                    SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort);
                    socketProxy = new Proxy(Proxy.Type.SOCKS, addr);
                }
            }
        }
    }
    if (useProxy) {
        if (socketProxy != null) {
            conn.setProxy(socketProxy);
        } else {
            conn.setProxy(proxyHost, proxyPort);
        }
        if (proxyUsername != null && !"".equals(proxyUsername)) {
            conn.setProxyUsername(proxyUsername);
            if (proxyPassword != null && !"".equals(proxyPassword)) {
                conn.setProxyPassword(proxyPassword);
                Authenticator.setDefault(new Authenticator() {

                    @Override
                    public PasswordAuthentication getPasswordAuthentication() {
                        if (getRequestorType() == Authenticator.RequestorType.PROXY) {
                            return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
                        } else {
                            return super.getPasswordAuthentication();
                        }
                    }
                });
            }
        }
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 40 with PasswordAuthentication

use of java.net.PasswordAuthentication in project jgnash by ccavanaugh.

the class NetworkAuthenticator method getPasswordAuthentication.

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    final Preferences auth = Preferences.userRoot().node(NODEHTTP);
    final ResourceBundle resources = ResourceUtils.getBundle();
    final char[][] pass = { null };
    final String[] user = new String[1];
    // get the password
    if (auth.get(HTTPPASS, null) != null && !auth.get(HTTPPASS, null).isEmpty()) {
        pass[0] = auth.get(HTTPPASS, null).toCharArray();
    }
    // get the user
    user[0] = auth.get(HTTPUSER, null);
    if (user[0] != null) {
        if (user[0].length() <= 0) {
            user[0] = null;
        }
    }
    // if either returns null, pop a dialog
    if (user[0] == null || pass[0] == null) {
        final Dialog<Pair<String, String>> dialog = new Dialog<>();
        dialog.setTitle(resources.getString("Title.HTTPProxy"));
        dialog.setHeaderText(resources.getString("Message.EnterNetworkAuth"));
        // Set the button types.
        final ButtonType loginButtonType = new ButtonType(resources.getString("Button.Ok"), ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getStylesheets().addAll(MainView.DEFAULT_CSS);
        dialog.getDialogPane().getStyleClass().addAll("dialog");
        dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
        // Create the username and password labels and fields.
        final GridPane grid = new GridPane();
        grid.getStylesheets().addAll(MainView.DEFAULT_CSS);
        grid.getStyleClass().addAll("form");
        final TextField userNameField = new TextField();
        final PasswordField passwordField = new PasswordField();
        grid.add(new Label(resources.getString("Label.UserName")), 0, 0);
        grid.add(userNameField, 1, 0);
        grid.add(new Label(resources.getString("Label.Password")), 0, 1);
        grid.add(passwordField, 1, 1);
        // Enable/Disable login button depending on whether a username was entered.
        final Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
        loginButton.setDisable(true);
        // bind the button, must not be empty
        loginButton.disableProperty().bind(userNameField.textProperty().isEmpty());
        dialog.getDialogPane().setContent(grid);
        // Request focus on the username field by default.
        Platform.runLater(userNameField::requestFocus);
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == loginButtonType) {
                return new Pair<>(userNameField.getText(), passwordField.getText());
            }
            return null;
        });
        final Optional<Pair<String, String>> result = dialog.showAndWait();
        result.ifPresent(usernamePassword -> {
            user[0] = usernamePassword.getKey();
            pass[0] = usernamePassword.getValue().toCharArray();
        });
    }
    return new PasswordAuthentication(user[0], pass[0]);
}
Also used : GridPane(javafx.scene.layout.GridPane) Node(javafx.scene.Node) Label(javafx.scene.control.Label) Dialog(javafx.scene.control.Dialog) TextField(javafx.scene.control.TextField) ResourceBundle(java.util.ResourceBundle) PasswordField(javafx.scene.control.PasswordField) Preferences(java.util.prefs.Preferences) ButtonType(javafx.scene.control.ButtonType) Pair(javafx.util.Pair) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

PasswordAuthentication (java.net.PasswordAuthentication)48 Authenticator (java.net.Authenticator)18 URL (java.net.URL)11 InetSocketAddress (java.net.InetSocketAddress)9 Proxy (java.net.Proxy)8 InetAddress (java.net.InetAddress)6 Test (org.junit.Test)5 HttpURLConnection (java.net.HttpURLConnection)4 MalformedURLException (java.net.MalformedURLException)4 URI (java.net.URI)4 IOException (java.io.IOException)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 Dimension (java.awt.Dimension)2 Font (java.awt.Font)2 File (java.io.File)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 java.net (java.net)2