|
1 | 1 | /*
|
2 |
| - * Copyright 2014-2020 the original author or authors. |
| 2 | + * Copyright 2014-2021 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
@@ -473,6 +473,60 @@ void findByChangedSecurityPrincipalNameReload() {
|
473 | 473 | assertThat(findByPrincipalName.keySet()).containsOnly(toSave.getId());
|
474 | 474 | }
|
475 | 475 |
|
| 476 | + @Test // gh-1791 |
| 477 | + void changeSessionIdWhenSessionExpiresThenRemovesAllPrincipalIndexIds() { |
| 478 | + RedisSession toSave = this.repository.createSession(); |
| 479 | + toSave.setAttribute(SPRING_SECURITY_CONTEXT, this.context); |
| 480 | + |
| 481 | + this.repository.save(toSave); |
| 482 | + String usernameSessionKey = "RedisIndexedSessionRepositoryITests:index:" + INDEX_NAME + ":" + getSecurityName(); |
| 483 | + |
| 484 | + RedisSession findById = this.repository.findById(toSave.getId()); |
| 485 | + String originalFindById = findById.getId(); |
| 486 | + |
| 487 | + assertThat(this.redis.boundSetOps(usernameSessionKey).members()).contains(originalFindById); |
| 488 | + |
| 489 | + String changeSessionId = findById.changeSessionId(); |
| 490 | + findById.setAttribute(SPRING_SECURITY_CONTEXT, this.context); |
| 491 | + |
| 492 | + this.repository.save(findById); |
| 493 | + |
| 494 | + assertThat(this.redis.boundSetOps(usernameSessionKey).members()).contains(changeSessionId); |
| 495 | + |
| 496 | + String body = "RedisIndexedSessionRepositoryITests:sessions:expires:" + changeSessionId; |
| 497 | + String channel = "__keyevent@0__:expired"; |
| 498 | + DefaultMessage message = new DefaultMessage(channel.getBytes(StandardCharsets.UTF_8), |
| 499 | + body.getBytes(StandardCharsets.UTF_8)); |
| 500 | + byte[] pattern = new byte[] {}; |
| 501 | + this.repository.onMessage(message, pattern); |
| 502 | + |
| 503 | + assertThat(this.redis.boundSetOps(usernameSessionKey).members()).isEmpty(); |
| 504 | + } |
| 505 | + |
| 506 | + @Test |
| 507 | + void changeSessionIdWhenPrincipalNameChangesThenNewPrincipalMapsToNewSessionId() { |
| 508 | + String principalName = "findByChangedPrincipalName" + UUID.randomUUID(); |
| 509 | + String principalNameChanged = "findByChangedPrincipalName" + UUID.randomUUID(); |
| 510 | + RedisSession toSave = this.repository.createSession(); |
| 511 | + toSave.setAttribute(INDEX_NAME, principalName); |
| 512 | + |
| 513 | + this.repository.save(toSave); |
| 514 | + |
| 515 | + RedisSession findById = this.repository.findById(toSave.getId()); |
| 516 | + String changeSessionId = findById.changeSessionId(); |
| 517 | + findById.setAttribute(INDEX_NAME, principalNameChanged); |
| 518 | + this.repository.save(findById); |
| 519 | + |
| 520 | + Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, |
| 521 | + principalName); |
| 522 | + assertThat(findByPrincipalName).isEmpty(); |
| 523 | + |
| 524 | + findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalNameChanged); |
| 525 | + |
| 526 | + assertThat(findByPrincipalName).hasSize(1); |
| 527 | + assertThat(findByPrincipalName.keySet()).containsOnly(changeSessionId); |
| 528 | + } |
| 529 | + |
476 | 530 | @Test
|
477 | 531 | void changeSessionIdWhenOnlyChangeId() {
|
478 | 532 | String attrName = "changeSessionId";
|
|
0 commit comments