前后端分离 | 关于登录状态那些事
Posted 慕课训练营
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了前后端分离 | 关于登录状态那些事相关的知识,希望对你有一定的参考价值。
同源策略
-
协议相同 -
域名相同 -
端口相同
http
,域名是
www.a.com
,端口是
80
。只要这3个相同,我们就可以在请求(Request)时带上Cookie,
同域下的前后端分离
@RequestMapping("setCookie")public String setCookie(HttpServletResponse response){
Cookie cookie = new Cookie("test","same");
cookie.setPath("/");
response.addCookie(cookie);
return "success";}
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>test</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script>
$(function () {
$.ajax({
url : "/test/setCookie",
method: "get",
success : function (json) {
console.log(json);
}
});
})
</script></head><body>
aaa</body></html>
http://www.a.com:8888/index.html
,访问前先设置hosts,将www.a.com指向我们本机。访问结果如图所示:
@RequestMapping("getCookie")public String getCookie(HttpServletRequest request,HttpServletResponse response){
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length >0) {
for (Cookie cookie : cookies) {
System.out.println("name:" + cookie.getName() + "-----value:" + cookie.getValue());
}
}
return "success";}
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<title>user</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script>
$(function () {
$.ajax({
url : "http://www.b.com:8888/test/getCookie",
method: "get",
success : function (json) {
console.log(json);
}
});
})
</script></head><body></body></html>
name:test-----value:same
不同域下的前后端分离
JSONP解决跨域
@RequestMapping("setCookie")public String setCookie(HttpServletResponse response,String callback){
Cookie cookie = new Cookie("test","same");
cookie.setPath("/");
response.addCookie(cookie);
if (StringUtils.isNotBlank(callback)){
return callback+"('success')";
}
return "success";}@RequestMapping("getCookie")public String getCookie(HttpServletRequest request,HttpServletResponse response,String callback){
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length >0) {
for (Cookie cookie : cookies) {
System.out.println("name:" + cookie.getName() + "-----value:" + cookie.getValue());
}
}
if (StringUtils.isNotBlank(callback)){
return callback+"('success')";
}
return "success";}
<script>
$(function () {
$.ajax({
url : "http://www.b.com:8888/test/setCookie?callback=?",
method: "get",
dataType : 'jsonp',
success : function (json) {
console.log(json);
}
});
})</script>
<script>
$(function () {
$.ajax({
url : "http://www.b.com:8888/test/getCookie?callback=?",
method: "get",
dataType : 'jsonp',
success : function (json) {
console.log(json);
}
});
})</script>
name:test-----value:same
CORS解决跨域
@RequestMapping("setCookie")@CrossOrigin(origins="http://www.a.com:8888",allowCredentials = "true")public String setCookie(HttpServletResponse response){
Cookie cookie = new Cookie("test","same");
cookie.setPath("/");
response.addCookie(cookie);
return "success";}@RequestMapping("getCookie")@CrossOrigin(origins="http://www.a.com:8888",allowCredentials = "true")public String getCookie(HttpServletRequest request,HttpServletResponse response){
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length >0) {
for (Cookie cookie : cookies) {
System.out.println("name:" + cookie.getName() + "-----value:" + cookie.getValue());
}
}
return "success";}
@CrossOrigin
注解允许跨域,
origins
设置了允许跨域请求的域,
allowCredentials
允许设置和接受Cookie。
<script>
$(function () {
$.ajax({
url : "http://www.b.com:8888/test/setCookie",
method: "get",
success : function (json) {
console.log(json);
},
xhrFields: {
withCredentials: true
}
});
})</script><script>
$(function () {
$.ajax({
url : "http://www.b.com:8888/test/getCookie",
method: "get",
success : function (json) {
console.log(json);
},
xhrFields: {
withCredentials: true
}
});
})</script>
name:test-----value:same
总结
-
前后端同域——与普通登录没有区别 -
前后端不同域 -
JSONP方式实现 -
CORS方式实现
以上是关于前后端分离 | 关于登录状态那些事的主要内容,如果未能解决你的问题,请参考以下文章