Search in sources :

Example 31 with Authenticator

use of java.net.Authenticator in project tomee by apache.

the class ReferencingAuthenticator method getPasswordAuthentication.

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    Authenticator cxfauth = auth.get();
    if (cxfauth == null) {
        remove();
    }
    PasswordAuthentication pauth = null;
    if (wrapped != null) {
        try {
            pauth = tryWith(wrapped);
            if (pauth != null) {
                return pauth;
            }
        } catch (Exception e) {
            pauth = null;
        }
    }
    if (cxfauth != null) {
        try {
            pauth = tryWith(cxfauth);
        } catch (Exception e1) {
            pauth = null;
        }
    }
    return pauth;
}
Also used : Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 32 with Authenticator

use of java.net.Authenticator in project tomee by apache.

the class ReferencingAuthenticator method tryWithInternal.

private PasswordAuthentication tryWithInternal(Authenticator a) throws Exception {
    if (a == null) {
        return null;
    }
    final Field[] fields;
    if (SKIPCHECK) {
        fields = Authenticator.class.getDeclaredFields();
    } else {
        fields = AccessController.doPrivileged((PrivilegedAction<Field[]>) () -> Authenticator.class.getDeclaredFields());
    }
    for (final Field f : fields) {
        if (!Modifier.isStatic(f.getModifiers())) {
            f.setAccessible(true);
            Object o = f.get(this);
            f.set(a, o);
        }
    }
    Method method;
    if (SKIPCHECK) {
        method = Authenticator.class.getDeclaredMethod("getPasswordAuthentication");
        method.setAccessible(true);
    } else {
        method = AccessController.doPrivileged((PrivilegedAction<Method>) () -> {
            try {
                return Authenticator.class.getDeclaredMethod("getPasswordAuthentication");
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        });
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            method.setAccessible(true);
            return null;
        });
    }
    return (PasswordAuthentication) method.invoke(a);
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Method(java.lang.reflect.Method) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 33 with Authenticator

use of java.net.Authenticator in project tomee by apache.

the class ReferencingAuthenticator method remove.

private void remove() {
    try {
        // Try Authenticator.getDefault() first, JDK9+
        final MethodHandle mt = MethodHandles.lookup().findStatic(Authenticator.class, "getDefault", MethodType.methodType(Authenticator.class));
        removeInternal((Authenticator) mt.invoke());
    } catch (final NoSuchMethodException | IllegalAccessException ex) {
        removeInternal();
    } catch (Throwable e) {
    // ignore
    }
}
Also used : Authenticator(java.net.Authenticator) MethodHandle(java.lang.invoke.MethodHandle)

Example 34 with Authenticator

use of java.net.Authenticator in project tomee by apache.

the class ReferencingAuthenticator method removeFromChain.

private void removeFromChain(Authenticator a) {
    try {
        if (a.getClass().getName().equals(ReferencingAuthenticator.class.getName())) {
            // multiple referencing authenticators, we can remove ourself
            Field f2 = a.getClass().getDeclaredField("wrapped");
            f2.setAccessible(true);
            Authenticator a2 = (Authenticator) f2.get(a);
            if (a2 == this) {
                f2.set(a, wrapped);
            } else {
                removeFromChain(a2);
            }
        }
    } catch (Throwable t) {
    // ignore
    }
}
Also used : Field(java.lang.reflect.Field) Authenticator(java.net.Authenticator)

Example 35 with Authenticator

use of java.net.Authenticator in project cxf by apache.

the class AbstractCodegenMojo method configureProxyServerSettings.

protected void configureProxyServerSettings() throws MojoExecutionException {
    Proxy proxy = mavenSession.getSettings().getActiveProxy();
    if (proxy != null) {
        getLog().info("Using proxy server configured in maven.");
        if (proxy.getHost() == null) {
            throw new MojoExecutionException("Proxy in settings.xml has no host");
        }
        if (proxy.getHost() != null) {
            System.setProperty(HTTP_PROXY_HOST, proxy.getHost());
        }
        if (String.valueOf(proxy.getPort()) != null) {
            System.setProperty(HTTP_PROXY_PORT, String.valueOf(proxy.getPort()));
        }
        if (proxy.getNonProxyHosts() != null) {
            System.setProperty(HTTP_NON_PROXY_HOSTS, proxy.getNonProxyHosts());
        }
        if (!StringUtils.isEmpty(proxy.getUsername()) && !StringUtils.isEmpty(proxy.getPassword())) {
            final String authUser = proxy.getUsername();
            final String authPassword = proxy.getPassword();
            Authenticator.setDefault(new Authenticator() {

                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(authUser, authPassword.toCharArray());
                }
            });
            System.setProperty(HTTP_PROXY_USER, authUser);
            System.setProperty(HTTP_PROXY_PASSWORD, authPassword);
        }
    }
}
Also used : Proxy(org.apache.maven.settings.Proxy) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

Authenticator (java.net.Authenticator)83 PasswordAuthentication (java.net.PasswordAuthentication)52 URL (java.net.URL)18 Proxy (java.net.Proxy)14 InetSocketAddress (java.net.InetSocketAddress)12 HttpClient (java.net.http.HttpClient)11 Field (java.lang.reflect.Field)10 HttpURLConnection (java.net.HttpURLConnection)10 IOException (java.io.IOException)8 Test (org.junit.Test)7 Method (java.lang.reflect.Method)6 File (java.io.File)5 SocketAddress (java.net.SocketAddress)5 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ServerSocket (java.net.ServerSocket)3 URISyntaxException (java.net.URISyntaxException)3 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3