Java 未通过客户端浏览器中HttpServeRetrResponse中的spring rest重定向api在浏览器中设置Cookie
我试图在客户端浏览器中设置cookie,同时通过指定主页的URI从我的SpringRESTAPI控制器重定向到应用程序主页(托管在其他地方)。 但cookie似乎出现在响应头中,而不是在cookie数据库中设置 这里是域和路径的值Java 未通过客户端浏览器中HttpServeRetrResponse中的spring rest重定向api在浏览器中设置Cookie,java,spring,rest,servlets,cookies,Java,Spring,Rest,Servlets,Cookies,我试图在客户端浏览器中设置cookie,同时通过指定主页的URI从我的SpringRESTAPI控制器重定向到应用程序主页(托管在其他地方)。 但cookie似乎出现在响应头中,而不是在cookie数据库中设置 这里是域和路径的值 domain = localhost path = / isSecure = false/true based on env. 我已经尝试了很多方法让它发挥作用,但很少有人能做到这一点 domain=localhost:8080[作为我在8080端口上运行的ui代码
domain = localhost
path = /
isSecure = false/true based on env.
我已经尝试了很多方法让它发挥作用,但很少有人能做到这一点
@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);
}
}
很少有什么是堆截图;
问题已经解决。从第一天起,我就怀疑这一切都是因为“域名”。 还不知道为什么在域中放置“localhost”不起作用,可能是DNS没有得到解决 我是如何解决的; 我在/etc/hosts文件中输入了以下条目 127.0.0.1 xx.yy.zz-r.com 然后将域用作“.zz-r.com”,并通过 xx.yy.zz-r.com:8080/----------
它成功了。为什么要混合Jersey和Spring MVC注释…从修复开始。是的,实际上已经修复了。
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;
}