【发布时间】:2017-08-25 15:36:29
【问题描述】:
我试图在客户端浏览器中设置 cookie,同时通过指定主页的 URI 从我的 Spring rest api 控制器重定向到应用主页(托管在其他地方)。 但似乎 cookie 出现在响应标头中,但未在 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;
}
很少有关于堆积的截图;
【问题讨论】:
-
你为什么要混合 Jersey 和 Spring MVC 注释......从修复它开始。
-
嗯,其实已经修好了。
标签: java spring rest servlets cookies