集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题。因为session是保存在服务器上面的。那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现(通过ip绑定服务器其中一台,就没有集群概念了);2.通过cookie备份session实现(因为cookie数据保存在客户端,不推荐;3.通过redis备份session实现(推荐);

学习本章节之前,建议依次阅读以下文章,更好的串联全文内容,如已掌握以下列出知识点,请跳过:

vSpring Session概念

Spring Session 提供了一套创建和管理 Servlet HttpSession 的方案。Spring Session 提供了集群 Session(Clustered Sessions)功能,默认采用外置的 Redis 来存储 Session 数据(不用手动存储到redis中),以此来解决 Session 共享的问题。

Spring Session 为企业级 Java 应用的 session 管理带来了革新,使得以下的功能更加容易实现:

API 和用于管理用户会话的实现。
支持每个浏览器上使用多个 session,从而能够很容易地构建更加丰富的终端用户体验。
当用户使用 WebSocket 发送请求的时候,能够保持 HttpSession 处于活跃状态。
控制 session id 如何在客户端和服务器之间进行交换,这样的话就能很容易地编写 Restful API,因为它可以从 HTTP头信息中获取 session id,而不必再依赖于 cookie。
将 session 所保存的状态卸载到特定的外部 session 存储中,如 Redis 或 Apache Geode 中,它们能够以独立于应用服务器的方式提供高质量的集群。
HttpSession - 允许以应用程序容器(即 Tomcat)中性的方式替换 HttpSession。

关于更多Spring Session概念的介绍,可以看看官网Spring Session

vSpring Session整合

1.1 引入pmo.xml

        <dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>

1.2 创建useraccount表

CREATE TABLE `useraccount` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`age` int(10) NOT NULL,
`phone` bigint NOT NULL,
`email` varchar(255) NOT NULL,
`account` varchar(100) NOT NULL UNIQUE,
`pwd` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
insert into `useraccount` values(1,'赵(dev)',23,158,'3658561548@qq.com','test001','test001');
insert into `useraccount` values(2,'钱(dev)',27,136,'3658561548@126.com','test002','test002');
insert into `useraccount` values(3,'孙(dev)',31,159,'3658561548@163.com','test003','test003');
insert into `useraccount` values(4,'李(dev)',35,130,'3658561548@sina.com','test004','test004');
select * from `useraccount`;

1.3 mapper

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.dao.UserAccountMapper">
<resultMap id="BaseResultMap" type="com.demo.pojo.UserAccount">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="phone" jdbcType="BIGINT" property="phone" />
<result column="email" jdbcType="VARCHAR" property="email" />
<result column="account" jdbcType="VARCHAR" property="account" />
<result column="pwd" jdbcType="VARCHAR" property="pwd" />
</resultMap>
<sql id="Base_Column_List">
id, username, age, phone, email, account, pwd
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from useraccount
where id = #{id,jdbcType=INTEGER}
</select>
<select id="getUserByAccount" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from useraccount
where account = #{account}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from useraccount
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.demo.pojo.UserAccount">
insert into useraccount (id, username, age,
phone, email, account,
pwd)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{phone,jdbcType=BIGINT}, #{email,jdbcType=VARCHAR}, #{account,jdbcType=VARCHAR},
#{pwd,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.demo.pojo.UserAccount">
insert into useraccount
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="username != null">
username,
</if>
<if test="age != null">
age,
</if>
<if test="phone != null">
phone,
</if>
<if test="email != null">
email,
</if>
<if test="account != null">
account,
</if>
<if test="pwd != null">
pwd,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
<if test="phone != null">
#{phone,jdbcType=BIGINT},
</if>
<if test="email != null">
#{email,jdbcType=VARCHAR},
</if>
<if test="account != null">
#{account,jdbcType=VARCHAR},
</if>
<if test="pwd != null">
#{pwd,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.demo.pojo.UserAccount">
update useraccount
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
<if test="phone != null">
phone = #{phone,jdbcType=BIGINT},
</if>
<if test="email != null">
email = #{email,jdbcType=VARCHAR},
</if>
<if test="account != null">
account = #{account,jdbcType=VARCHAR},
</if>
<if test="pwd != null">
pwd = #{pwd,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.demo.pojo.UserAccount">
update useraccount
set username = #{username,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
phone = #{phone,jdbcType=BIGINT},
email = #{email,jdbcType=VARCHAR},
account = #{account,jdbcType=VARCHAR},
pwd = #{pwd,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
package com.demo.dao;

import com.demo.pojo.UserAccount;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; @Mapper
@Repository
public interface UserAccountMapper {
int deleteByPrimaryKey(Integer id); int insert(UserAccount record); int insertSelective(UserAccount record); UserAccount selectByPrimaryKey(Integer id); UserAccount getUserByAccount(@Param("account") String account); int updateByPrimaryKeySelective(UserAccount record); int updateByPrimaryKey(UserAccount record);
}

1.4 实体类

package com.demo.pojo;

import java.io.Serializable;

public class UserAccount  implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id; private String username; private Integer age; private Long phone; private String email; private String account; private String pwd; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username == null ? null : username.trim();
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Long getPhone() {
return phone;
} public void setPhone(Long phone) {
this.phone = phone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email == null ? null : email.trim();
} public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account == null ? null : account.trim();
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd == null ? null : pwd.trim();
}
}

1.5 service层

Service

UserAccount getUserByAccount(String account);

ServiceImpl

    @Override
public UserAccount getUserByAccount(String account){
return userAccountMapper.getUserByAccount(account);
}

1.6 添加session配置类

package com.demo.common;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; /**
* Created by toutou on 2019/1/22.
*/
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
/**
* maxInactiveIntervalInSeconds: 设置 Session 失效时间,使用 Redis Session 之后,原 Boot 的 server.session.timeout 属性不再生效。
*/
public class SessionRedisConfig {
}

1.7 设置拦截器

关于拦截器不懂的话,可以看我之前的一篇文章《SpringBoot(十一)过滤器和拦截器》

    /*
* 进入controller层之前拦截请求
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
System.out.println("MyTestInterceptor 1111111111");
Object user= request.getSession().getAttribute("useraccount");
if (user==null){
// response.sendRedirect(request.getContextPath()+"/error");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("请先登录");
return false;
} return true;
}
    @Override
public void addInterceptors(InterceptorRegistry registry) {
// 自定义拦截器,添加拦截路径和排除拦截路径
registry.addInterceptor(myTestInterceptor).addPathPatterns("/**").excludePathPatterns("/userlogin");
}

设置全局拦截

1.8 登录/注销

package com.demo.controller;

import com.demo.pojo.UserAccount;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; /**
* Created by toutou on 2019/1/22.
*/
@RestController
public class LoginController {
@Autowired
UserService userService; @RequestMapping(value = "/userlogin")
public String Login(HttpServletRequest request, String account, String pwd)
{
UserAccount user= userService.getUserByAccount(account);
if (user!=null && user.getPwd().equals(pwd)){
request.getSession().setAttribute("useraccount",user);
return "登录成功";
} return "登录失败";
} @RequestMapping(value = "/userlogout")
public String logout (HttpServletRequest request){
HttpSession session=request.getSession();
session.removeAttribute("useraccount");
return "退出登录";
}
}

v效果演示

2.1 直接访问页面

提示:请先登录

2.2 调用登录接口

登录以后再次访问页面,提示访问成功。

2.3 退出登录

v源码地址

https://github.com/toutouge/javademosecond/tree/master/hellospringboot

作  者:请叫我头头哥

出  处:http://www.cnblogs.com/toutou/

关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!

版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信

声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!

SpringBoot进阶教程(二十六)整合Redis之共享Session的更多相关文章

  1. SpringBoot进阶教程(二十九)整合Redis 发布订阅

    SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...

  2. SpringBoot进阶教程(二十八)整合Redis事物

    Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...

  3. SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用

    在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...

  4. SpringBoot进阶教程(二十四)整合Redis

    缓存现在几乎是所有中大型网站都在用的必杀技,合理的利用缓存不仅能够提升网站访问速度,还能大大降低数据库的压力.Redis提供了键过期功能,也提供了灵活的键淘汰策略,所以,现在Redis用在缓存的场合非 ...

  5. SpringBoot进阶教程(五十九)整合Codis

    上一篇博文<详解Codis安装与部署>中,详细介绍了codis的安装与部署,这篇文章主要介绍介绍springboot整合codis.如果之前看过<SpringBoot进阶教程(五十二 ...

  6. SpringBoot进阶教程(七十四)整合ELK

    在上一篇文章<SpringBoot进阶教程(七十三)整合elasticsearch >,已经详细介绍了关于elasticsearch的安装与使用,现在主要来看看关于ELK的定义.安装及使用 ...

  7. SpringBoot进阶教程(二十二)集成RabbitMQ---MQ实战演练

    RabbitMQ是一个在AMQP基础上完成的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.RabbitMQ是流行的开源消息队列系统,用erlang语言开发.Rab ...

  8. SpringBoot进阶教程(二十七)整合Redis之分布式锁

    在之前的一篇文章(<Java分布式锁,搞懂分布式锁实现看这篇文章就对了>),已经介绍过几种java分布式锁,今天来个Redis分布式锁的demo.redis 现在已经成为系统缓存的必备组件 ...

  9. Android高手进阶教程(二十八)之---Android ViewPager控件的使用(基于ViewPager的横向相册)!!!

      分类: Android高手进阶 Android基础教程 2012-09-14 18:10 29759人阅读 评论(35) 收藏 举报 android相册layoutobjectclassloade ...

随机推荐

  1. 14.Ubuntu基本命令

    vi编辑器 {  :上一段diamante } :下一段代码 dw: 删除一个单词 权限 前面的分三组 第一: 文件拥有者的权限 第二:同组者拥有的权限 第三:其他人拥有的权限 前面“-”表示是文件 ...

  2. HTTP协议简单记录

    http协议的格式 1. 首行 2. 头 3. 空行 4. 体 http请求头 #Referer 请求来自哪里,如果是在http://www.baidu.com上点击链接发出的请求,那么Referer ...

  3. JS的事件绑定、事件流模型

    .t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.JS事件 (一)JS事件分类 1.鼠标事件:click/dbclick ...

  4. Netty中如何序列化数据

    JDK提供了ObjectOutputStream和ObjectInputStream,用于通过网络对POJO的基本数据类型和图进行序列化和反序列化.该API并不复杂,而且可以被应用于任何实现了java ...

  5. JXP

    JSX 初识:它是JavaScript的语法扩展,建议在React中使用它来描述UI的外观. 考虑这个变量声明:   在JSX中嵌入表达式 可以通过将其包含在大括号中来嵌入JSX中的任何JavaScr ...

  6. Windows 安装 Vue

    引言 在公司 linux 环境下安装不顺利,回家在 windows 下操作感觉到一种幸福 nginx 先安装了 nginx,其实跟 vue 没关系,只是打算用它做 web 服务,此处略过 nginx ...

  7. 设计模式(Design Patterns)Java版

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  8. Linux下启动时间优化专题

    1. 过往优化总结 陆陆续续在Linux进行启动时间优化,之前有两份文档,分别从内核和用户空间两个方向进行了优化. <Android/Linux boot time分析优化>和<Bu ...

  9. Java基础小知识1——分别使用字节流和字符流复制文件

    在日常使用计算机过程中经常会涉及文件的复制,今天我们就从Java代码的角度,看看在Java程序中文件复制的过程是如何实现的. 1.使用字节流缓冲区复制文件 示例代码如下: import java.io ...

  10. CAP原理和BASE思想

    分布式领域CAP理论,Consistency(一致性), 数据一致更新,所有数据变动都是同步的Availability(可用性), 好的响应性能Partition tolerance(分区容错性) 可 ...