mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-06-05 04:13:20 +08:00
refactor: 优化token相关事件为异步事件,方便传递上下文 (#287)
This commit is contained in:
@@ -26,7 +26,6 @@ import org.hswebframework.web.authorization.token.event.UserTokenChangedEvent;
|
||||
import org.hswebframework.web.authorization.token.event.UserTokenCreatedEvent;
|
||||
import org.hswebframework.web.authorization.token.event.UserTokenRemovedEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -170,42 +169,39 @@ public class DefaultUserTokenManager implements UserTokenManager {
|
||||
}
|
||||
return Mono.defer(() -> {
|
||||
Set<String> tokens = getUserToken(userId);
|
||||
tokens.forEach(token -> signOutByToken(token, false));
|
||||
tokens.clear();
|
||||
userStorage.remove(userId);
|
||||
return Mono.empty();
|
||||
return Flux
|
||||
.fromIterable(tokens)
|
||||
.flatMap(token -> signOutByToken(token, false))
|
||||
.then(Mono.fromRunnable(() -> {
|
||||
tokens.clear();
|
||||
userStorage.remove(userId);
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
private void signOutByToken(String token, boolean removeUserToken) {
|
||||
if (token == null) {
|
||||
return;
|
||||
}
|
||||
LocalUserToken tokenObject = tokenStorage.remove(token);
|
||||
if (tokenObject != null) {
|
||||
String userId = tokenObject.getUserId();
|
||||
if (removeUserToken) {
|
||||
Set<String> tokens = getUserToken(userId);
|
||||
if (!tokens.isEmpty()) {
|
||||
tokens.remove(token);
|
||||
}
|
||||
if (tokens.isEmpty()) {
|
||||
userStorage.remove(tokenObject.getUserId());
|
||||
private Mono<Void> signOutByToken(String token, boolean removeUserToken) {
|
||||
if (token != null) {
|
||||
LocalUserToken tokenObject = tokenStorage.remove(token);
|
||||
if (tokenObject != null) {
|
||||
String userId = tokenObject.getUserId();
|
||||
if (removeUserToken) {
|
||||
Set<String> tokens = getUserToken(userId);
|
||||
if (!tokens.isEmpty()) {
|
||||
tokens.remove(token);
|
||||
}
|
||||
if (tokens.isEmpty()) {
|
||||
userStorage.remove(tokenObject.getUserId());
|
||||
}
|
||||
}
|
||||
return new UserTokenRemovedEvent(tokenObject).publish(eventPublisher);
|
||||
}
|
||||
publishEvent(new UserTokenRemovedEvent(tokenObject));
|
||||
}
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> signOutByToken(String token) {
|
||||
return Mono.fromRunnable(() -> signOutByToken(token, true));
|
||||
}
|
||||
|
||||
protected void publishEvent(ApplicationEvent event) {
|
||||
if (null != eventPublisher) {
|
||||
eventPublisher.publishEvent(event);
|
||||
}
|
||||
return signOutByToken(token, true);
|
||||
}
|
||||
|
||||
public Mono<Void> changeTokenState(UserToken userToken, TokenState state) {
|
||||
@@ -216,7 +212,7 @@ public class DefaultUserTokenManager implements UserTokenManager {
|
||||
token.setState(state);
|
||||
syncToken(userToken);
|
||||
|
||||
publishEvent(new UserTokenChangedEvent(copy, userToken));
|
||||
return new UserTokenChangedEvent(copy, userToken).publish(eventPublisher);
|
||||
}
|
||||
return Mono.empty();
|
||||
}
|
||||
@@ -250,13 +246,13 @@ public class DefaultUserTokenManager implements UserTokenManager {
|
||||
detail.setType(type);
|
||||
detail.setMaxInactiveInterval(maxInactiveInterval);
|
||||
detail.setState(TokenState.normal);
|
||||
Runnable doSign = () -> {
|
||||
Mono<Void> doSign = Mono.defer(() -> {
|
||||
tokenStorage.put(token, detail);
|
||||
|
||||
getUserToken(userId).add(token);
|
||||
|
||||
publishEvent(new UserTokenCreatedEvent(detail));
|
||||
};
|
||||
return new UserTokenCreatedEvent(detail).publish(eventPublisher);
|
||||
});
|
||||
AllopatricLoginMode mode = allopatricLoginModes.getOrDefault(type, allopatricLoginMode);
|
||||
if (mode == AllopatricLoginMode.deny) {
|
||||
return getByUserId(userId)
|
||||
@@ -268,17 +264,16 @@ public class DefaultUserTokenManager implements UserTokenManager {
|
||||
}
|
||||
return Mono.empty();
|
||||
})
|
||||
.then(Mono.just(detail))
|
||||
.doOnNext(__ -> doSign.run());
|
||||
.then(doSign)
|
||||
.thenReturn(detail);
|
||||
} else if (mode == AllopatricLoginMode.offlineOther) {
|
||||
return getByUserId(userId)
|
||||
.filter(userToken -> type.equals(userToken.getType()))
|
||||
.flatMap(userToken -> changeTokenState(userToken, TokenState.offline))
|
||||
.then(Mono.just(detail))
|
||||
.doOnNext(__ -> doSign.run());
|
||||
.then(doSign)
|
||||
.thenReturn(detail);
|
||||
}
|
||||
doSign.run();
|
||||
return Mono.just(detail);
|
||||
return doSign.thenReturn(detail);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -2,13 +2,12 @@ package org.hswebframework.web.authorization.token.event;
|
||||
|
||||
import org.hswebframework.web.authorization.events.AuthorizationEvent;
|
||||
import org.hswebframework.web.authorization.token.UserToken;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.hswebframework.web.event.DefaultAsyncEvent;
|
||||
|
||||
public class UserTokenChangedEvent extends ApplicationEvent implements AuthorizationEvent {
|
||||
public class UserTokenChangedEvent extends DefaultAsyncEvent implements AuthorizationEvent {
|
||||
private final UserToken before, after;
|
||||
|
||||
public UserTokenChangedEvent(UserToken before, UserToken after) {
|
||||
super(after);
|
||||
this.before = before;
|
||||
this.after = after;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
package org.hswebframework.web.authorization.token.event;
|
||||
|
||||
import org.hswebframework.web.authorization.token.UserToken;
|
||||
import org.hswebframework.web.authorization.events.AuthorizationEvent;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.hswebframework.web.authorization.token.UserToken;
|
||||
import org.hswebframework.web.event.DefaultAsyncEvent;
|
||||
|
||||
public class UserTokenCreatedEvent extends ApplicationEvent implements AuthorizationEvent {
|
||||
public class UserTokenCreatedEvent extends DefaultAsyncEvent implements AuthorizationEvent {
|
||||
private final UserToken detail;
|
||||
|
||||
public UserTokenCreatedEvent(UserToken detail) {
|
||||
super(detail);
|
||||
this.detail = detail;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,17 +2,19 @@ package org.hswebframework.web.authorization.token.event;
|
||||
|
||||
import org.hswebframework.web.authorization.events.AuthorizationEvent;
|
||||
import org.hswebframework.web.authorization.token.UserToken;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.hswebframework.web.event.DefaultAsyncEvent;
|
||||
|
||||
public class UserTokenRemovedEvent extends ApplicationEvent implements AuthorizationEvent {
|
||||
public class UserTokenRemovedEvent extends DefaultAsyncEvent implements AuthorizationEvent {
|
||||
|
||||
private static final long serialVersionUID = -6662943150068863177L;
|
||||
|
||||
private final UserToken token;
|
||||
|
||||
public UserTokenRemovedEvent(UserToken token) {
|
||||
super(token);
|
||||
this.token=token;
|
||||
}
|
||||
|
||||
public UserToken getDetail() {
|
||||
return ((UserToken) getSource());
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -345,8 +345,9 @@ public class RedisUserTokenManager implements UserTokenManager {
|
||||
if (eventPublisher == null) {
|
||||
return notifyTokenRemoved(token.getToken());
|
||||
}
|
||||
return Mono.fromRunnable(() -> eventPublisher.publishEvent(new UserTokenRemovedEvent(token)))
|
||||
.then(notifyTokenRemoved(token.getToken()));
|
||||
return new UserTokenRemovedEvent(token)
|
||||
.publish(eventPublisher)
|
||||
.then(notifyTokenRemoved(token.getToken()));
|
||||
}
|
||||
|
||||
private Mono<Void> onTokenChanged(UserToken old, SimpleUserToken newToken) {
|
||||
@@ -354,7 +355,9 @@ public class RedisUserTokenManager implements UserTokenManager {
|
||||
if (eventPublisher == null) {
|
||||
return notifyTokenRemoved(newToken.getToken());
|
||||
}
|
||||
return Mono.fromRunnable(() -> eventPublisher.publishEvent(new UserTokenChangedEvent(old, newToken)));
|
||||
return new UserTokenChangedEvent(old, newToken)
|
||||
.publish(eventPublisher)
|
||||
.then(notifyTokenRemoved(newToken.getToken()));
|
||||
}
|
||||
|
||||
private Mono<UserToken> onUserTokenCreated(SimpleUserToken token) {
|
||||
@@ -363,10 +366,10 @@ public class RedisUserTokenManager implements UserTokenManager {
|
||||
return notifyTokenRemoved(token.getToken())
|
||||
.thenReturn(token);
|
||||
}
|
||||
return Mono
|
||||
.fromRunnable(() -> eventPublisher.publishEvent(new UserTokenCreatedEvent(token)))
|
||||
.then(notifyTokenRemoved(token.getToken()))
|
||||
.thenReturn(token);
|
||||
return new UserTokenCreatedEvent(token)
|
||||
.publish(eventPublisher)
|
||||
.then(notifyTokenRemoved(token.getToken()))
|
||||
.thenReturn(token);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user