10.1 分布式集群环境下的集成(同域名、同项目)

10.1.1        创建SpringBoot的web支持项目07-springboot-session

创建项目

10.1.2        在pom.xml文件中添加依赖

<!-- 配置spring session的依赖 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<!-- 配置spring session data redis的依赖 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<!-- springboot集成redis的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

10.1.3        在application.properties中配置端口号、上下文根及Redis连接

# 配置内嵌tomcat服务器信息
server.port=8080
server.servlet.context-path=/A07-springboot-session #配置redis连接信息
spring.redis.host=192.168.132.128
spring.redis.port=6379
spring.redis.password=123456 spring.session.store-type=redis

10.1.4        创建SessionController向session中存取数据

代码示例

package com.bjpowernode.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; @RestController
public class SessionController { @GetMapping("/boot/set")
public String setSession(HttpSession session){
session.setAttribute("url","http://www.bjpowernode.com");
return "set session ok";
} @GetMapping("/boot/get")
public String getSession(HttpSession session){
String url= (String) session.getAttribute("url");
return url;
}
}

10.1.5        让项目使用SpringSession

  • 在SpingBoot程序入口类Application上@EnableRedisHttpSession注解
package com.bjpowernode.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @SpringBootApplication
@EnableRedisHttpSession
public class A07SpringbootSessionApplication { public static void main(String[] args) {
SpringApplication.run(A07SpringbootSessionApplication.class, args);
} }
注解@EnableRedisHttpSession启用redis作为session容器
  • 在application.properties配置文件中加入(可选):spring.session.store-type=redis

10.1.6        启动Redis服务

10.1.7        启动SpringBoot程序测试

设置地址:http://localhost:8080/A07-springboot-session/boot/set

获取地址:http://localhost:8080/A07-springboot-session/boot/get

  • 通过Redis Desktop Manager查看Redis库中信息

  • 修改服务器端口号,通过jar包的方式启动9090端口的tomcat,通过浏览器访问从session中取数据

springboot集成Spring Session的更多相关文章

  1. SpringBoot 集成 Spring Session

    SpringBoot 集成 Spring Session 应该讲解清楚,为什么要使用 Redis 进行 Session 的管理. Session 复制又是什么概念. Spring Session 在汪 ...

  2. SpringBoot集成Spring Security(7)——认证流程

    文章目录 一.认证流程 二.多个请求共享认证信息 三.获取用户认证信息 在前面的六章中,介绍了 Spring Security 的基础使用,在继续深入向下的学习前,有必要理解清楚 Spring Sec ...

  3. SpringBoot集成Spring Security(6)——登录管理

    文章目录 一.自定义认证成功.失败处理 1.1 CustomAuthenticationSuccessHandler 1.2 CustomAuthenticationFailureHandler 1. ...

  4. SpringBoot集成Spring Security(4)——自定义表单登录

    通过前面三篇文章,你应该大致了解了 Spring Security 的流程.你应该发现了,真正的 login 请求是由 Spring Security 帮我们处理的,那么我们如何实现自定义表单登录呢, ...

  5. Springboot集成Spring Batch

    Spring官网 (https://spring.io/projects/spring-batch#overview)对Spring  Batch的解释: 一个轻量级的.全面的批处理框架,用于开发对企 ...

  6. SpringBoot集成Spring Security入门体验

    一.前言 Spring Security 和 Apache Shiro 都是安全框架,为Java应用程序提供身份认证和授权. 二者区别 Spring Security:重量级安全框架 Apache S ...

  7. SpringBoot集成Spring Security(5)——权限控制

    在第一篇中,我们说过,用户<–>角色<–>权限三层中,暂时不考虑权限,在这一篇,是时候把它完成了. 为了方便演示,这里的权限只是对角色赋予权限,也就是说同一个角色的用户,权限是 ...

  8. SpringBoot集成Spring Security(2)——自动登录

    在上一章:SpringBoot集成Spring Security(1)——入门程序中,我们实现了入门程序,本篇为该程序加上自动登录的功能. 文章目录 一.修改login.html二.两种实现方式 2. ...

  9. Spring boot集成spring session实现session共享

    最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...

随机推荐

  1. centos7修复grub2

    GRUB  :“the Grand Unified Bootloader ”引导加载程序 1.主要配置文件 #/boot/grub2/grub.cfg #rm -rf /boot/grub2/grub ...

  2. eclipse Maven Bootstrap 导航栏

    1.在pom.xml添加两个依赖 Bootstrap 依赖和jQuery依赖 代码如下 <!-- https://mvnrepository.com/artifact/org.webjars/b ...

  3. ES6中Set和Map

    1.Set 实例的创建 Set实例它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身是一个构造函数用来生成Set数据结构. Set 函数可以接受一个数组(或者具有 iterable 接口 ...

  4. mysql --explain+slowlog

    一.EXPALIN 在SQL语句之前加上EXPLAIN关键字就可以获取这条SQL语句执行的计划 那么返回的这些字段是什么呢? 我们先关心一下比较重要的几个字段: 1. select_type 查询类型 ...

  5. 【Python】【demo实验18】【练习实例】【统计输入字符串中,数字的个数、英文字母的个数及其他符号的个数】

    原题: 输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. (本题暂时不支持中文字符及汉字) 我的代码: #!/usr/bin/python # encoding=utf-8 # -* ...

  6. PAT B1020 月饼(25)

    题目描述 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部 ...

  7. python函数篇0-1

    创建类和对象 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “类” 和 “对象” 的使用. 类就是一个模板,模板里可以包含多个函数, ...

  8. Socket-实例

    import socket,os,time server = socket.socket() server.bind(("localhost",9999)) server.list ...

  9. Boot-crm管理系统开发教程(二)

    ps:昨天将管理员登录的功能完成了,并完美的解决跳过登录从而进入管理界面的bug,今天我们将实现"查询用户"功能. ①在po包中创建Customer类,并编写相关变量和添加set/ ...

  10. go 常量

    // 参考:https://studygolang.com/articles/15905?fr=sidebar // iota迭代定义常量 //const配合iota关键字使用,可以定义一组由0开始+ ...