我试图在客户端浏览器中设置 cookie,同时通过指定主页的 URI 从 Spring Rest api Controller 重定向到应用程序主页(托管在其他地方)。 但 cookie 似乎出现在响应 header 中,但没有在 cookie 数据库中设置。
这是域和路径的值;
domain = localhost
path = /
isSecure = false/true based on env.
我尝试了很多方法来使其发挥作用,其中很少有在下面;
- domain = localhost:8080 [作为我在 8080 端口上运行的 ui 代码]
- 域名 =
:8080 - 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;
}
一些关于堆放内容的屏幕截图;
最佳答案
问题已解决。从第一天起,我就怀疑这一切都是因为“域名”。 还不知道为什么将“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/