1. 通过$.getScript跨域获得cookies
前端代码
通过页面加载后调用后台接口获得对应服务下的cookies,返回来的必须是js对象。
$.getScript('$siteDomain/ssoApi?'+Math.random(),function(){
console.log(" username:" + userCookie.uname);
})
后台代码
@ResponseBody
@RequestMapping("/home/ssoApi")
public void getCookies(HttpServletRequest request,HttpServletResponse response){
String uid = CookieHelper.getCookieVal(request, "uid");
String uname=CookieHelper.getCookieVal(request,"uname");
PrintWriter out = null;
try {
String userCookie="var userCookie={uid:"+uid+","+"uname:"+"'"+uname+"'"+"}";
out = response.getWriter();
out.println(userCookie);
} catch (IOException e) {
e.printStackTrace();
}
}
通过以上例子就可以实现js跨域获得cookies实现显示用户的登陆状态,和其他功能。