Spring Cloud微服务安全实战_5-4_认证服务器使用spring session

这一节介绍,在认证服务器上使用spring session。

由于认证服务器肯定要是一个高可用状态,所以一定是一个集群,这就需要做session共享,最简单的实现就是使用spring session。

 Spring Session官方文档 :https://docs.spring.io/spring-session/docs/2.1.9.RELEASE/reference/html5/

Spring session有redis实现,有jdbc实现,用法差不多,这里使用redis

 

可以看到,配置一个注解,然后配置好redis,即可使用spring session with redis。

 开始使用 

1,引入maven依赖

 
<!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--springboot2.X默认使用lettuce连接池,需要引入commons-pool2-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <!--spring session redis-->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
 
 
 

2,配好redis

 3,写注解,启用spring session

配置session失效时间:一个月

 重启订单,认证,网关,客户端应用admin,四个服务器,在客户端应用admin上做登录

登录成功后,重启认证服务器,再刷新客户端应用的页面,还是处于登录状态,说明认证服务器的session已经持久化了

 redis:

如果使用jdbc的实现,那就照着官方文档做吧!

github代码:https://github.com/lhy1234/springcloud-security/tree/chapt-5-4-springsession  如果帮助到了你,给个小星星吧

猜你喜欢

转载自www.cnblogs.com/lihaoyang/p/12153911.html