本文整理汇总了Java中java.net.Authenticator类的典型用法代码示例。如果您正苦于以下问题:Java Authenticator类的具体用法?Java Authenticator怎么用?Java Authenticator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Authenticator类属于java.net包,在下文中一共展示了Authenticator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: authenticateProxy
import java.net.Authenticator; //导入依赖的package包/类
@Override public Credential authenticateProxy(
Proxy proxy, URL url, List<Challenge> challenges) throws IOException {
for (Challenge challenge : challenges) {
if (!"Basic".equalsIgnoreCase(challenge.getScheme())) {
continue;
}
InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address();
PasswordAuthentication auth = Authenticator.requestPasswordAuthentication(
proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(),
url.getProtocol(), challenge.getRealm(), challenge.getScheme(), url,
Authenticator.RequestorType.PROXY);
if (auth != null) {
return Credential.basic(auth.getUserName(), new String(auth.getPassword()));
}
}
return null;
}
示例2: testIsAuthenticationDialogSuppressed
import java.net.Authenticator; //导入依赖的package包/类
public void testIsAuthenticationDialogSuppressed() throws Exception {
final boolean[] suppressed = new boolean[1];
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
suppressed[0] = NetworkSettings.isAuthenticationDialogSuppressed();
return super.getPasswordAuthentication();
}
});
Callable<Void> callable = new Callable<Void>() {
@Override
public Void call() throws Exception {
Authenticator.requestPasswordAuthentication("wher.ev.er", Inet4Address.getByName("1.2.3.4"), 1234, "http", null, "http");
return null;
}
};
NetworkSettings.suppressAuthenticationDialog(callable);
assertTrue(suppressed[0]);
}
示例3: getSystemCreds
import java.net.Authenticator; //导入依赖的package包/类
private static PasswordAuthentication getSystemCreds(
final AuthScope authscope,
final Authenticator.RequestorType requestorType) {
final String hostname = authscope.getHost();
final int port = authscope.getPort();
final String protocol = port == 443 ? "https" : "http";
return Authenticator.requestPasswordAuthentication(
hostname,
null,
port,
protocol,
null,
translateScheme(authscope.getScheme()),
null,
requestorType);
}
示例4: privilegedRequestPasswordAuthentication
import java.net.Authenticator; //导入依赖的package包/类
private static PasswordAuthentication
privilegedRequestPasswordAuthentication(
final String host,
final InetAddress addr,
final int port,
final String protocol,
final String prompt,
final String scheme,
final URL url,
final RequestorType authType) {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<PasswordAuthentication>() {
public PasswordAuthentication run() {
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Requesting Authentication: host =" + host + " url = " + url);
}
PasswordAuthentication pass = Authenticator.requestPasswordAuthentication(
host, addr, port, protocol,
prompt, scheme, url, authType);
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Authentication returned: " + (pass != null ? pass.toString() : "null"));
}
return pass;
}
});
}
示例5: getAnswer
import java.net.Authenticator; //导入依赖的package包/类
private void getAnswer() {
if (!answered) {
answered = true;
PasswordAuthentication passAuth =
Authenticator.requestPasswordAuthentication(
hci.host, hci.addr, hci.port, hci.protocol,
hci.prompt, hci.scheme, hci.url, hci.authType);
/**
* To be compatible with existing callback handler implementations,
* when the underlying Authenticator is canceled, username and
* password are assigned null. No exception is thrown.
*/
if (passAuth != null) {
username = passAuth.getUserName();
password = passAuth.getPassword();
}
}
}
示例6: HttpCallerInfo
import java.net.Authenticator; //导入依赖的package包/类
/**
* Constructor an un-schemed object for site access.
*/
public HttpCallerInfo(URL url, Authenticator a) {
this.url= url;
prompt = "";
host = url.getHost();
int p = url.getPort();
if (p == -1) {
port = url.getDefaultPort();
} else {
port = p;
}
InetAddress ia;
try {
ia = InetAddress.getByName(url.getHost());
} catch (Exception e) {
ia = null;
}
addr = ia;
protocol = url.getProtocol();
authType = RequestorType.SERVER;
scheme = "";
authenticator = a;
}
示例7: getAnswer
import java.net.Authenticator; //导入依赖的package包/类
private void getAnswer() {
if (!answered) {
answered = true;
PasswordAuthentication passAuth =
Authenticator.requestPasswordAuthentication(
hci.authenticator,
hci.host, hci.addr, hci.port, hci.protocol,
hci.prompt, hci.scheme, hci.url, hci.authType);
/**
* To be compatible with existing callback handler implementations,
* when the underlying Authenticator is canceled, username and
* password are assigned null. No exception is thrown.
*/
if (passAuth != null) {
username = passAuth.getUserName();
password = passAuth.getPassword();
}
}
}
示例8: main
import java.net.Authenticator; //导入依赖的package包/类
public static void main (String args[]) throws Exception {
Authenticator defaultAuth = Authenticator.getDefault();
if (defaultAuth != null) {
throw new RuntimeException("Unexpected authenticator: null expected");
}
MyAuthenticator auth = new MyAuthenticator();
Authenticator.setDefault(auth);
defaultAuth = Authenticator.getDefault();
if (defaultAuth != auth) {
throw new RuntimeException("Unexpected authenticator: auth expected");
}
System.setSecurityManager(new SecurityManager());
try {
defaultAuth = Authenticator.getDefault();
throw new RuntimeException("Expected security exception not raised");
} catch (AccessControlException s) {
System.out.println("Got expected exception: " + s);
if (!s.getPermission().equals(new NetPermission("requestPasswordAuthentication"))) {
throw new RuntimeException("Unexpected permission check: " + s.getPermission());
}
}
System.out.println("Test passed with default authenticator "
+ defaultAuth);
}
示例9: setupProxy
import java.net.Authenticator; //导入依赖的package包/类
private static ProxyTunnelServer setupProxy() throws IOException {
ProxyTunnelServer pserver = new ProxyTunnelServer();
/*
* register a system wide authenticator and setup the proxy for
* authentication
*/
Authenticator.setDefault(new TestAuthenticator());
// register with the username and password
pserver.needUserAuth(true);
pserver.setUserAuth("Test", "test123");
pserver.start();
return pserver;
}
示例10: runWithAuthenticator
import java.net.Authenticator; //导入依赖的package包/类
@Override
public void
runWithAuthenticator(
Authenticator authenticator,
Runnable target )
{
try{
Authenticator.setDefault( authenticator );
target.run();
}finally{
SESecurityManager.installAuthenticator();
}
}
示例11: GitAuth
import java.net.Authenticator; //导入依赖的package包/类
/**
* Constructor.
*
* @param oldAuth The wrapped authenticator.
*
* @throws NoSuchFieldException Unable to access wrapped authenticator data.
*/
private GitAuth(Authenticator oldAuth) throws NoSuchFieldException {
this.oldAuth = oldAuth;
// Getting the fields with java reflection
requestingHost = Authenticator.class.getDeclaredField("requestingHost");
requestingSite = Authenticator.class.getDeclaredField("requestingSite");
requestingPort = Authenticator.class.getDeclaredField("requestingPort");
requestingProtocol = Authenticator.class.getDeclaredField("requestingProtocol");
requestingPrompt = Authenticator.class.getDeclaredField("requestingPrompt");
requestingScheme = Authenticator.class.getDeclaredField("requestingScheme");
requestingURL = Authenticator.class.getDeclaredField("requestingURL");
requestingAuthType = Authenticator.class.getDeclaredField("requestingAuthType");
// Making the fields accessible
requestingHost.setAccessible(true);
requestingSite.setAccessible(true);
requestingPort.setAccessible(true);
requestingProtocol.setAccessible(true);
requestingPrompt.setAccessible(true);
requestingScheme.setAccessible(true);
requestingURL.setAccessible(true);
requestingAuthType.setAccessible(true);
}
示例12: install
import java.net.Authenticator; //导入依赖的package包/类
/**
* Installs my custom authenticator. Gets the current authenticator by
* reflection and compares it with the previous one. If they are not the same
* instance, then my authenticator will be installed. If not, then it means
* that my authenticator is already installed
*/
public static void install() {
final Authenticator[] currentAuth = new Authenticator[1];
try {
Field declaredField = Authenticator.class.getDeclaredField("theAuthenticator");
declaredField.setAccessible(true);
currentAuth[0] = (Authenticator) declaredField.get(null);
if (currentAuth[0] == null || currentAuth[0] != installedAuthenticator) {
GitAuth oldInstalledAuth = installedAuthenticator;
installedAuthenticator = new GitAuth(currentAuth[0]);
installedAuthenticator.setBoundHosts(oldInstalledAuth.getBoundHosts());
Authenticator.setDefault(installedAuthenticator);
}
} catch (Throwable e) {
if (logger.isDebugEnabled()) {
logger.debug(e, e);
}
}
}
示例13: testUserPassword
import java.net.Authenticator; //导入依赖的package包/类
@Test
public void testUserPassword() throws Exception {
startServer(buildUserPasswordConfig());
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("oryx", "pass".toCharArray());
}
});
try {
String response = Resources.toString(
new URL("http://localhost:" + getHTTPPort() + "/helloWorld"),
StandardCharsets.UTF_8);
assertEquals("Hello, World", response);
} finally {
Authenticator.setDefault(null);
}
}
示例14: privilegedRequestPasswordAuthentication
import java.net.Authenticator; //导入依赖的package包/类
private static PasswordAuthentication
privilegedRequestPasswordAuthentication(
final String host,
final InetAddress addr,
final int port,
final String protocol,
final String prompt,
final String scheme,
final URL url,
final RequestorType authType) {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<>() {
public PasswordAuthentication run() {
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Requesting Authentication: host =" + host + " url = " + url);
}
PasswordAuthentication pass = Authenticator.requestPasswordAuthentication(
host, addr, port, protocol,
prompt, scheme, url, authType);
if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
logger.finest("Authentication returned: " + (pass != null ? pass.toString() : "null"));
}
return pass;
}
});
}
示例15: authenticate
import java.net.Authenticator; //导入依赖的package包/类
@Test public void authenticate() throws Exception {
server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_UNAUTHORIZED)
.addHeader("www-authenticate: Basic realm=\"protected area\"")
.setBody("Please authenticate."));
server.enqueue(new MockResponse().setBody("Successful auth!"));
Authenticator.setDefault(new RecordingAuthenticator());
urlFactory.setClient(urlFactory.client().newBuilder()
.authenticator(new JavaNetAuthenticator())
.build());
connection = urlFactory.open(server.url("/").url());
assertEquals("Successful auth!", readAscii(connection.getInputStream(), Integer.MAX_VALUE));
RecordedRequest denied = server.takeRequest();
assertNull(denied.getHeader("Authorization"));
RecordedRequest accepted = server.takeRequest();
assertEquals("GET / HTTP/1.1", accepted.getRequestLine());
assertEquals("Basic " + RecordingAuthenticator.BASE_64_CREDENTIALS,
accepted.getHeader("Authorization"));
}