use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.
the class TestCookieReuse method testAutoEndSession.
/**
* Verify that we canNOT RE-use the cookie for REST session if the session is valid
* @throws HttpException
*/
@Test
public void testAutoEndSession() throws ServiceException, IOException, HttpException {
// establish legitimate connection
TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraForceClearCookies, "TRUE");
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
URI uri = mbox.getRestURI("Inbox?fmt=rss");
HttpClientContext context = HttpClientContext.create();
HttpClient alice = mbox.getHttpClient(uri);
// create evesdropper's connection
HttpClientBuilder eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
List<Cookie> cookies = context.getCookieStore().getCookies();
BasicCookieStore cookieStore = new BasicCookieStore();
for (Cookie cookie : cookies) {
BasicClientCookie basicCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
basicCookie.setDomain(uri.getHost());
basicCookie.setPath("/");
basicCookie.setSecure(false);
cookieStore.addCookie(cookie);
}
eve.setDefaultCookieStore(cookieStore);
Account a = TestUtil.getAccount(USER_NAME);
a.setForceClearCookies(true);
EndSessionRequest esr = new EndSessionRequest();
mbox.invokeJaxb(esr);
HttpGet get = new HttpGet(uri.toString());
HttpResponse response = HttpClientUtil.executeMethod(eve.build(), get, context);
int statusCode = response.getStatusLine().getStatusCode();
Assert.assertEquals("This request should not succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
use of org.apache.http.impl.cookie.BasicClientCookie in project zm-mailbox by Zimbra.
the class TestCookieReuse method testForceEndSession.
/**
* Verify that we canNOT RE-use the cookie taken from a legitimate HTTP session for a REST request
* after ending the original session
* @throws HttpException
*/
@Test
public void testForceEndSession() throws ServiceException, IOException, HttpException {
// establish legitimate connection
TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraForceClearCookies, "FALSE");
ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
URI uri = mbox.getRestURI("Inbox?fmt=rss");
HttpClient alice = mbox.getHttpClient(uri);
HttpClientContext context = HttpClientContext.create();
// create evesdropper's connection
HttpClientBuilder eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
List<Cookie> cookies = context.getCookieStore().getCookies();
BasicCookieStore cookieStore = new BasicCookieStore();
for (Cookie cookie : cookies) {
BasicClientCookie basicCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
basicCookie.setDomain(uri.getHost());
basicCookie.setPath("/");
basicCookie.setSecure(false);
cookieStore.addCookie(cookie);
}
eve.setDefaultCookieStore(cookieStore);
Account a = TestUtil.getAccount(USER_NAME);
a.setForceClearCookies(false);
EndSessionRequest esr = new EndSessionRequest();
esr.setLogOff(true);
mbox.invokeJaxb(esr);
HttpGet get = new HttpGet(uri.toString());
HttpResponse response = HttpClientUtil.executeMethod(eve.build(), get);
int statusCode = response.getStatusLine().getStatusCode();
Assert.assertEquals("This request should not succeed. Getting status code " + statusCode, HttpStatus.SC_UNAUTHORIZED, statusCode);
}
use of org.apache.http.impl.cookie.BasicClientCookie in project ddf by codice.
the class LogoutMessageImpl method sendSamlLogoutRequest.
@Override
public String sendSamlLogoutRequest(LogoutWrapper request, String targetUri, boolean isSoap, @Nullable Cookie cookie) throws IOException, LogoutSecurityException {
XMLObject xmlObject = isSoap ? SamlProtocol.createSoapMessage((SignableSAMLObject) request.getMessage()) : (XMLObject) request;
Element requestElement = getElementFromSaml(new LogoutWrapperImpl(xmlObject));
String requestMessage = DOM2Writer.nodeToString(requestElement);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost post = new HttpPost(targetUri);
post.addHeader("Cache-Control", "no-cache, no-store");
post.addHeader("Pragma", "no-cache");
post.addHeader("SOAPAction", SAML_SOAP_ACTION);
post.addHeader("Content-Type", "application/soap+xml");
post.setEntity(new StringEntity(requestMessage, "utf-8"));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
BasicHttpContext context = new BasicHttpContext();
if (cookie != null) {
BasicClientCookie basicClientCookie = new BasicClientCookie(cookie.getName(), cookie.getValue());
basicClientCookie.setDomain(cookie.getDomain());
basicClientCookie.setPath(cookie.getPath());
BasicCookieStore cookieStore = new BasicCookieStore();
cookieStore.addCookie(basicClientCookie);
context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}
return httpClient.execute(post, responseHandler, context);
}
}
use of org.apache.http.impl.cookie.BasicClientCookie in project api-manager by cehome-com.
the class HttpUtils method setAuthInfo.
private void setAuthInfo(HttpResponse httpResponse, String domain) {
Header firstHeader = httpResponse.getFirstHeader("Set-Cookie");
if (firstHeader != null) {
String setCookie = firstHeader.getValue();
String JSESSIONID = setCookie.substring("JSESSIONID=".length(), setCookie.indexOf(";"));
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", JSESSIONID);
cookie.setDomain(domain);
cookieStore.addCookie(cookie);
} else {
try {
String result = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
JSONObject resultObject = JSON.parseObject(result);
String code = resultObject.getString("code");
if (!StringUtils.isEmpty(code) && code.equals("0")) {
String token = resultObject.getJSONObject("data").getString("token");
tokenPool.put(domain, token);
}
} catch (IOException e) {
logger.error("sendRequest error!", e);
}
}
}
use of org.apache.http.impl.cookie.BasicClientCookie in project warn-report by saaavsaaa.
the class WebRequestClient method testPostWithCookie.
public static String testPostWithCookie(String url, String cookieKey, String cookieValue, HttpEntity paras) throws Exception {
/*HttpContext localContext = new BasicHttpContext();
// 在本地上下问中绑定一个本地存储
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cs);
cs.addCookie(new BasicClientCookie(cookieKey, cookieValue));*/
Cookie[] cookies = new Cookie[1];
cookies[0] = new BasicClientCookie(cookieKey, cookieValue);
setCookies(cookies);
return opera(url, paras, false);
}
Aggregations