简体   繁体   中英

Can not access cookies in spring jersey based application

I am working on spring security implementation module and i need to set and get few cookies. i have tried creating cookies by using ( javax.servlet.http.Cookie ) and ( javax.ws.rs.core.NewCookie ) both are working fine for setting cookies, i can see the cookies in the browser but when i am trying to access them, it does give me only JSESSIONID, i need to access other cookies also.

This is how i am setting the cookies, and in both ways i will save the cookies successfully on the browser:

Cookie cookieOne = new Cookie("SERVLETCOOKIE", "TESTING COOKIES");

NewCookie cookieTwo = new NewCookie("WSRSCOOKIE", "TESTING COOKIES");

when i try to access the cookies, i have tried both @Autowired and @Context as below, but i can only get JSESSIONID cookie.

@Autowired HttpServletRequest request;
and 
@Context HttpServletRequest request;

and i am trying to access the cookies as below :

Cookie[] cookieList = request.getCookies();

        if(cookieList !=null && cookieList.length >0){
            for(int i=0;i<cookieList.length;i++){
                Cookie cookie = cookieList[i];

                if(cookie.getName().equals("SERVLETCOOKIE")){
                    String value1 = cookie.getValue();
                    logger.info("cookie found. value ="+value1 );
                }
                if(cookie.getName().equals("WSRSCOOKIE")){
                        String value2 = cookie.getValue();
                        logger.info("cookie found. value ="+value2 );
                    }
                }
            }

It would be great if someone help me point out the way i can get all the other cookies.

The question is a few years old. However today I had a similar problem. The solution was: If you are using the Apache http connector then the jersey cookie api seems not to be supported. Try to get the cookies over the Apache api:

public static Cookie getCookie(final ClientRequestContext clientRequestContext, final String key){
    final CookieStore cookieStore = ApacheConnectorProvider.getCookieStore(clientRequestContext.getClient());
    return cookieStore.getCookies().stream().filter(c->c.getName().equals(key)).collect(CollectorUtil.singletonOrNullCollector());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM