spring boot redis session
1. pom.xml
这里 spring parent的版本 2.1.5会报错 2.1.0和2.1.4经过测试正常
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.2.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.smile</groupId>
- <artifactId>shiro-client</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>shiro-client</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.session</groupId>
- <artifactId>spring-session-data-redis</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
2.config
开启redis session
- import org.springframework.context.annotation.Configuration;
- import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
- @Configuration
- @EnableRedisHttpSession
- public class RedisSessionConfig {
- }
3. application.yml
- spring:
- session:
- store-type: redis
- timeout: 3600s
- redis:
- flush-mode: on_save
- namespace: spring:session
- redis:
- host: localhost
- port:
- timeout: 5000ms
4 测试类
- package com.smile.shiroclient.controller;
- import com.smile.shiroclient.model.User;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.servlet.http.HttpSession;
- import java.util.UUID;
- @RestController
- public class UserController {
- @RequestMapping("/getUser")
- @Cacheable(value="user-key")
- public User getUser() {
- User user=new User("aa@126.com", "aa", "aa123456", "aa","123");
- System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
- return user;
- }
- @RequestMapping("/uid")
- String uid(HttpSession session) {
- UUID uid = (UUID) session.getAttribute("uid");
- if (uid == null) {
- uid = UUID.randomUUID();
- }
- session.setAttribute("uid", uid);
- return session.getId();
- }
- }
到这里session 已经存到redis中, user 还未放入redis
5. 开启查询缓存(将user放入redis)
- import org.springframework.cache.annotation.CachingConfigurerSupport;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.cache.interceptor.KeyGenerator;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import java.lang.reflect.Method;
- @Configuration
- @EnableCaching
- public class RedisConfig extends CachingConfigurerSupport{
- @Bean
- public KeyGenerator keyGenerator() {
- return new KeyGenerator() {
- @Override
- public Object generate(Object target, Method method, Object... params) {
- StringBuilder sb = new StringBuilder();
- sb.append(target.getClass().getName());
- sb.append(method.getName());
- for (Object obj : params) {
- sb.append(obj.toString());
- }
- return sb.toString();
- }
- };
- }
- }
spring boot redis session的更多相关文章
- spring boot + redis 实现session共享
这次带来的是spring boot + redis 实现session共享的教程. 在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring se ...
- spring boot 分布式session实现
spring boot 分布式session实现 主要是通过包装HttpServletRequest将session相关的方法进行代理. 具体是的实现就是通过SessionRepositoryFilt ...
- spring boot redis缓存JedisPool使用
spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...
- Spring Boot 项目学习 (三) Spring Boot + Redis 搭建
0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...
- Spring Boot Redis 集成配置(转)
Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...
- spring boot redis 缓存(cache)集成
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring Boot 分布式Session状态保存Redis
在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而打到另外一台服务器的时候,session丢失. 常规的解决方 ...
- (38)Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而访问到另外 ...
- Spring Boot + Redis 实现Shiro集群
为实现Web应用的分布式集群部署,要解决登录session的统一.本文利用shiro做权限控制,redis做session存储,结合spring boot快速配置实现session共享. 1.引入相关 ...
随机推荐
- 安装VUE教程
这段时间公司要准备开始用VUE,安装的过程中就遇到各种奇葩问题 1.Node.js安装 https://nodejs.org/en/download/ 安装好noedeJS然后继续安装下一步 3.执行 ...
- python 序列 转换 各种操作
# 数据结构 字符串 列表 元组 数字序列# 10-19的整数# r1 = range(10,20)# print(r1)# print(type(r1))## # 19# print(r1[9])# ...
- python3.6 使用newspaper库的Article包来快速抓取网页的文章或者新闻等正文
我主要是用了两个方法来抽去正文内容,第一个方法,诸如xpath,css,正则表达式,beautifulsoup来解析新闻页面的时候,总是会遇到这样那样各种奇奇怪怪的问题,让人很头疼.第二个方法是后面标 ...
- 根据ip获取地理信息.php
根据ip获取地理信息.php <?php function getIPLoc_sina($queryIP){ $url = 'http://int.dpool.sina.com.cn/iploo ...
- JavaSE--类与对象
一.类 类是具有相同特性(数据元素)和行为(功能)的对象的抽象就是类.因此,对象的抽象是类,类的具体化就是对象,也可以说类的实例是对象,类实际上就是一种数据类型.类具有属性,它是对象的状态的抽象,用数 ...
- 【leetcode 476】. Number Complement
给定一个正整数,输出其补码. 思路:利用mask掩码进行异或, 利用 temp >> 1 大于0 来决定mask长度,求出的mask 为二进制 1 0 0 0 0类型, ...
- Ubuntu:MySQL与phpmyadmin安装、配置并使用。
0. 小建议 Ubuntu 16.04.因为MySQL对于Ubuntu 18.04不是很适配,会出现终端MySQL无法输入中文等问题.如果用Ubuntu 18.04,会需要多解决很多细节问题. 建议将 ...
- (转) weblogic 12c忘记密码
weblogic安装后,很久不用,忘记访问控制台的用户名或者密码,可通过以下步骤来重置用户名密码. 说明:%DOMAIN_HOME%:指WebLogic Server 域(Domain)目录 我的是: ...
- 关于 php for zookeeper
原文:Distributed application in PHP with Apache Zookeeper 地址:http://systemsarchitect.net/distributed-a ...
- JSONP 跨域请求原理
0x00 简介 由于浏览器的同源策略,我们想要从别的域获取数据变得困难,需要特殊的技术才能获取 0x01 使用 客户域:client.com 服务器(他域):server.com 如客户想访问 : h ...