public final class SpnegoHttpURLConnection extends Object
This mechanism is an alternative to HTTP Basic Authentication where the HTTP server does not support Basic Auth but instead has SPNEGO support
A krb5.conf and a login.conf is required when using this class. Take a look at the spnego.sourceforge.net documentation for an example krb5.conf file. Also, you must provide a keytab file, or a username and password, or allowtgtsessionkey.
Example usage (username/password):
public static void main(final String[] args) throws Exception {
System.setProperty("java.security.krb5.conf", "krb5.conf");
System.setProperty("sun.security.krb5.debug", "true");
System.setProperty("java.security.auth.login.config", "login.conf");
SpnegoHttpURLConnection spnego = null;
try {
spnego = new SpnegoHttpURLConnection("spnego-client", "dfelix", "myp@s5");
spnego.connect(new URL("http://medusa:8080/index.jsp"));
System.out.println(spnego.getResponseCode());
} finally {
if (null != spnego) {
spnego.disconnect();
}
}
}
Alternatively, if the server supports HTTP Basic Authentication, this Class is NOT needed and instead you can do something like the following:
public static void main(final String[] args) throws Exception {
final String creds = "dfelix:myp@s5";
final String token = Base64.encodeImpl(creds.getBytes());
URL url = new URL("http://medusa:8080/index.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty(Constants.AUTHZ_HEADER
, Constants.BASIC_HEADER + " " + token);
conn.connect();
System.out.println("Response Code:" + conn.getResponseCode());
}
To see a working example and instructions on how to use a keytab, take a look at the creating a client keytab example.
| Constructor and Description |
|---|
SpnegoHttpURLConnection(SpnegoClient spnegoClient) |
SpnegoHttpURLConnection(String loginModuleName)
Deprecated.
|
SpnegoHttpURLConnection(String loginModuleName,
String username,
String password)
Deprecated.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addRequestProperty(String key,
String value)
Adds an HTTP Request property.
|
HttpURLConnection |
connect(URL url)
Opens a communications link to the resource referenced by
this URL, if such a connection has not already been established.
|
HttpURLConnection |
connect(URL url,
ByteArrayOutputStream dooutput)
Opens a communications link to the resource referenced by
this URL, if such a connection has not already been established.
|
void |
disconnect()
Logout and clear request properties.
|
InputStream |
getErrorStream()
Returns an error stream that reads from this open connection.
|
String |
getHeaderField(int index)
Get header value at specified index.
|
String |
getHeaderField(String name)
Get header value by header name.
|
String |
getHeaderFieldKey(int index)
Get header field key at specified index.
|
InputStream |
getInputStream()
Returns an input stream that reads from this open connection.
|
OutputStream |
getOutputStream()
Returns an output stream that writes to this open connection.
|
int |
getResponseCode()
Returns HTTP Status code.
|
String |
getResponseMessage()
Returns HTTP Status message.
|
boolean |
isContextEstablished()
Returns true if GSSContext has been established.
|
void |
requestCredDeleg(boolean requestDelegation)
Request that this GSSCredential be allowed for delegation.
|
void |
setRequestMethod(String method)
May override the default GET method.
|
void |
setRequestProperty(String key,
String value)
Sets an HTTP Request property.
|
@Deprecated public SpnegoHttpURLConnection(String loginModuleName) throws LoginException
loginModuleName - loginModuleNameLoginException - LoginException@Deprecated public SpnegoHttpURLConnection(String loginModuleName, String username, String password) throws LoginException
loginModuleName - loginModuleNameusername - usernamepassword - passwordLoginException - LoginExceptionpublic SpnegoHttpURLConnection(SpnegoClient spnegoClient)
public HttpURLConnection connect(URL url) throws GSSException, PrivilegedActionException, IOException
This implementation simply calls this objects connect(URL, ByteArrayOutputStream) method but passing in a null for the second argument.
url - urlGSSException - GSSExceptionPrivilegedActionException - PrivilegedActionExceptionIOException - IOExceptionURLConnection.connect()public HttpURLConnection connect(URL url, ByteArrayOutputStream dooutput) throws GSSException, PrivilegedActionException, IOException
url - target URLdooutput - optional message/payload to send to serverGSSException - GSSExceptionPrivilegedActionException - PrivilegedActionExceptionIOException - IOExceptionURLConnection.connect()public void disconnect()
HttpURLConnection.disconnect()public boolean isContextEstablished()
public void addRequestProperty(String key, String value)
key - request property namevalue - request propery valueURLConnection.addRequestProperty(String, String)public void setRequestProperty(String key, String value)
key - request property namevalue - request property valueURLConnection.setRequestProperty(String, String)public InputStream getErrorStream() throws IOException
IOException - IOExceptionHttpURLConnection.getErrorStream()public String getHeaderField(int index)
index - indexpublic String getHeaderField(String name)
name - name headerURLConnection.getHeaderField(String)public String getHeaderFieldKey(int index)
index - indexpublic InputStream getInputStream() throws IOException
IOException - IOExceptionURLConnection.getInputStream()public OutputStream getOutputStream() throws IOException
IOException - IOExceptionURLConnection.getOutputStream()public int getResponseCode()
throws IOException
IOException - IOExceptionHttpURLConnection.getResponseCode()public String getResponseMessage() throws IOException
IOException - IOExceptionHttpURLConnection.getResponseMessage()public void requestCredDeleg(boolean requestDelegation)
requestDelegation - true to allow/request delegationpublic void setRequestMethod(String method)
method - methodHttpURLConnection.setRequestMethod(String)Copyright © 2021. All rights reserved.