forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: invalidate all sessions of a user after password changed (halo-…
…dev#5757) * feat: invalidate all sessions of a user after password changed * fix: unit test case * refactor: use spring session 3.3 to adapt * refactor: compatible with session timeout configuration * refactor: indexed session repository * Reload page after changed the password Signed-off-by: Ryan Wang <i@ryanc.cc> * chore: update session repository --------- Signed-off-by: Ryan Wang <i@ryanc.cc> Co-authored-by: Ryan Wang <i@ryanc.cc>
- Loading branch information
Showing
10 changed files
with
348 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
application/src/main/java/run/halo/app/event/user/PasswordChangedEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package run.halo.app.event.user; | ||
|
||
import lombok.Getter; | ||
import org.springframework.context.ApplicationEvent; | ||
|
||
@Getter | ||
public class PasswordChangedEvent extends ApplicationEvent { | ||
private final String username; | ||
|
||
public PasswordChangedEvent(Object source, String username) { | ||
super(source); | ||
this.username = username; | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
...src/main/java/run/halo/app/security/session/InMemoryReactiveIndexedSessionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package run.halo.app.security.session; | ||
|
||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import java.time.Duration; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import org.springframework.beans.factory.DisposableBean; | ||
import org.springframework.session.DelegatingIndexResolver; | ||
import org.springframework.session.IndexResolver; | ||
import org.springframework.session.MapSession; | ||
import org.springframework.session.PrincipalNameIndexResolver; | ||
import org.springframework.session.ReactiveMapSessionRepository; | ||
import org.springframework.session.Session; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class InMemoryReactiveIndexedSessionRepository extends ReactiveMapSessionRepository | ||
implements ReactiveIndexedSessionRepository<MapSession>, DisposableBean { | ||
|
||
final IndexResolver<MapSession> indexResolver = | ||
new DelegatingIndexResolver<>(new PrincipalNameIndexResolver<>(PRINCIPAL_NAME_INDEX_NAME)); | ||
|
||
private final ConcurrentMap<String, Set<IndexKey>> sessionIdIndexMap = | ||
new ConcurrentHashMap<>(); | ||
private final ConcurrentMap<IndexKey, Set<String>> indexSessionIdMap = | ||
new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Prevent other requests from being parsed and acquiring the session during its deletion, | ||
* which could result in an unintended renewal. Currently, it acts as a buffer, and having a | ||
* slightly prolonged expiration period is sufficient. | ||
*/ | ||
private final Cache<String, Boolean> invalidateSessionIds = CacheBuilder.newBuilder() | ||
.expireAfterWrite(Duration.ofMinutes(10)) | ||
.maximumSize(10_000) | ||
.build(); | ||
|
||
public InMemoryReactiveIndexedSessionRepository(Map<String, Session> sessions) { | ||
super(sessions); | ||
} | ||
|
||
@Override | ||
public Mono<Void> save(MapSession session) { | ||
if (invalidateSessionIds.getIfPresent(session.getId()) != null) { | ||
return this.deleteById(session.getId()); | ||
} | ||
return super.save(session) | ||
.then(updateIndex(session)); | ||
} | ||
|
||
@Override | ||
public Mono<Void> deleteById(String id) { | ||
return removeIndex(id) | ||
.then(Mono.defer(() -> { | ||
invalidateSessionIds.put(id, true); | ||
return super.deleteById(id); | ||
})); | ||
} | ||
|
||
@Override | ||
public Mono<Map<String, MapSession>> findByIndexNameAndIndexValue(String indexName, | ||
String indexValue) { | ||
var indexKey = new IndexKey(indexName, indexValue); | ||
return Flux.fromStream((() -> indexSessionIdMap.getOrDefault(indexKey, Set.of()).stream())) | ||
.flatMap(this::findById) | ||
.collectMap(Session::getId); | ||
} | ||
|
||
@Override | ||
public Mono<Map<String, MapSession>> findByPrincipalName(String principalName) { | ||
return this.findByIndexNameAndIndexValue(PRINCIPAL_NAME_INDEX_NAME, principalName); | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
sessionIdIndexMap.clear(); | ||
indexSessionIdMap.clear(); | ||
invalidateSessionIds.invalidateAll(); | ||
} | ||
|
||
Mono<Void> removeIndex(String sessionId) { | ||
return getIndexes(sessionId) | ||
.doOnNext(indexKey -> indexSessionIdMap.computeIfPresent(indexKey, | ||
(key, sessionIdSet) -> { | ||
sessionIdSet.remove(sessionId); | ||
return sessionIdSet.isEmpty() ? null : sessionIdSet; | ||
}) | ||
) | ||
.then(Mono.defer(() -> { | ||
sessionIdIndexMap.remove(sessionId); | ||
return Mono.empty(); | ||
})) | ||
.then(); | ||
} | ||
|
||
Mono<Void> updateIndex(MapSession session) { | ||
return removeIndex(session.getId()) | ||
.then(Mono.defer(() -> { | ||
indexResolver.resolveIndexesFor(session) | ||
.forEach((name, value) -> { | ||
IndexKey indexKey = new IndexKey(name, value); | ||
indexSessionIdMap.computeIfAbsent(indexKey, | ||
unusedSet -> ConcurrentHashMap.newKeySet()) | ||
.add(session.getId()); | ||
// Update sessionIdIndexMap | ||
sessionIdIndexMap.computeIfAbsent(session.getId(), | ||
unusedSet -> ConcurrentHashMap.newKeySet()) | ||
.add(indexKey); | ||
}); | ||
return Mono.empty(); | ||
})) | ||
.then(); | ||
} | ||
|
||
Flux<IndexKey> getIndexes(String sessionId) { | ||
return Flux.fromIterable(sessionIdIndexMap.getOrDefault(sessionId, Set.of())); | ||
} | ||
|
||
/** | ||
* For testing purpose. | ||
*/ | ||
ConcurrentMap<String, Set<IndexKey>> getSessionIdIndexMap() { | ||
return sessionIdIndexMap; | ||
} | ||
|
||
/** | ||
* For testing purpose. | ||
*/ | ||
ConcurrentMap<IndexKey, Set<String>> getIndexSessionIdMap() { | ||
return indexSessionIdMap; | ||
} | ||
|
||
record IndexKey(String attributeName, String attributeValue) { | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ication/src/main/java/run/halo/app/security/session/ReactiveIndexedSessionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package run.halo.app.security.session; | ||
|
||
import org.springframework.session.ReactiveFindByIndexNameSessionRepository; | ||
import org.springframework.session.ReactiveSessionRepository; | ||
import org.springframework.session.Session; | ||
|
||
public interface ReactiveIndexedSessionRepository<S extends Session> | ||
extends ReactiveSessionRepository<S>, ReactiveFindByIndexNameSessionRepository<S> { | ||
} |
38 changes: 38 additions & 0 deletions
38
application/src/main/java/run/halo/app/security/session/SessionInvalidationListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package run.halo.app.security.session; | ||
|
||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.session.ReactiveFindByIndexNameSessionRepository; | ||
import org.springframework.session.ReactiveSessionRepository; | ||
import org.springframework.session.Session; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Flux; | ||
import run.halo.app.event.user.PasswordChangedEvent; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class SessionInvalidationListener { | ||
|
||
private final ReactiveFindByIndexNameSessionRepository<? extends Session> | ||
indexedSessionRepository; | ||
private final ReactiveSessionRepository<? extends Session> sessionRepository; | ||
|
||
@Async | ||
@EventListener | ||
public void onPasswordChanged(PasswordChangedEvent event) { | ||
String username = event.getUsername(); | ||
// Invalidate session | ||
invalidateUserSessions(username); | ||
} | ||
|
||
private void invalidateUserSessions(String username) { | ||
indexedSessionRepository.findByPrincipalName(username) | ||
.map(Map::keySet) | ||
.flatMapMany(Flux::fromIterable) | ||
.flatMap(sessionRepository::deleteById) | ||
.then() | ||
.block(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.