Search in sources :

Example 61 with Authenticator

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

the class AbstractTestAuth_JDK method withAuthJDK.

public static UpdateExecutionHTTP withAuthJDK(UpdateExecutionHTTPBuilder builder, String user, String passwd) {
    Authenticator authenticator = AuthLib.authenticator(user, passwd);
    HttpClient hc = HttpClient.newBuilder().authenticator(authenticator).build();
    return builder.httpClient(hc).build();
}
Also used : HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator)

Example 62 with Authenticator

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

the class AbstractTestAuth_JDK method withAuthJDK.

public static QueryExecutionHTTP withAuthJDK(QueryExecutionHTTPBuilder builder, String user, String passwd) {
    Authenticator authenticator = AuthLib.authenticator(user, passwd);
    HttpClient hc = HttpClient.newBuilder().authenticator(authenticator).build();
    return builder.httpClient(hc).build();
}
Also used : HttpClient(java.net.http.HttpClient) Authenticator(java.net.Authenticator)

Example 63 with Authenticator

use of java.net.Authenticator in project twitter-2-weibo by rjyo.

the class HttpClient method getConnection.

private HttpURLConnection getConnection(String url) throws IOException {
    HttpURLConnection con = null;
    if (proxyHost != null && !proxyHost.equals("")) {
        if (proxyAuthUser != null && !proxyAuthUser.equals("")) {
            log("Proxy AuthUser: " + proxyAuthUser);
            log("Proxy AuthPassword: " + proxyAuthPassword);
            Authenticator.setDefault(new Authenticator() {

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // respond only to proxy auth requests
                    if (getRequestorType().equals(RequestorType.PROXY)) {
                        return new PasswordAuthentication(proxyAuthUser, proxyAuthPassword.toCharArray());
                    } else {
                        return null;
                    }
                }
            });
        }
        final Proxy proxy = new Proxy(Type.HTTP, InetSocketAddress.createUnresolved(proxyHost, proxyPort));
        if (DEBUG) {
            log("Opening proxied connection(" + proxyHost + ":" + proxyPort + ")");
        }
        con = (HttpURLConnection) new URL(url).openConnection(proxy);
    } else {
        con = (HttpURLConnection) new URL(url).openConnection();
    }
    if (connectionTimeout > 0 && !isJDK14orEarlier) {
        con.setConnectTimeout(connectionTimeout);
    }
    if (readTimeout > 0 && !isJDK14orEarlier) {
        con.setReadTimeout(readTimeout);
    }
    return con;
}
Also used : Proxy(java.net.Proxy) HttpURLConnection(java.net.HttpURLConnection) Authenticator(java.net.Authenticator) URL(java.net.URL) PasswordAuthentication(java.net.PasswordAuthentication)

Example 64 with Authenticator

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

the class CXFAuthenticator method addAuthenticator.

public static synchronized void addAuthenticator() {
    if (instance == null) {
        instance = new CXFAuthenticator();
        Authenticator wrapped = null;
        if (JavaUtils.isJava9Compatible()) {
            try {
                Method m = ReflectionUtil.getMethod(Authenticator.class, "getDefault");
                wrapped = (Authenticator) m.invoke(null);
            } catch (Exception e) {
            // ignore
            }
        } else {
            for (final Field f : ReflectionUtil.getDeclaredFields(Authenticator.class)) {
                if (f.getType().equals(Authenticator.class)) {
                    ReflectionUtil.setAccessible(f);
                    try {
                        wrapped = (Authenticator) f.get(null);
                        if (wrapped != null && wrapped.getClass().getName().equals(ReferencingAuthenticator.class.getName())) {
                            Method m = wrapped.getClass().getMethod("check");
                            m.setAccessible(true);
                            m.invoke(wrapped);
                        }
                        wrapped = (Authenticator) f.get(null);
                    } catch (Exception e) {
                    // ignore
                    }
                }
            }
        }
        try {
            Class<?> cls;
            InputStream ins = ReferencingAuthenticator.class.getResourceAsStream("ReferencingAuthenticator.class");
            byte[] b = IOUtils.readBytesFromStream(ins);
            if (JavaUtils.isJava9Compatible()) {
                Class<?> methodHandles = Class.forName("java.lang.invoke.MethodHandles");
                Method m = ReflectionUtil.getMethod(methodHandles, "lookup");
                Object lookup = m.invoke(null);
                m = ReflectionUtil.getMethod(lookup.getClass(), "findClass", String.class);
                try {
                    cls = (Class<?>) m.invoke(lookup, "org.apache.cxf.transport.http.ReferencingAuthenticator");
                } catch (InvocationTargetException e) {
                    // use defineClass as fallback
                    m = ReflectionUtil.getMethod(lookup.getClass(), "defineClass", byte[].class);
                    cls = (Class<?>) m.invoke(lookup, b);
                }
            } else {
                ClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {

                    public ClassLoader run() {
                        return new URLClassLoader(new URL[0], ClassLoader.getSystemClassLoader());
                    }
                }, null);
                Method m = ReflectionUtil.getDeclaredMethod(ClassLoader.class, "defineClass", String.class, byte[].class, Integer.TYPE, Integer.TYPE);
                ReflectionUtil.setAccessible(m).invoke(loader, ReferencingAuthenticator.class.getName(), b, 0, b.length);
                cls = loader.loadClass(ReferencingAuthenticator.class.getName());
                try {
                    // clear the acc field that can hold onto the webapp classloader
                    Field f = ReflectionUtil.getDeclaredField(loader.getClass(), "acc");
                    ReflectionUtil.setAccessible(f).set(loader, null);
                } catch (Throwable t) {
                // ignore
                }
            }
            final Authenticator auth = (Authenticator) cls.getConstructor(Authenticator.class, Authenticator.class).newInstance(instance, wrapped);
            if (System.getSecurityManager() == null) {
                Authenticator.setDefault(auth);
            } else {
                AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

                    public Boolean run() {
                        Authenticator.setDefault(auth);
                        return true;
                    }
                });
            }
        } catch (Throwable t) {
        // ignore
        }
    }
}
Also used : InputStream(java.io.InputStream) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) URL(java.net.URL) Field(java.lang.reflect.Field) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Authenticator(java.net.Authenticator)

Example 65 with Authenticator

use of java.net.Authenticator in project cxf 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)

Aggregations

Authenticator (java.net.Authenticator)80 PasswordAuthentication (java.net.PasswordAuthentication)50 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)9 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 ArrayList (java.util.ArrayList)3 Content (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Content)3 Debug (org.sonatype.goodies.httpfixture.server.jetty.behaviour.Debug)3 JCommander (com.beust.jcommander.JCommander)2