Cross-Domain Cookie-based single-point Logon

Source: Internet
Author: User
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.

 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.