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();
}
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();
}
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;
}
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
}
}
}
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);
}
Aggregations