Spring Boot session与cookie的使用
Session
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
@CrossOrigin
public class HelloSessionController {
@RequestMapping("/add")
public String addSession(HttpServletRequest httpServletRequest,
@RequestParam("username")String username) {
HttpSession session = httpServletRequest.getSession();
session.setAttribute("username",username);
session.setMaxInactiveInterval(10000);
return "添加成功";
}
@RequestMapping("/show")
public Object showSession(HttpServletRequest httpServletRequest) {
HttpSession session = httpServletRequest.getSession();
Object object = session.getAttribute("username");
return object;
}
}
Cookie
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/cookie")
public class HelloCookieController {
@RequestMapping("/add")
public String addCookie(HttpServletRequest request,HttpServletResponse response,
@RequestParam("username")String username) {
Cookie cookie = new Cookie("username", username);
cookie.setPath(request.getContextPath());
cookie.setMaxAge(80000);
response.addCookie(cookie);
return "添加成功";
}
@RequestMapping("/show")
public String showCookie(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if(cookie.getName().equals("username")) {
System.out.println(cookie.getName());
System.out.println(cookie.getValue());
return cookie.getValue().toString();
}
}
return "null";
}
}
Spring Boot session与cookie的使用的更多相关文章
- spring boot session error
Error starting ApplicationContext. To display the conditions report re-run your application with 'de ...
- Spring Boot Session 超时时间
springboot session https://www.jianshu.com/p/523572937db8 springboot2以上版本设置session超时时间 https://blog. ...
- spring boot + session+redis解决session共享问题
自己没有亲自试过,不过看了下这个例子感觉靠谱,以后做了测试,在加以说明. PS:后期经验证,上面例子可行.我们平时存session里面的值,直接存在了redis里面了.
- 如何在Spring Boot中使用Cookies
一. 导读 本文大纲 读取HTTP Cookie 设置HTTP Cookie 读取所有Cookie[] 为Cookie设置过期时间 Https与Cookie HttpOnly Cookie 删除Coo ...
- Spring Boot+AngularJS中因为跨域导致Session丢失
http://blog.csdn.net/dalangzhonghangxing/article/details/52446821 如果还在为跨域问题烦恼,请查看博主的 解决angular+sprin ...
- Spring Session - Spring Boot
The completed guide can be found in the boot sample application. Updating Dependencies Before you us ...
- 使用Spring Session实现Spring Boot水平扩展
小编说:本文使用Spring Session实现了Spring Boot水平扩展,每个Spring Boot应用与其他水平扩展的Spring Boot一样,都能处理用户请求.如果宕机,Nginx会将请 ...
- spring boot 中使用redis session
spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...
- Java中设置Session过期时间(Spring Boot)
1.Spring Boot: server.session.cookie.comment = #注释会话cookie. server.session.cookie.domain = #会话cookie ...
随机推荐
- vue-cli 使用better-scroll
better-scroll api文档https://ustbhuangyi.github.io/better-scroll/doc/zh-hans/ 一:安装better-scroll 插件 cn ...
- [总结]多项式类数学相关(定理&证明&板子)
目录 写在前面 前置技能 多项式相关 多项式的系数表示 多项式的点值表示 复数相关 复数的意义 复数的基本运算 单位根 代码相关 多项式乘法 快速傅里叶变换 DFT IDFT 算法实现 递归实现 迭代 ...
- C# ABP - 创建自己的模块
本篇文章介绍怎么创建自己的模块,并且使用依赖注入方法进行模块间的无缝结合. 我们创建一下自己的一个会员模块,针对不同的系统都可以用.你们可以看看我是怎么做的,或者从中得到启发. 目录 1.开始创建项目 ...
- [android] 手机卫士号码归属地查询
使用小米号码归属地数据库,有两张表data1和data2 先查询data1表,把手机号码截取前7位 select outkey from data1 where id=”前七位手机号” 再查询data ...
- MYSQL查询优化(Ⅱ)
本文列举出五个MySQL查询优化的方法,当然,优化的方法还有很多. 1.优化数据类型 MySQL中数据类型有多种,如果你是一名DBA,正在按照优化的原则对数据类型进行严格的检查,但开发人员可能会选择他 ...
- Java基础——iO(一)
此文为了解IO知识的基础篇,这块我看了几天,感觉和前段时间学习集合一样,零散又重要.想记录一下这块由浅入深的学习过程.所以,接下来会记录几篇学习笔记,之后会有以前深入总结.因为这块比较重要啊,所以一定 ...
- 【C#数据结构系列】树和二叉树
线性结构中的数据元素是一对一的关系,树形结构是一对多的非线性结构,非常类似于自然界中的树,数据元素之间既有分支关系,又有层次关系.树形结构在现实世界中广泛存在,如家族的家谱.一个单位的行政机构组织等都 ...
- Linux常用基本命令(head)
head命令 作用:显示文件的头部内容,默认显示前面10行 格式: head [option] [file] -n <行数> -c <字节> ghostwu@dev:~/lin ...
- POJ2387(KB4-A)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 54716 Accepted ...
- JavaScriptDOM操作那些事儿
查询: ①.标准DOM操作API: document.getElementById. document.getElementsByTagName. document.getElementsByName ...