目录
需求
记录token
清除token
解决登出时长过长
首页 Java java教程 SpringBoot Security如何实现单点登出并清除所有token

SpringBoot Security如何实现单点登出并清除所有token

May 14, 2023 pm 01:43 PM
token springboot security

需求

  • A、B、C 系统通过 sso 服务实现登录

  • A、B、C 系统分别获取 Atoken、Btoken、Ctoken 三个 token

  • 其中某一个系统主动登出后,其他两个系统也登出

  • 至此全部 Atoken、Btoken、Ctoken 失效

记录token

pom 文件引入依赖

  • Redis数据库依赖

  • hutool:用于解析token

1

2

3

4

5

6

7

8

9

<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

<dependency>

   <groupId>cn.hutool</groupId>

   <artifactId>hutool-all</artifactId>

   <version>5.7.13</version>

</dependency>

登录后复制

token 存储类 实现 AuthJdbcTokenStore

  • TokenStore 继承 JdbcTokenStore

  • 使用登录用户的用户名 username 做 Redis 的 key

  • 因为用户登录的系统会有多个,所以 value 使用 Redis 的列表类型来存储 token

  • 设置有效时间,保证不少于 list 里 token 的最大有效时间

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

@Component

public class AuthJdbcTokenStore extends JdbcTokenStore {

    public static final String USER_HAVE_TOKEN = "user-tokens:";

    @Resource

    RedisTemplate redisTemplate;

    public AuthJdbcTokenStore(DataSource connectionFactory) {

        super(connectionFactory);

    }

    @Override

    public void storeAccessToken(OAuth3AccessToken token, OAuth3Authentication authentication) {

        super.storeAccessToken(token, authentication);

        if (Optional.ofNullable(authentication.getUserAuthentication()).isPresent()) {

            User user = (User) authentication.getUserAuthentication().getPrincipal();

            String userTokensKey = USER_HAVE_TOKEN + user.getUsername();

            String tokenValue = token.getValue();

            redisTemplate.opsForList().leftPush(userTokensKey, tokenValue);

            Long seconds = redisTemplate.opsForValue().getOperations().getExpire(userTokensKey);

            Long tokenExpTime = getExpTime(tokenValue);

            Long expTime = seconds < tokenExpTime ? tokenExpTime : seconds;

            redisTemplate.expire(userTokensKey, expTime, TimeUnit.SECONDS);

        }

    }

    private long getExpTime(String accessToken) {

        JWT jwt = JWTUtil.parseToken(accessToken);

        cn.hutool.json.JSONObject jsonObject = jwt.getPayload().getClaimsJson();

        long nowTime = Instant.now().getEpochSecond();

        long expEndTime = jsonObject.getLong("exp");

        long expTime = (expEndTime - nowTime);

        return expTime;

    }

}

登录后复制

oauth_access_token 使用 JdbcTokenStore 存储 token 需要新增表

1

2

3

4

5

6

7

8

9

10

11

CREATE TABLE `oauth_access_token` (

  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,

  `token_id` varchar(255) DEFAULT NULL,

  `token` blob,

  `authentication_id` varchar(255) DEFAULT NULL,

  `user_name` varchar(255) DEFAULT NULL,

  `client_id` varchar(255) DEFAULT NULL,

  `authentication` blob,

  `refresh_token` varchar(255) DEFAULT NULL,

  UNIQUE KEY `authentication_id` (`authentication_id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

登录后复制

AuthorizationServerConfigurerAdapter 使用 AuthJdbcTokenStore 做 token 存储

  • 引入 DataSource,因为 JdbcTokenStore 的构造方法必须传入 DataSource

  • 创建按 TokenStore,用 AuthJdbcTokenStore 实现

  • tokenServices 添加 TokenStore

  • endpoints 添加 tokenServices

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

@Configuration

@EnableAuthorizationServer

public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired

    private DataSource dataSource;

    ...

    @Bean

    public TokenStore tokenStore() {

        JdbcTokenStore tokenStore = new AuthJdbcTokenStore(dataSource);

        return tokenStore;

    }

    @Override

    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {

        DefaultTokenServices tokenServices = new DefaultTokenServices();

        tokenServices.setTokenStore(tokenStore());

        endpoints

                .authenticationManager(authenticationManager)

                .tokenServices(tokenServices)

                .accessTokenConverter(converter)

        ;

    }

    ...

}

登录后复制

清除token

  • 继承 SimpleUrlLogoutSuccessHandler

  • 获取用户名 userName

  • 获取登录时存储在 Redis 的 token 列表

  • token 字符串转换成 OAuth3AccessToken

  • 使用 tokenStore 删除 token

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

@Component

public class AuthLogoutSuccessHandler1 extends SimpleUrlLogoutSuccessHandler {

    String USER_HAVE_TOKEN = AuthJdbcTokenStore.USER_HAVE_TOKEN;

    @Resource

    RedisTemplate redisTemplate;

    @Resource

    TokenStore tokenStore;

    @Override

    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        if (!Objects.isNull(authentication)) {

            String userName = authentication.getName();

            String userTokensKey = USER_HAVE_TOKEN + userName;

            Long size = redisTemplate.opsForList().size(userTokensKey);

            List<String> list = redisTemplate.opsForList().range(userTokensKey, 0, size);

            for (String tokenValue : list) {

                OAuth3AccessToken token = tokenStore.readAccessToken(tokenValue);

                if (Objects.nonNull(token)) {

                    tokenStore.removeAccessToken(token);

                }

            }

            redisTemplate.delete(userTokensKey);

            super.handle(request, response, authentication);

        }

    }

}

登录后复制

解决登出时长过长

场景:项目运行一段时间后,发现登出时间越来越慢

问题:通过 debug 发现耗时主要在删除 token 那一段

1

tokenStore.removeAccessToken(token);

登录后复制

原因:随着时间推移,token 越来越多,token 存储表 oauth_access_token 变得异常的大,所以删除效率非常差

解决办法:使用其他 TokenStore,或者清除 oauth_access_token 的表数据

以上是SpringBoot Security如何实现单点登出并清除所有token的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
两个点博物馆:所有展览以及在哪里可以找到它们
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

解决kernel_security_check_failure蓝屏的17种方法 解决kernel_security_check_failure蓝屏的17种方法 Feb 12, 2024 pm 08:51 PM

Kernelsecuritycheckfailure(内核检查失败)就是一个比较常见的停止代码类型,可蓝屏错误出现不管是什么原因都让很多的有用户们十分的苦恼,下面就让本站来为用户们来仔细的介绍一下17种解决方法吧。kernel_security_check_failure蓝屏的17种解决方法方法1:移除全部外部设备当您使用的任何外部设备与您的Windows版本不兼容时,则可能会发生Kernelsecuritycheckfailure蓝屏错误。为此,您需要在尝试重新启动计算机之前拔下全部外部设备。

Springboot怎么集成Jasypt实现配置文件加密 Springboot怎么集成Jasypt实现配置文件加密 Jun 01, 2023 am 08:55 AM

Jasypt介绍Jasypt是一个java库,它允许开发员以最少的努力为他/她的项目添加基本的加密功能,并且不需要对加密工作原理有深入的了解用于单向和双向加密的高安全性、基于标准的加密技术。加密密码,文本,数字,二进制文件...适合集成到基于Spring的应用程序中,开放API,用于任何JCE提供程序...添加如下依赖:com.github.ulisesbocchiojasypt-spring-boot-starter2.1.1Jasypt好处保护我们的系统安全,即使代码泄露,也可以保证数据源的

怎么在SpringBoot中使用Redis实现分布式锁 怎么在SpringBoot中使用Redis实现分布式锁 Jun 03, 2023 am 08:16 AM

一、Redis实现分布式锁原理为什么需要分布式锁在聊分布式锁之前,有必要先解释一下,为什么需要分布式锁。与分布式锁相对就的是单机锁,我们在写多线程程序时,避免同时操作一个共享变量产生数据问题,通常会使用一把锁来互斥以保证共享变量的正确性,其使用范围是在同一个进程中。如果换做是多个进程,需要同时操作一个共享资源,如何互斥呢?现在的业务应用通常是微服务架构,这也意味着一个应用会部署多个进程,多个进程如果需要修改MySQL中的同一行记录,为了避免操作乱序导致脏数据,此时就需要引入分布式锁了。想要实现分

登录token无效问题如何解决 登录token无效问题如何解决 Sep 14, 2023 am 10:57 AM

登录token无效问题可以通过检查网络连接、检查token有效期、清除缓存和Cookie、检查登录状态、联系应用程序开发者和加强账号安全来解决。详细介绍:1、检查网络连接,重新连接网络或者更换网络环境;2、检查token有效期,重新获取一个新的token,或者联系应用程序的开发者;3、清除缓存和Cookie,清除浏览器缓存和Cookie,然后重新登录应用程序;4、检查登录状态。

登录token无效怎么办 登录token无效怎么办 Sep 14, 2023 am 11:33 AM

登录token无效的解决办法有检查Token是否过期、检查Token是否正确、检查Token是否被篡改、检查Token是否与用户匹配、清除缓存或Cookie、检查网络连接和服务器状态、重新登录或请求新的Token、联系技术支持或开发人员等。详细介绍:1、检查Token是否过期,登录Token通常会设置有效期,一旦超过有效期,就会被认为无效等等。

springboot读取文件打成jar包后访问不到怎么解决 springboot读取文件打成jar包后访问不到怎么解决 Jun 03, 2023 pm 04:38 PM

springboot读取文件,打成jar包后访问不到最新开发出现一种情况,springboot打成jar包后读取不到文件,原因是打包之后,文件的虚拟路径是无效的,只能通过流去读取。文件在resources下publicvoidtest(){Listnames=newArrayList();InputStreamReaderread=null;try{ClassPathResourceresource=newClassPathResource("name.txt");Input

SpringBoot与SpringMVC的比较及差别分析 SpringBoot与SpringMVC的比较及差别分析 Dec 29, 2023 am 11:02 AM

SpringBoot和SpringMVC都是Java开发中常用的框架,但它们之间有一些明显的差异。本文将探究这两个框架的特点和用途,并对它们的差异进行比较。首先,我们来了解一下SpringBoot。SpringBoot是由Pivotal团队开发的,它旨在简化基于Spring框架的应用程序的创建和部署。它提供了一种快速、轻量级的方式来构建独立的、可执行

SpringBoot怎么自定义Redis实现缓存序列化 SpringBoot怎么自定义Redis实现缓存序列化 Jun 03, 2023 am 11:32 AM

1、自定义RedisTemplate1.1、RedisAPI默认序列化机制基于API的Redis缓存实现是使用RedisTemplate模板进行数据缓存操作的,这里打开RedisTemplate类,查看该类的源码信息publicclassRedisTemplateextendsRedisAccessorimplementsRedisOperations,BeanClassLoaderAware{//声明了key、value的各种序列化方式,初始值为空@NullableprivateRedisSe

See all articles