Last active
August 29, 2015 14:03
-
-
Save mnadeem/4df563fb11fa810a203f to your computer and use it in GitHub Desktop.
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
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.security.KeyManagementException; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.SecureRandom; | |
import java.util.List; | |
import javax.net.ssl.SSLContext; | |
import org.apache.http.Header; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpUriRequest; | |
import org.apache.http.client.methods.RequestBuilder; | |
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |
import org.apache.http.conn.ssl.SSLContexts; | |
import org.apache.http.conn.ssl.TrustSelfSignedStrategy; | |
import org.apache.http.cookie.Cookie; | |
import org.apache.http.impl.client.BasicCookieStore; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.impl.client.LaxRedirectStrategy; | |
import org.apache.http.util.EntityUtils; | |
//http://www.codeproject.com/Articles/80314/How-to-Connect-to-a-SiteMinder-Protected-Resource | |
public class AccessSiteminderProtectedResource { | |
private static final String PASSWORD = "pwd"; | |
private static final String USER_NAME = "userId"; | |
private static final String SITEMINDER_PROTECTED_RESOURCE = "protectedResource"; | |
private static final String SITEMINDER_LOGIN_URL = "siteMinderLoginUrl?TARGET=-SM-" + SITEMINDER_PROTECTED_RESOURCE; | |
public static void main(String[] args) throws Exception { | |
BasicCookieStore cookieStore = new BasicCookieStore(); | |
SSLContext sslcontext = buildSSLContext(); | |
SSLConnectionSocketFactory sslsf = buildSSLConnectionSocketFactory(sslcontext); | |
CloseableHttpClient httpclient = buildHttpClient(cookieStore, sslsf); | |
try { | |
String nextLocation = executeLogin(cookieStore, httpclient); | |
accessApp(httpclient, nextLocation); | |
} finally { | |
httpclient.close(); | |
} | |
} | |
private static SSLContext buildSSLContext() | |
throws NoSuchAlgorithmException, KeyManagementException, | |
KeyStoreException { | |
SSLContext sslcontext = SSLContexts.custom() | |
.setSecureRandom(new SecureRandom()) | |
.loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(); | |
return sslcontext; | |
} | |
private static SSLConnectionSocketFactory buildSSLConnectionSocketFactory( | |
SSLContext sslcontext) { | |
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( | |
sslcontext, | |
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); | |
return sslsf; | |
} | |
private static CloseableHttpClient buildHttpClient( | |
BasicCookieStore cookieStore, SSLConnectionSocketFactory sslsf) { | |
CloseableHttpClient httpclient = HttpClients.custom() | |
.setSSLSocketFactory(sslsf).setDefaultCookieStore(cookieStore) | |
.setRedirectStrategy(new LaxRedirectStrategy()) | |
.build(); | |
return httpclient; | |
} | |
private static String executeLogin(BasicCookieStore cookieStore, | |
CloseableHttpClient httpclient) throws URISyntaxException, | |
IOException, ClientProtocolException { | |
HttpUriRequest loginPost = RequestBuilder | |
.post() | |
.setUri(new URI(SITEMINDER_LOGIN_URL)) | |
.addParameter("USER", USER_NAME) | |
.addParameter("PASSWORD", PASSWORD).build(); | |
System.out.println("executing request" + loginPost.getRequestLine() + "\n"); | |
CloseableHttpResponse loginResponse = httpclient.execute(loginPost); | |
String nexLocation; | |
try { | |
HttpEntity loginResponseEntity = loginResponse.getEntity(); | |
System.out.println("Login form post Status: " + loginResponse.getStatusLine()); | |
EntityUtils.consume(loginResponseEntity); | |
System.out.println(); | |
System.out.println("Post logon cookies:"); | |
System.out.println(); | |
displayCookies(cookieStore); | |
System.out.println(); | |
System.out.println(); | |
System.out.println("Login Post Headers----------------------------------------"); | |
displayHeaders(loginResponse); | |
System.out.println(); | |
System.out.println(); | |
nexLocation = SITEMINDER_PROTECTED_RESOURCE; | |
} finally { | |
loginResponse.close(); | |
} | |
return nexLocation; | |
} | |
private static void accessApp(CloseableHttpClient httpclient, String nextLocation) throws IOException, ClientProtocolException { | |
HttpGet appGet = new HttpGet(nextLocation); | |
System.out.println("executing request" + appGet.getRequestLine()); | |
CloseableHttpResponse response = httpclient.execute(appGet); | |
try { | |
HttpEntity entity = response.getEntity(); | |
System.out.println("\n\n\n\n---------------------------------------- \n"); | |
System.out.println("App Get Status: " + response.getStatusLine()); | |
System.out.println(EntityUtils.toString(entity)); | |
EntityUtils.consume(entity); | |
} finally { | |
response.close(); | |
} | |
} | |
private static void displayHeaders(CloseableHttpResponse loginResponse) { | |
for (Header header : loginResponse.getAllHeaders()) { | |
System.out.println(header); | |
} | |
} | |
private static void displayCookies(BasicCookieStore cookieStore) { | |
List<Cookie> cookies = cookieStore.getCookies(); | |
if (cookies.isEmpty()) { | |
System.out.println("None"); | |
} else { | |
for (int i = 0; i < cookies.size(); i++) { | |
System.out.println("- " + cookies.get(i).toString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment