java - Cookie 未通过客户端浏览器中的 httpserveletrresponse 中的 Spring Rest 重定向 api 在浏览器中设置

标签 java spring rest servlets cookies

我试图在客户端浏览器中设置 cookie,同时通过指定主页的 URI 从 Spring Rest api Controller 重定向到应用程序主页(托管在其他地方)。 但 cookie 似乎出现在响应 header 中,但没有在 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;

}

一些关于堆放内容的屏幕截图; Cookie database

最佳答案

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

这是我如何解决它的; 我在/etc/hosts 文件中使用以下条目创建了一个条目 127.0.0.1 xx.yy.zz-r.com

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

并且成功了。

关于java - Cookie 未通过客户端浏览器中的 httpserveletrresponse 中的 Spring Rest 重定向 api 在浏览器中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43139408/

相关文章:

java - Lombok注解没有效果

java - 如何使用 Vaadin 上传图像 Java Spring 应用程序

java - Spring ClassPathXmlApplicationContext 不断刷新

java - 在 Weblogic 12.1.3 上部署 Jersey 应用程序

java - 我想使用 java.....任何 api 监听 USB 端口上的数据?

java - 使两个线程继续在后台运行

ruby-on-rails - RESTful 应用程序中的 XSRF

node.js - Express REST API 不返回整数数组

java - Swagger includePatterns()

java - 如何在 SwaggerUI 中显示父项目的端点