Read parent domain cookies in java - 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.

Related

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

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?

Add another cookie when session is created [duplicate]

This question already has an answer here:
Fill a form with saved cookies
(1 answer)
Closed 5 years ago.
In my web application developed in struts 2 (javax.servlet v 2.5 and an embedded tomcat lib), I have the JSESSIONID always automatically added to the cookies after the first request when a HttpSession is created (at first call to request.getSession()), I know that the JSESSIONID aims to identify the created http session.
Now I want to add more cookies but I don't found how to adding this cookie in the framework struts 2? and how can I remove it from the cookie of the response when user logged out.
Add cookieName Cookie:
Cookie newCookie = new Cookie("cookieName", "cookieValue");
newCookie.setMaxAge(60*60*24*365); //Store cookie for 1 year
response.addCookie(newCookie);
and then remove it:
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("cookieName")) {
cookie.setValue("");
cookie.setPath("/");
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
}
}

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.

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.

How to use get/set cookies in GAE/java

hi I'm trying to get all cookies of the browser, like I did in my java projects.
javax.servlet.http.Cookie
String value = "";
Cookie cookie;
Cookie[] allcookies = request.getCookies();
for(int i=0;i<allscookies.length;i++){
cookie = allcookies[i];
if(cookie.getDomain().equals("mydomain") && cookie.getName().equals("cookiename")){
value = cookie.getValue();
}
}
but does not work on my Google App Engine project and I get this error
HTTP ERROR 500
Caused by:
java.lang.NullPointerException
Anyone knows any other way.Also try with this library but can not find how to use it no where com.google.appengine.repackaged.org.json.Cookie
It's not about GAE.
Cookie[] allcookies = request.getCookies();
May be null in any environment - it depends on whether the browser sent any cookies for the URL you're calling. Presumably your browser always had some cookies for the test URL you've used before deploying to GAE, and no cookies for the GAE URL.
Simply add an if (allcookies != null) { ... } around the loop.

Categories

Resources