Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 在SpringMVC中共享cookies_Spring Mvc_Cookies - Fatal编程技术网

Spring mvc 在SpringMVC中共享cookies

Spring mvc 在SpringMVC中共享cookies,spring-mvc,cookies,Spring Mvc,Cookies,我有一个在上运行的现有web应用程序 https://subdomain.example.com 现在我想有更多的子域 https://subdomain2.example.com 如何使用SpringMVC设置以下内容,以便在从第一个域重定向到第二个域后,不会再次提示用户进行身份验证 Set-Cookie: name=value; domain=example.com 看看这个控制器示例,但要记住两件事: 如果您连接到127.0.0.1,则在本地环境中工作时,放置任意固定域将不允许您访

我有一个在上运行的现有web应用程序

https://subdomain.example.com
现在我想有更多的子域

https://subdomain2.example.com
如何使用SpringMVC设置以下内容,以便在从第一个域重定向到第二个域后,不会再次提示用户进行身份验证

Set-Cookie: name=value; domain=example.com

看看这个控制器示例,但要记住两件事:

  • 如果您连接到127.0.0.1,则在本地环境中工作时,放置任意固定域将不允许您访问cookie
  • 您的cookie可以被该主机上的所有子域(example.com)读取,而不仅仅是您想要的子域
类别:

package com.test.foo;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/foo")
public class FooController {

    @RequestMapping("/cookie")
    public String setCookie(HttpServletRequest request, HttpServletResponse response)  {
        String value = "value";
        Cookie cookie = new Cookie("name", value);
        cookie.setPath("/");//<-- important
        cookie.setDomain("example.com");
        response.addCookie(cookie);
        return "foo/index";//your view
    }
}
package com.test.foo;
导入javax.servlet.http.Cookie;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.RequestMapping;
@控制器
@请求映射(“/foo”)
公共类FooController{
@请求映射(“/cookie”)
公共字符串setCookie(HttpServletRequest请求,HttpServletResponse响应){
String value=“value”;
Cookie Cookie=新Cookie(“名称”,值);

cookie.setPath(“/”;//当我这样做并重定向到子域时..cookie不会持久化!我应该怎么做???@manukyanv07 cookie在会话结束时过期默认情况下,使用cookie.setMaxAge(60*60*24*365*10);要保持itI,我不太确定,但您可以将这些配置放到
etc/hosts
文件中进行测试:
127.0.0.1 example.com
127.0.0.1 subdomain1.example.com
127.0.0.1 subdomain2.example.com
127.0.0.1 subdomain3.example.com