How to access parent domain cookies from application running at subdomain? - java

I have Servlet web application running at subdomain can access other cookies but other cookies set by sso application into parent domain level is not access. But I can see those cookies are there in the browser.
In the servlet application, I have the code to read cookies:
httpRequest = (HttpServletRequest)request;
Cookie[] cookies = httpRequest.getCookies();
if(cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("SESSION_ID")) accessToken = cookie.getValue();
}
}
The above code only can retrive the cookies set by subdomain application. If I checked at browser cookies list I can see the "SESSION_ID" cookie and it's value with primary domain.
Cookies looks like this:
Name Value Domain
SESSION_ID 123 .primarydomain.com
XYZ 001 subdomain.primarydomain.com
ABC 2233
In the above cookies list I can only access XYZ and ABC but not the value of SESSION_ID. Am I missing some basic thing here?

Related

Get cookies set for subdomain

so I am using Jsoup and I am trying to get the cookies set for the site subdomain. The Connection.Response#cookies only returns the one set for that domain. I would like to grab the cookies so that I can use them to request data from the subdomain.
Example:
Connection.Response res = Jsoup.connect("https://www.tvplayer.com/watch/dave").userAgent("Mozilla/5.0").timeout(10000).method(Connection.Method.GET).execute();
Document doc = Jsoup.connect("https://live.tvplayer.com/").timeout(10000).cookies(res.cookies()).get();
If you request it throws a 403 due to the cookie not being set, this is because the cookies for the response doesn't have the ones from the subdomain live. just the main domain.
Outputting the cookies gives the cookies AWSELB and PHPSESSID only.

Fetching cookie

I have a site www.mysite.com. This site is called from another site www.aaa.com where I stamp the cookie (say x = 1) from the server.
Now when my site is called from new site www.new.com, will I be able to fetch the cookie (x=1) that I had stamped from my server when my server was called from www.aaa.com ?
NOTE: All the above processes take place in the same browser.
I am trying to do access the cookie but not been able to do so. I am using vertx as the server.
Small snippet to fetch the cookies in the server:
public Set getAllCookiesName(RoutingContext context) {
Set<Cookie> cookies = context.cookies();
Set<String> cookieNames = new HashSet<String>();
for (Cookie cookie: cookies) {
cookieNames.add(cookie.getName());
}
return cookieNames;
}
Cookies are not shared across different domains, however they can be shared between domains and their subdomains. For e.g.if your mysite is subdomain of another site www.aaa.com (mysite.aaa.com) then cookies could be shared. However it would depend on what path you set your cookie into.

Read parent domain cookies in java

I want to read parent domain(domain.com) cookies
Running the code from xyz.domain.com
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
//only subdomain cookies(i.e. xyz.domain.com) are displayed
}
}
Here I am not getting domain.com cookies
Similar to the below link
Access parent domain cookies from an application running on a subdomain
Update:
Cannot edit the cookies .i.e. cannot set domain of the cookie
When cookie is created in domain.com, you need to set domain as cookie.setDomain("."), Then cookie will be available in sub domain.

How to read persistent cookies stored by browser in java web start application

I have a java web start application.
Before launching the application web page stores a persistent cookie.
This is have the cookie has created: (.asp page):
Response.Cookies("MyApp")("Test") = "SomeValue"
Response.Cookies("Myapp").Expires = DateAdd("yyyy", 1, Now())
I can see the cookie inside chrome.
And this is how i tried to read the cookies:
CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
CookieStore cookieJar = manager.getCookieStore();
List<HttpCookie> cookies = cookieJar.getCookies();
for (HttpCookie cookie : cookies) {
System.out.println("CookieHandler retrieved cookie: "
+ cookie);
}
How can i read that cookie in my application?
Have you taken a look at this ? It has an example of Accessing Cookies from a java web start application.

Access parent domain cookies from an application running on a subdomain

I have a cookie set in the path of the parent domain ( which I have no control over). But I have an application running in one of the subdomains (I have access to this). How can I access the cookies set against the parent domain?
For instance, say I have the cookies:
Name Value Domain (not https)
ABC 1 .example.com
XYZ 0 foo.bar.example.com
The app is running on foo.bar.example.com and the cookie is set at .example.com
It's a Java application. I tried to debug but I can only see the cookies set for the subdomain, not the primary domain.
Here's the source
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
//Here I only see cookies set for the sub domain but not the parent domain. The cookies collection has no parent domain cookies.
}
}
Also according to this stackoverflow answer maybe the browser is not sending the parent domain cookies to the app?
I feel like I'm missing something elementary here. Any suggestions?
The problem in my case was an external Proxy server (SSO server in my case) was filtering the cookies. So it was an environmental problem. I'm sure that parent domain cookies are available in normal environments.

Categories

Resources