1. pom.xml

这里 spring parent的版本 2.1.5会报错 2.1.0和2.1.4经过测试正常

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.2.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.smile</groupId>
  12. <artifactId>shiro-client</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>shiro-client</name>
  15. <description>Demo project for Spring Boot</description>
  16.  
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20.  
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-data-redis</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.session</groupId>
  28. <artifactId>spring-session-data-redis</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-web</artifactId>
  37. </dependency>
  38.  
  39. </dependencies>
  40.  
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49.  
  50. </project>

2.config

开启redis session

  1. import org.springframework.context.annotation.Configuration;
  2. import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
  3.  
  4. @Configuration
  5. @EnableRedisHttpSession
  6. public class RedisSessionConfig {
  7. }

3. application.yml

  1. spring:
  2. session:
  3. store-type: redis
  4. timeout: 3600s
  5. redis:
  6. flush-mode: on_save
  7. namespace: spring:session
  8. redis:
  9. host: localhost
  10. port:
  11. timeout: 5000ms

4 测试类

  1. package com.smile.shiroclient.controller;
  2.  
  3. import com.smile.shiroclient.model.User;
  4. import org.springframework.cache.annotation.Cacheable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. import javax.servlet.http.HttpSession;
  9. import java.util.UUID;
  10.  
  11. @RestController
  12. public class UserController {
  13.  
  14. @RequestMapping("/getUser")
  15. @Cacheable(value="user-key")
  16. public User getUser() {
  17. User user=new User("aa@126.com", "aa", "aa123456", "aa","123");
  18. System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
  19. return user;
  20. }
  21.  
  22. @RequestMapping("/uid")
  23. String uid(HttpSession session) {
  24. UUID uid = (UUID) session.getAttribute("uid");
  25. if (uid == null) {
  26. uid = UUID.randomUUID();
  27. }
  28. session.setAttribute("uid", uid);
  29. return session.getId();
  30. }
  31. }

到这里session 已经存到redis中, user 还未放入redis

5. 开启查询缓存(将user放入redis)

  1. import org.springframework.cache.annotation.CachingConfigurerSupport;
  2. import org.springframework.cache.annotation.EnableCaching;
  3. import org.springframework.cache.interceptor.KeyGenerator;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. import java.lang.reflect.Method;
  8.  
  9. @Configuration
  10. @EnableCaching
  11. public class RedisConfig extends CachingConfigurerSupport{
  12.  
  13. @Bean
  14. public KeyGenerator keyGenerator() {
  15. return new KeyGenerator() {
  16. @Override
  17. public Object generate(Object target, Method method, Object... params) {
  18. StringBuilder sb = new StringBuilder();
  19. sb.append(target.getClass().getName());
  20. sb.append(method.getName());
  21. for (Object obj : params) {
  22. sb.append(obj.toString());
  23. }
  24. return sb.toString();
  25. }
  26. };
  27. }
  28. }

spring boot redis session的更多相关文章

  1. spring boot + redis 实现session共享

    这次带来的是spring boot + redis 实现session共享的教程. 在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring se ...

  2. spring boot 分布式session实现

    spring boot 分布式session实现 主要是通过包装HttpServletRequest将session相关的方法进行代理. 具体是的实现就是通过SessionRepositoryFilt ...

  3. spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...

  4. Spring Boot 项目学习 (三) Spring Boot + Redis 搭建

    0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...

  5. Spring Boot Redis 集成配置(转)

    Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...

  6. spring boot redis 缓存(cache)集成

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  7. Spring Boot 分布式Session状态保存Redis

    在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而打到另外一台服务器的时候,session丢失. 常规的解决方 ...

  8. (38)Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而访问到另外 ...

  9. Spring Boot + Redis 实现Shiro集群

    为实现Web应用的分布式集群部署,要解决登录session的统一.本文利用shiro做权限控制,redis做session存储,结合spring boot快速配置实现session共享. 1.引入相关 ...

随机推荐

  1. 安装VUE教程

    这段时间公司要准备开始用VUE,安装的过程中就遇到各种奇葩问题 1.Node.js安装 https://nodejs.org/en/download/ 安装好noedeJS然后继续安装下一步 3.执行 ...

  2. python 序列 转换 各种操作

    # 数据结构 字符串 列表 元组 数字序列# 10-19的整数# r1 = range(10,20)# print(r1)# print(type(r1))## # 19# print(r1[9])# ...

  3. python3.6 使用newspaper库的Article包来快速抓取网页的文章或者新闻等正文

    我主要是用了两个方法来抽去正文内容,第一个方法,诸如xpath,css,正则表达式,beautifulsoup来解析新闻页面的时候,总是会遇到这样那样各种奇奇怪怪的问题,让人很头疼.第二个方法是后面标 ...

  4. 根据ip获取地理信息.php

    根据ip获取地理信息.php <?php function getIPLoc_sina($queryIP){ $url = 'http://int.dpool.sina.com.cn/iploo ...

  5. JavaSE--类与对象

    一.类 类是具有相同特性(数据元素)和行为(功能)的对象的抽象就是类.因此,对象的抽象是类,类的具体化就是对象,也可以说类的实例是对象,类实际上就是一种数据类型.类具有属性,它是对象的状态的抽象,用数 ...

  6. 【leetcode 476】. Number Complement

    给定一个正整数,输出其补码. 思路:利用mask掩码进行异或, 利用 temp >> 1 大于0 来决定mask长度,求出的mask 为二进制 1 0 0 0 0类型,           ...

  7. Ubuntu:MySQL与phpmyadmin安装、配置并使用。

    0. 小建议 Ubuntu 16.04.因为MySQL对于Ubuntu 18.04不是很适配,会出现终端MySQL无法输入中文等问题.如果用Ubuntu 18.04,会需要多解决很多细节问题. 建议将 ...

  8. (转) weblogic 12c忘记密码

    weblogic安装后,很久不用,忘记访问控制台的用户名或者密码,可通过以下步骤来重置用户名密码. 说明:%DOMAIN_HOME%:指WebLogic Server 域(Domain)目录 我的是: ...

  9. 关于 php for zookeeper

    原文:Distributed application in PHP with Apache Zookeeper 地址:http://systemsarchitect.net/distributed-a ...

  10. JSONP 跨域请求原理

    0x00 简介 由于浏览器的同源策略,我们想要从别的域获取数据变得困难,需要特殊的技术才能获取 0x01 使用 客户域:client.com 服务器(他域):server.com 如客户想访问 : h ...