首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在服务器端创建的angular js上无法获取cookie (Jersey)

在服务器端创建的angular js上无法获取cookie (Jersey)
EN

Stack Overflow用户
提问于 2015-10-06 15:00:35
回答 1查看 260关注 0票数 0

我有一个cookie,它是在服务器端创建的(Rest服务)。下面是创建cookie的rest服务:

代码语言:javascript
复制
        @Path("token")
        public class AuthService {

        @POST
        @Produces(MediaType.APPLICATION_JSON)
        public Response generateToken(@Context HttpServletRequest httpServletRequest) {

        //some code to get serializedJwt and xsrftoken. path is "/" and domain is "http://localhost"

        Cookie jwtCookie = new Cookie("jwt", serializedJwt, path, domain);
        Cookie xsrfCookie = new Cookie("X-XSRF-TOKEN", xsrfToken, path, domain);

        NewCookie newJwtCookie = new NewCookie(jwtCookie, null, maxAge, false);
        NewCookie newXsrfCookie = new NewCookie(xsrfCookie, null, maxAge, false);

        return Response.status(SUCCESSFUL_REQUEST)
        .header(ERROR_HEADER_NAME, SUCCESS)
        .header("SET-COOKIE", newJwtCookie.toString()+" ; HttpOnly")
        .header("SET-COOKIE", newXsrfCookie.toString())
        .entity(MAPPER.writeValueAsString(responseBody)).build();
        }
        }

现在我正在尝试从angular js(v1.4.6)应用程序中检索这个cookie。

代码语言:javascript
复制
        console.log($cookies.get("X-XSRF-TOKEN")); //prints undefined
        $cookies.put('abc',"kishore"); //just for testing purpose
        console.log($cookies.get("abc")); //this prints kishore
        console.log(document.cookie); //this prints "abc=kishore"

注意:对于X-XSRF-TOKEN,httpOnly为false。

EN

回答 1

Stack Overflow用户

发布于 2015-10-09 18:46:13

删除domain=http://localhost后问题得到解决

代码语言:javascript
复制
Point from Stack overflow which helped:
•   By design domain names must have at least two dots otherwise browser will say they are invalid.
•   Explicit setting domain cookie on localhost doesn't work for chrome.
•   You can only set domain cookies for registry controlled domains, i.e. something ending in .com or so, but not IPs or intranet hostnames like localhost
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32963667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档