Because the project requires single-point logon, the main idea is: System 1: user name and password --> write cookie --> other systems read the cookie.
1. Cookie sharing on the same server
@Component("userLoginAction")@Namespace("/userLogin")@ParentPackage("json-default")public class UserLoginAction extends ActionSupport{ @Action(value="saveCookie",results={ @Result(name=SUCCESS,location="/WEB-INF/page/success.ftl") }) public String saveCookie(){ Cookie cook=new Cookie("userName","lisi"); cook.setPath("/"); cook.setMaxAge(-1); ServletActionContext.getResponse().addCookie(cook); return SUCCESS; }}
Cook. setpath ("/"); set to the same webapp
Cook. setmaxage (-1); set to close the browser and clear the cookie.
2. Cross-Domain Cookie sharing. The cookie cross-domain is not cross-domain, but cross-domain.
Set the local domain name in the host file.
package cn.action;import javax.servlet.ServletContext;import javax.servlet.http.Cookie;import org.apache.struts2.ServletActionContext;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import org.springframework.stereotype.Component;import com.opensymphony.xwork2.ActionSupport;@Component("userLoginAction")@Namespace("/userLogin")@ParentPackage("json-default")public class UserLoginAction extends ActionSupport{ @Action(value="saveCookie",results={ @Result(name=SUCCESS,location="/WEB-INF/page/success.ftl") }) public String saveCookie(){ Cookie cook=new Cookie("userName","lisi"); cook.setPath("/"); cook.setMaxAge(-1); cook.setDomain(".demo.com"); ServletActionContext.getResponse().addCookie(cook); return SUCCESS; }}
Cook. setdomain (".demo.com"); shared subdomain .demo.com
OK. You can view the cookie in the browser.