【问题标题】:Cookie not getting set in browser via spring rest redirect api in httpserveletrresponse in client browserCookie 未通过客户端浏览器中 httpserveletrresponse 中的 spring rest 重定向 api 在浏览器中设置
【发布时间】:2017-08-25 15:36:29
【问题描述】:

我试图在客户端浏览器中设置 cookie,同时通过指定主页的 URI 从我的 Spring rest api 控制器重定向到应用主页(托管在其他地方)。 但似乎 cookie 出现在响应标头中,但未在 cookie 数据库中设置。

这里是域和路径的值;

domain = localhost
path = /
isSecure = false/true based on env.

我已经尝试了很多方法来使它工作,其中很少有人在下面;

  1. domain = localhost:8080 [作为我在 8080 端口上运行的 ui 代码]
  2. 域=:8080
  3. domain = xyz.com [我在我的主机文件中提到了一个条目 127.0.0.1:8080 xyz.com

任何人请帮助,它被卡住了很长一段时间。

@RequestMapping(value = "/login", method = RequestMethod.GET)

public ResponseEntity<?> ssoLoginAndFetchUserInfo(@RequestParam(value = "code", required = true) String code,
        @RequestParam(value = "state", required = true) String state, HttpServletResponse response) {
    try {
      normalLog.info("sso/login api invoked with code {} and state {}", code, state);
    final SSOUserInfoHostInfoWrapper info = ssoServices.ssoFetchUserInformation(code, state);
    normalLog.info("info fetched {}", info);

    response.addCookie(CommonUtil.createCookie(SSOContants.UserInfoConstants.IDENTITY_TOKEN,
        info.getUserInfo().getTokenInfo().getId_token(), info.getHostInfo().getHostname(),
        info.getUserInfo().getTokenInfo().getExpires_in(), IDENTITY_COOKIE_NAME, "/",
        info.getHostInfo().isSecure()));

    response.addCookie(
        CommonUtil.createCookie(SSOContants.UserInfoConstants.USER_NAME, info.getUserInfo().getUserName(),
            info.getHostInfo().getHostname(), info.getUserInfo().getTokenInfo().getExpires_in(),
            USERNAME_COOKIE_NAME, "/", info.getHostInfo().isSecure()));

    response.addCookie(
        CommonUtil.createCookie(SSOContants.UserInfoConstants.USER_ID, info.getUserInfo().getUserId(),
            info.getHostInfo().getHostname(), info.getUserInfo().getTokenInfo().getExpires_in(),
            USERNAME_COOKIE_ID, "/", info.getHostInfo().isSecure()));

    response.addCookie(
        CommonUtil.createCookie("authentication_token", "sdfsdfsdf",
            info.getHostInfo().getHostname(), info.getUserInfo().getTokenInfo().getExpires_in(),
            "authentication_token", "/", info.getHostInfo().isSecure()));

    // Redirect to app login page
    response.setHeader("Location", info.getHostInfo().getAppHomePageURI());
    return new ResponseEntity<>(HttpStatus.FOUND);

    } catch (Exception e) {
        return super.returnSpringError(e);
    }
}

实用方法

public static Cookie createCookie(final String name, final String value, final String hostname, final int expiresIn,
        final String comment, final String validToPath, final boolean isSecure) {
    Cookie c = new Cookie(name, value);
    c.setPath(validToPath);
    c.setDomain(hostname);
    c.setVersion(1);
    c.setComment(comment);
    c.setMaxAge(expiresIn);
    c.setSecure(isSecure);

    return c;

}

很少有关于堆积的截图;

【问题讨论】:

  • 你为什么要混合 Jersey 和 Spring MVC 注释......从修复它开始。
  • 嗯,其实已经修好了。

标签: java spring rest servlets cookies


【解决方案1】:

问题已解决。从第一天起,我就怀疑这一切都是因为“域”。 还不知道为什么将“localhost”放在域中不起作用,可能是 DNS 没有得到解析。

这是我如何解决的; 我在 /etc/hosts 文件中输入了以下条目 127.0.0.1 xx.yy.zz-r.com

然后使用域作为“.zz-r.com”并通过以下方式访问所有ui页面 xx.yy.zz-r.com:8080/----------

它奏效了。

【讨论】:

    【解决方案2】:

    遇到了同样的问题。我的案例是使用附加的 cookie 将重定向从后端传递到前端。在一个主域内。代码没有成功

    cookie.path = "/"
    cookie.domain = "domain.com"
    cookie.maxAge = 60
    cookie.isHttpOnly = false
    response.addCookie(cookie)
    

    但是当我更改为时一切都会运行

    response.setHeader("Set-Cookie", "customCookie=value; Path=/; Max-Age=60; Domain=domain.com")
    

    【讨论】:

      猜你喜欢
      • 2018-10-26
      • 1970-01-01
      • 2020-07-21
      • 2021-09-30
      • 2019-07-25
      • 2023-03-16
      • 2021-04-23
      • 1970-01-01
      相关资源
      最近更新 更多