SpringMVC学习笔记3_通过redis缓存和cookie实现Session共享

本文介绍了一种通过Redis缓存和Cookie来实现在多个业务系统间共享Session的SSO单点登录解决方案。流程包括业务系统调用SSO登录接口,SSO接口在Redis中存储Session信息,然后将SessionID返回并存入业务系统的Cookie,最后业务系统利用Cookie中的SessionID获取和验证用户信息。
摘要由CSDN通过智能技术生成
摘要:

通过redis缓存和cookie实现单点登录


整体实现思路如下图所示。

Session的缓存机制


1.业务系统发起登录请求,调用SSO用户登录接口;


2.SSO登录接口的处理逻辑:

2.1 根据用户名和密码去数据库验证用户是否合法。

2.2 用户验证通过之后,生成SessionID,并返回给业务系统。

   同时以SessionID为key,存储用户信息到redis缓存中


  SSO登录接口代码:
  public JSONObject userLogin(@RequestBody JSONObject jsonObject){
        UserLoginResponse userLoginResponss = new UserLoginResponse();
        try {
            logger.info("处理用户登录业务逻辑,接收报文"+jsonObject);
            String msgWithDigesta=SecurityUtil.scfMatch(jsonObject.toString(), newXService.getPrivateKey());
            //生成实体
            User user = JSONObject.parseObject(msgWithDigesta,User.class);
            //是否验证用户的密码
            boolean isChechPassword = true;
            User userInfo = anaService.loginCheckUserInfo(user,isChechPassword);
            // 存储用户信息到redis缓存中
            String ticket = anaService.storeUserLoginSessionInRedis(userInfo,user.getModuleCode());
          
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 添加Redis依赖库 在项目的pom.xml文件中添加Redis的依赖库。 ```xml <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.3.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.0</version> </dependency> ``` 2. 配置Redis连接池 在SpringMVC的配置文件中添加Redis的连接池配置。 ```xml <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost"/> <property name="port" value="6379"/> <<property name="password" value="password"/>> <property name="poolConfig"> <bean class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1000"/> <property name="maxIdle" value="100"/> <property name="minIdle" value="10"/> <property name="maxWaitMillis" value="10000"/> </bean> </property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> ``` 3. 配置Session共享SpringMVC的配置文件中添加Session共享的配置。 ```xml <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="1800"/> </bean> <bean class="org.springframework.session.web.http.CookieHttpSessionIdResolver"> <constructor-arg name="cookieName" value="SESSION_ID"/> <property name="cookiePath" value="/"/> <property name="cookieMaxAge" value="-1"/> </bean> ``` 4. 启用Session共享SpringMVC的配置文件中启用Session共享。 ```xml <bean class="org.springframework.session.web.http.SessionRepositoryFilter"> <constructor-arg> <bean class="org.springframework.session.data.redis.RedisOperationsSessionRepository"> <constructor-arg ref="redisTemplate"/> </bean> </constructor-arg> </bean> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="sessionAttributeStore"> <bean class="org.springframework.session.web.http.SessionRepositoryFilter"> <constructor-arg> <bean class="org.springframework.session.data.redis.RedisOperationsSessionRepository"> <constructor-arg ref="redisTemplate"/> </bean> </constructor-arg> </bean> </property> </bean> ``` 5. 测试 启动项目后,访问任意一个页面,然后在Redis中就可以看到Session的信息了。 ```shell redis-cli 127.0.0.1:6379> keys * 1) "spring:session:sessions:5b5d4b4f-9a9f-4e4e-bd56-4b4c7f1d8f0b" 127.0.0.1:6379> get spring:session:sessions:5b5d4b4f-9a9f-4e4e-bd56-4b4c7f1d8f0b "\xac\xed\x00\x05sr\x00\x17java.util.HashMap\x85\x95\x1d\x9b\x94\xee\x0c\x00\x00\x03I\x00\x05valuexr\x00\x10java.lang.String\x86\xac\xed\x00\x05sr\x00\x11java.lang.String\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01Ljava/lang/String;L\x00\x04namet\x00\x12Ljava/lang/String;L\x00\x08usernameq\x00~\x00\x01sq\x00~\x00\x0bpasswordq\x00~\x00\x0b" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值