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 ...
随机推荐
- 分布式理论(七)—— 一致性协议之 ZAB
前言 在前面的文章中,我们说了很多一致性协议,比如 Paxos,Raft,2PC,3PC等等,今天我们再讲一种协议,ZAB 协议,该协议应该是所有一致性协议中生产环境中应用最多的了.为什么呢?因为他是 ...
- [C#]使用RabbitMQ模拟抽奖系统的例子
背景:在实际的项目中,经常有客户需要做抽奖的活动,大部分的都是注册送产品.送红包这些需求.这都是有直接的利益效果,所以经常会遇见系统被盗刷的情况,每一次遇见这种项目的上线都是绷紧神经,客户又都喜欢在过 ...
- CSS 分类 选择器
CSS:层叠样式表(英文全称:Cascading Style Sheets) 后缀名:css 标志 style 对网页中元素位置的排版进行像素级精 ...
- Oracle11g自带的SQL_developer无法打开
在安装完Oracle Database 11g Release 2数据库,想试一下Oracle自带的SQL DeveloperW工具,在操作系统菜单的所有程序中找到SQL Developer如下所示, ...
- C#进行数据筛选(一)
这里介绍数据筛选的第一种方式,不用过滤器,给新手看得 public DataTable SourceList(string Wmain, string OrderNo, string Process) ...
- 【15】模板方法模式(Template Method)
一.引言 提到模板,大家肯定不免想到生活中的“简历模板”.“论文模板”.“Word中模版文件”等.在现实生活中,模板的概念就是——有一个规定的格式,然后每个人都可以根据自己的需求或情况去更新它.例如简 ...
- 【学习笔记】--- 老男孩学Python,day5 列表 元祖
今日主要内容1. list(增删改查) 列表可以装大量的数据. 不限制数据类型. 表示方式:[] 方括号中的每一项用逗号隔开 列表和字符串一样.也有索引和切片 常用的功能: 1. 增: append( ...
- JavaScript--DOM元素尺寸和位置(22)
一 获取元素的CSS大小 1.通过style内联获取元素的大小 var box = document.getElementById('box'); // 获得元素; box.style.width; ...
- BZOJ5289: [Hnoi2018]排列
传送门 第一步转化,令 \(q[p[i]]=i\),那么题目变成: 有一些 \(q[a[i]]<q[i]\) 的限制,\(q\) 必须为排列,求 \(max(\sum_{i=1}^{n}w[i] ...
- HTML5 简单归纳 -- 前端知识 (二)
HTML5 全屏事件 全屏事件:requestFullScreen 关闭全屏:cancelFullScreen 判断是否全屏:fullScreenElement 注意:现各大主流浏览器中由于内核不同的 ...