Created
July 6, 2021 17:29
-
-
Save mrcodetastic/4429333bde916e32159d67c7f41ffed9 to your computer and use it in GitHub Desktop.
Connect to a website using Apache HTTPClient5 using Windows Authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Session { | |
/* | |
* Use Apache HTTPClient5 WinClient extensions to automatically gain SSO login via. domain credentials of | |
* currently logged in user. | |
*/ | |
// Store SSO cookie's between httpclient and server to maintain session | |
private CookieStore cookieStore = new BasicCookieStore(); | |
// Base http configuration used for all http requests | |
private RequestConfig requestConfig = RequestConfig.custom() | |
.setConnectTimeout(300, TimeUnit.SECONDS) | |
.setResponseTimeout(600, TimeUnit.SECONDS) | |
.setConnectionRequestTimeout(300, TimeUnit.SECONDS) | |
//.setProxy(httpsProxy) | |
.setRedirectsEnabled(true) | |
.setAuthenticationEnabled(true) // all I needed to do in the end to enable SSO!! | |
.setCircularRedirectsAllowed(true) | |
.build(); | |
// Single client with persistent cookie store. | |
private CloseableHttpClient httpclient = WinHttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultRequestConfig(requestConfig).build(); | |
private String url_base = "http://something.com/login/"; | |
// For logging | |
private static org.slf4j.Logger LOGGER; | |
public Session() | |
{ | |
} | |
public Boolean login() | |
{ | |
String httpOutput = ""; | |
/* | |
* Use Apache HTTPClient5 WinClient extensions to automatically gain SSO login via. domain credentials of currently logged in user. | |
*/ | |
if (!WinHttpClients.isWinAuthAvailable()) { | |
System.out.println("Integrated Win auth is not supported!!!"); | |
return Boolean.FALSE; | |
} | |
try | |
{ // HACK: ONE HUGE TRY STATEMENT OVER ALL STAGES | |
HttpGet httpget = new HttpGet(url_base + "login.php"); | |
httpget.setConfig(requestConfig); | |
System.out.println("Executing request: " + httpget.getRequestUri()); | |
HttpHost httpsTarget = new HttpHost("https", "something.com", 443); | |
CloseableHttpResponse response = httpclient.execute(httpsTarget, httpget); | |
try { | |
httpOutput = EntityUtils.toString(response.getEntity()); | |
System.out.println("Response: " + response.getCode() + " " + response.getReasonPhrase()); | |
if (httpOutput.contains("<SOMETHING OF INTEREST>")) { | |
System.out.println("Sucessfully managed to login."); | |
} | |
EntityUtils.consume(response.getEntity()); | |
response.close(); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} finally { | |
response.close(); | |
} | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
System.err.println("IO exception occurred during attempted login."); | |
e.printStackTrace(); | |
} catch (URISyntaxException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} finally { | |
//doLogout(); | |
System.out.println("Completed login()"); | |
} | |
} | |
} // Session |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment