架构实战篇(十二):Spring Boot 分布式Session共享Redis

写了个寂寞 2018-06-14 15:02:25 ⋅ 582 阅读

项目整体结构


一、maven 依赖

这边依赖的是spring boot 1.5.10 版本,2.x的版本session方法有做修改,不过用法没变

<?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>

    <groupId>com.itunion</groupId>
    <artifactId>spring-boot-redis-session</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-redis-session</name>
    <description>spring boot redis session</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build></project>


二、配置内容

默认给项目配置的是debug级别的日志,如果不需要看到可以修改成info或者error

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0

logging.level.root=info
logging.level.com.itunion=debug


三、程序入口

package com.itunion;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class SpringBootRedisSessionApplication {    
   public static void main(String[] args) {        SpringApplication.run(SpringBootRedisSessionApplication.class, args);    } }


四、实体类

用户类字段简单写了几个,昵称,从哪里登录的,会话编号token

package com.itunion.model;
import java.io.Serializable;
public class User implements Serializable {    
   private String nickName;    
   private String loginBy;    
   private String token;    
   public User(String nickName, String loginBy, String token) {            this.nickName = nickName;        
       this.loginBy = loginBy;        
       this.token = token;    }  
   // 省略get , set 方法    @Override    public String toString() {        
       return "User{" +                "nickName='" + nickName + '\'' +                ", loginBy='" + loginBy + '\'' +                ", token='" + token + '\'' +                '}';    } }


五、登录控制层

模拟了普通的账号密码登录,微信code方式登录,从session中获取用户信息,退出登录接口

登录成功需要把sessionId 返回到前端,退出需要调用session的invalidate 方法

package com.itunion.controller;
import com.itunion.model.Result;
import com.itunion.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController@RequestMappingpublic class LoginController {    
@Autowired    private HttpSession session;    
   // 账号密码登录    @GetMapping(value = "login")    
   public Result login(@RequestParam String username, @RequestParam String password) {        System.out.println("login username = [" + username + "], password = [" + password + "]");        User user = new User(username, "app", session.getId());        session.setAttribute("user", user);        
       // 这里记得把会话ID返回到前端,前端之后请求都需要携带该ID, 可以封装到对象中        return new Result<>(user);    }    
   // 微信登录    @GetMapping(value = "loginByWx")    
   public Result loginByWx(@RequestParam String code) {        System.out.println("loginByWx.code = [" + code + "]");                // 调用微信API获取OpenId等信息        User user = new User("Jim", "weixin", session.getId());        session.setAttribute("user", user);        
       return new Result<>(user);    }    
   // 退出    @GetMapping(value = "logout")    
   public Result logout() {        System.out.println("logout");        
       // session 设置为无效的        session.invalidate();        
   return new Result();    }    
   // 使用会话中的信息    @GetMapping(value = "hello")    
   public Result<User> hello() {        User user = (User) session.getAttribute("user");        System.out.println("hello " + user.toString());        
       return new Result<>(user);    } }


六、实现ExpiringSession会话类

因为 SessionRepositoryFilter 拦截器自动注入的是 ExpiringSession 类型的Session 如果你只是实现了Session接口将会报错,所以这边实现的 ExpiringSession 接口


又因为 redis 会自动销毁 session ,所以不需要对time相关方法做具体实现

同时用JsonIgnore 忽略不需要序列化的字段防止反序列化失败

package com.itunion.config.session;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.session.ExpiringSession;
import java.io.Serializable;
import java.util.HashMap;import java.util.Map;
import java.util.Set;
import java.util.UUID;
/** * 因为Filter 拦截器自动注入的是 ExpiringSession 类型的Session * 所以这边实现的 ExpiringSession 方法可以不用管,用JsonIgnore 忽略防止反序列化失败 */
public
class WxRedisSession implements ExpiringSession, Serializable {    
   private String id;    
   private Map<String, Object> sessionAttrs = new HashMap<String, Object>();    
   public WxRedisSession() {        
       this(UUID.randomUUID().toString().replace("-", ""));    }    
   public WxRedisSession(String id) {        
       this.id = id;    }    
   @Override    public String getId() {        
       return id;    }    
   
   @Override
   public <T> T getAttribute(String attributeName) {        
       return (T) this.sessionAttrs.get(attributeName);    }    
   @JsonIgnore    @Override    public Set<String> getAttributeNames() {        
       return this.sessionAttrs.keySet();    }    
   @Override    public void setAttribute(String attributeName, Object attributeValue) {        
       if (attributeValue == null) {            removeAttribute(attributeName);        } else {            
           this.sessionAttrs.put(attributeName, attributeValue);        }    }        
   @Override    public void removeAttribute(String attributeName) {        
       this.sessionAttrs.remove(attributeName);    }    
   public void setId(String id) {        
       this.id = id;    }    
   //   反序列化需要用到get set方法    public Map<String, Object> getSessionAttrs() {        
       return sessionAttrs;    }    
   public void setSessionAttrs(Map<String, Object> sessionAttrs) {        this.sessionAttrs = sessionAttrs;    }    
   // redis 会自动销毁 session ,所以不需要使用下面的方法    @JsonIgnore    @Override    public long getCreationTime() {        
       return 0;    }    
   @JsonIgnore    @Override    public void setLastAccessedTime(long lastAccessedTime) {    }    
   @JsonIgnore    @Override    public long getLastAccessedTime() {        
       return 0;    }    
   @JsonIgnore    @Override    public void setMaxInactiveIntervalInSeconds(int interval) {    }    
   @JsonIgnore    @Override    public int getMaxInactiveIntervalInSeconds() {        
       return 0;    }    
   @JsonIgnore    @Override    public boolean isExpired() {        
       return false;    } }


七、实现SessionRepository接口会话持久层

这个类主要就是通过RedisTemplate 这个类对session对象的保存、修改、删除等操作

package com.itunion.config.session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.ExpiringSession;
import org.springframework.session.SessionRepository;
import java.util.concurrent.TimeUnit;
/** * 主要用来管理session对象 */

public
class WxRedisSessionRepository implements SessionRepository<WxRedisSession> {    private static Logger log = LoggerFactory.getLogger(WxRedisSessionRepository.class);    
   // redis 连接工具    private RedisTemplate<String, Object> redisTemplate;    
   /**     * 如果不为空,将覆盖默认的超时时间,单位秒     * {@link ExpiringSession#setMaxInactiveIntervalInSeconds(int)}.     */    private Integer defaultMaxInactiveInterval;    
   public WxRedisSessionRepository(RedisTemplate<String, Object> redisTemplate) {        
       this.redisTemplate = redisTemplate;    }    
   public WxRedisSessionRepository(RedisTemplate<String, Object> redisTemplate, Integer defaultMaxInactiveInterval) {        
       this.redisTemplate = redisTemplate;        
       this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;    }    @Override    
   public WxRedisSession createSession()
{        WxRedisSession session = new WxRedisSession();        
       log.debug("createSession " + session.getId());        
       return session;    }    @Override    
   public void save(WxRedisSession session)
{        
       log.debug("save " + session.getId());        redisTemplate.opsForValue().set(session.getId(), session, defaultMaxInactiveInterval, TimeUnit.SECONDS);    }    @Override    
   public WxRedisSession getSession(String id)
{        
       log.debug("getSession " + id);        
       if (redisTemplate.hasKey(id)) {            
           return (WxRedisSession) redisTemplate.opsForValue().get(id);        } else {            
           return null;        }    }    @Override    
   public void delete(String id)
{        
       log.debug("delete " + id);        redisTemplate.delete(id);    }    
   public void setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval) {        
       this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;    } }


八、自定义会话策略 HttpSessionStrategy

这个类可以理解为一个请求过来了,程序从哪里去拿我需要的会话编号,主要用到的方法就是 getRequestedSessionId

package com.itunion.config.session;
import org.springframework.session.Session;
import org.springframework.session.web.http.HttpSessionStrategy;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// 会话策略, 比如会话的标识从哪里获取
public
class WxHttpSessionStrategy implements HttpSessionStrategy {      private String name;    
       public WxHttpSessionStrategy() {        
       this("token");    }    
   public WxHttpSessionStrategy(String name) {        
       this.name = name;    }    
   @Override    public String getRequestedSessionId(HttpServletRequest request) {        // 从header 中获取        String token = request.getHeader(name);        
       if (token != null) return token;        
       // 从请求参数中获取        token = request.getParameter(name);        
       if (token != null) return token;        
       // 增加自己的获取方式 比如:cookie        return null;    }    
   @Override    public void onNewSession(Session session, HttpServletRequest request, HttpServletResponse response) {        response.setHeader(this.name, session.getId());    }    
   @Override    public void onInvalidateSession(HttpServletRequest request, HttpServletResponse response) {        response.setHeader(this.name, "");    }    
   public void setName(String name) {        
       this.name = name;    } }


九、Redis 配置

这里我重新设置 StringRedisTemplate 值的序列化方式,把value内容序列化为json字符串
如果你希望只保存字符串的内容,可以只返回StringRedisTemplate对象

package com.itunion.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@Configurationpublic class RedisConfig {    
   @Value("${spring.redis.host}")    
   private String host;    
   @Value("${spring.redis.port}")    
   private Integer port;    
   @Value("${spring.redis.password}")    
   private String password;    
   @Value("${spring.redis.database}")    
   private Integer database;    @Bean    RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {        RedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);        
       // 重新设置 StringRedisTemplate 值的序列化方式,把value内容序列化为json字符串        // 如果你希望只保存字符串的内容,可以吧下面的内容去掉只用StringRedisTemplate        Jackson2JsonRedisSerializer<Object> redisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);        ObjectMapper om = new ObjectMapper();        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);        redisSerializer.setObjectMapper(om);        redisTemplate.setValueSerializer(redisSerializer);        
       // 应用设置        redisTemplate.afterPropertiesSet();        
       return redisTemplate;    }    
   @Bean    RedisConnectionFactory connectionFactory() {        
   // 建立redis 连接        JedisConnectionFactory factory = new JedisConnectionFactory();        factory.setHostName(host);        factory.setPort(port);        factory.setPassword(password);        factory.setDatabase(database);        
       return factory;    } }


十、启用配置

这里需要使用 @EnableSpringHttpSession 注解

package com.itunion.config.session;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
import org.springframework.session.web.http.HttpSessionStrategy;
@Configuration@EnableSpringHttpSessionpublic class HttpSessionConfig {    
   //session策略,这里默认会从头部,请求参数中获取内容    // 这里的token 可以自定义,主要用于请求参数的名字    @Bean    HttpSessionStrategy httpSessionStrategy() {        
       return new WxHttpSessionStrategy("token");    }    
   @Bean    WxRedisSessionRepository sessionRepository(RedisTemplate redisTemplate) {        
       return new WxRedisSessionRepository(redisTemplate, 3600);    } }

启动Redis服务


启动项目

模拟移动端测试

  1. 模拟登录操作 http://localhost:8080/loginByWx?code=123

{
   "code":100,"message":null,"result":{"nickName":"Jim","loginBy":"weixin","token":"0eab2c62e185400489f51c060ed1360f"},"timestamp":1528954812490}

看下redis 的数据


  1. 模拟普通请求

token参数要取登录成功返回的token值
http://localhost:8080/hello?token=0eab2c62e185400489f51c060ed1360f

  1. 模拟退出
    http://localhost:8080/logout?token=0eab2c62e185400489f51c060ed1360f

redis 中对应的token也会被删除


总结

在本篇文章当中我们看到原先写的 HttpFilter 和 HttpServlet 在 Spring boot 中可以方便快捷的配置进来,对于我们老的项目的支持还是不错的


更多精彩内容

架构实战篇(一):Spring Boot 整合MyBatis

架构实战篇(二):Spring Boot 整合Swagger2

架构实战篇(三):Spring Boot 整合MyBatis()

架构实战篇(四):Spring Boot 整合 Thymeleaf

架构实战篇(五):Spring Boot 表单验证和异常处理

架构实战篇(六):Spring Boot RestTemplate的使用

架构实战篇(七):Spring Boot Data JPA 快速入门

架构实战篇(八):Spring Boot 集成 Druid 数据源监控


关注我们

Git源码地址:https://github.com/qiaohhgz/spring-boot-redis-session.git


全部评论: 0

    我有话说:

    架构实战七):Spring Boot Assembly 整合 thymeleaf

    如何让服务器上的 sprig boot 项目升级变的方便快捷

    架构实战):Spring Boot 集成 Dubbo

    Dubbo是阿里巴巴SOA服务化治理方案的核心框架,一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案。

    架构实战五):Spring Boot 解耦之事件驱动

    通过使用spring 事件来解决业务代码的耦合

    架构实战一):Spring Boot 集成企业级搜索引擎 SolrCloud

    Solr是以Lucene为基础实现的文本检索应用服务。Solr部署方式有单机方式、多机Master-Slaver方式、Cloud方式。

    码云推荐:一个优秀的分布式spring boot/Spring Cloud API限流框架,特别适合微服务架构

    一个优秀的分布式spring boot/Spring Cloud API限流框架,特别适合微服务架构.

    架构实战(三)-Spring Boot架构搭建RESTful API案例

    之前分享了Spring Boot 整合Swagger 让API可视化和前后端分离架构 受到了大家一致好评 ,本节就接着上节的代码做了详细的查询代码的补充和完善并搭建RESTful API架构案例。

    架构实战(七):Spring Boot Data JPA 快速入门

    Spring Data JPA 是Spring Data 的一个子项目,它通过提供基于JPA的Repository极大了减少了操作JPA的代码。

    架构实战(六):Spring Boot RestTemplate的使用

    RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。

    架构实战四):Spring Boot 多缓存实战

    多场景下的不同缓存策略解决方案

    架构实战(一)-Spring Boot+MyBatis基础架构搭建

    Spring的追求一定是简单点简单点,让java的开发变得更加简单、容易。瞧瞧的告诉你们直接copy就能用哦~~~

    架构实战三):Spring Boot Logback 邮件通知

    日志对于应用程序来说是非常重要的,当你的程序报错了,而你又不知道是多么可怕的一件事情,本文使用logback把程序报错信息邮件到开发者

    微服务架构实战(六):Spring boot2.x 集成阿里大鱼短信接口详解与Demo

    Spring boot2.x 集成阿里大鱼短信接口,发送短信验证码及短信接口详解。

    架构实战六):Spring Boot Assembly服务化打包

    使用assembly来打包springboot微服务项目,让发布更简单

    SpringBoot+zk+dubbo架构实践):SpringBoot 集成 zookeeper

    不啰嗦,本完成两件事:1、搭建SpringBoot 框架;2、基于spring boot框架访问zookeeper。

    架构实战)-Spring Boot整合Swagger,让你的API可视化

    你还在跟前端对接上花费很多的时间而没有效果吗?你还在为写接口文档而烦恼吗?今天就教大家一个接口对接神器...

    架构实战(九):Spring Boot 集成 RocketMQ

    快速集成阿里开源消息队列 RocketMQ

    微服务架构学习笔记:gRPC Spring Boot Starter 2.2.0 发布,及使用步骤

    gRPC Spring Boot Starter 项目是一个 gRPC 的 Spring Boot 模块。内嵌一个 gRPC Server 对外提供服务,并支持 Spring Cloud 的服务发现