关于前后端不在统一服务器的跨域设置Cookie问题

1.后端允许跨域调用,并添加Cookie

	@CrossOrigin
	@RequestMapping(value="/login", produces = "application/json; charset=utf-8", method=RequestMethod.POST)
	@ApiOperation(value = "用户登录")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "string"),
		@ApiImplicitParam(name = "pwd", value = "密码", required = true, dataType = "string")
	})
	@ApiResponses({
		@ApiResponse(code = 200, message = "result", response = result.class)
	})
	public Object login(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value="name")String name,
			@RequestParam(value="pwd")String pwd) {

		System.out.println("login:");
		//1.验证
		result ret = new result(200, "登录成功", 0, tutil.currMSecs(), null);
	
		//略

		//
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

		String sSessionId = UUID.randomUUID().toString().replaceAll("-", "");;
		Cookie ck = new Cookie(sdpt.SEN_ID, sSessionId);
		ck.setPath("/");
		ck.setMaxAge(30*1000*60);

		response.addCookie(ck);

		HttpSession session = request.getSession();
		session.setAttribute(sdpt.SEN_ID, sSessionId);
		return ret;
	}

2.前端允许跨域

form.on('submit(hug_adminLogin)', function(data) {
		console.log(data.field)
		
		// 登录
		$.ajax({
			type: "post",
			url:  'http://xx.xxx.xx.xxx:8080/xxxx/syssetup/login',
			data: {
				name : 'xxxx',
				pwd  : 'x'
			},
			dataType: "json",
			xhrFields: {
				withCredentials:true
			},
			crossDomain: true,
			success: function(res){
				console.log(res);
				if(res.code == 200){
					layer.msg(res.msg);
				}else{
					layer.msg(res.msg);
				}
			}
		});
	})

结果:

结果2

但是在Application里面都没有Cookie,所以下次请求了,也没带上 

 

问题就是怎么才能带上Cookie

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值