SpringBoot 控制和使用前端 Cookie

一、Java 代码

package com.swmfizl.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@CrossOrigin(value = "http://localhost", maxAge = 3600, allowCredentials = "true")
public class CookieController {
	@RequestMapping(value = "setCookie", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public Map<String, Object> setCookie(HttpServletResponse response) {
		Map<String, Object> res = new HashMap<String, Object>();
		Cookie cookie = new Cookie("token", "cdb85c2f-2695-4982-a3e8-95754f8b50e0");
		cookie.setPath("/");
		cookie.setMaxAge(3600);
		response.addCookie(cookie);
		res.put("cookie", cookie );
		res.put("code", 0);
		res.put("msg", "添加cookie成功");
		return res;
	}

	@RequestMapping(value = "getCookie", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public Map<String, Object> getCookie(HttpServletRequest request) {
		Map<String, Object> res = new HashMap<String, Object>();
		Cookie[] cookies = request.getCookies();
		res.put("cookies", cookies);
		res.put("code", 0);
		res.put("msg", "获取cookies成功");
		return res;
	}
}

二、前端代码(Vue)

new Vue({
	el: "#App",
	data: {

	},
	methods: {
		/**
		 * 设置Cookie
		 * @param {Funtion} callFunction 回调方法
		 */
		setCookie: function(callFunction) {
			axios({
				method: 'post',
				url: "http://localhost:8080/setCookie",
				withCredentials: true,
			}).then(function(res) {
				callFunction(res)
			});
		},

		/**
		 * 获取Cookie
		 * @param {Funtion} callFunction 回调方法
		 */
		getCookie: function(callFunction) {
			axios({
				method: 'post',
				url: "http://localhost:8080/getCookie",
				withCredentials: true,
			}).then(function(res) {
				callFunction(res)
			});
		}
	},
	mounted: function() {
		var THIS = this;
		// 测试Cookie 使用
		this.setCookie(function(res) {
			console.log(res);
			THIS.getCookie(function(res) {
				console.log(res)
			})
		})
	}
})

三、运行前端页面,成功设置和获取 Cookie

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用`@CookieValue`注解来获取从前端传递过来的cookie值。具体地,可以在Controller的方法参数中使用该注解来获取cookie值,如下所示: ```java @GetMapping("/example") public String exampleMethod(@CookieValue("cookieName") String cookieValue, Model model) { model.addAttribute("cookieValue", cookieValue); return "example"; } ``` 上述代码中,`@CookieValue`注解用于获取名为`cookieName`的cookie值,并将其赋值给`cookieValue`参数。然后,可以将`cookieValue`添加到Model中,返回前端。 在前端,可以使用JavaScript的`document.cookie`属性来获取cookie值,并通过AJAX请求将其发送回后端。具体地,可以使用jQuery的`$.ajax()`方法来发送AJAX请求,如下所示: ```javascript var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)cookieName\s*\=\s*([^;]*).*$)|^.*$/, "$1"); $.ajax({ url: "/example", type: "GET", data: {cookieValue: cookieValue}, success: function(response) { console.log(response); }, error: function(xhr) { console.log(xhr.responseText); } }); ``` 上述代码中,`document.cookie`属性用于获取名为`cookieName`的cookie值,并将其保存在`cookieValue`变量中。然后,使用`$.ajax()`方法发送GET请求,并将`cookieValue`作为参数传递给后端。在后端,可以通过`@RequestParam`注解来获取请求参数,如下所示: ```java @GetMapping("/example") public String exampleMethod(@RequestParam("cookieValue") String cookieValue, Model model) { model.addAttribute("cookieValue", cookieValue); return "example"; } ``` 上述代码中,`@RequestParam`注解用于获取名为`cookieValue`的请求参数,并将其赋值给`cookieValue`参数。然后,可以将`cookieValue`添加到Model中,返回前端
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值