mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-05-07 21:55:57 +08:00
refactor: 优化过期时间处理
This commit is contained in:
@@ -3,34 +3,20 @@ package org.hswebframework.web.authorization.basic.configuration;
|
||||
import org.hswebframework.web.authorization.AuthenticationManager;
|
||||
import org.hswebframework.web.authorization.ReactiveAuthenticationManagerProvider;
|
||||
import org.hswebframework.web.authorization.access.DataAccessController;
|
||||
import org.hswebframework.web.authorization.access.DataAccessHandler;
|
||||
import org.hswebframework.web.authorization.basic.aop.AopMethodAuthorizeDefinitionParser;
|
||||
import org.hswebframework.web.authorization.basic.embed.EmbedAuthenticationProperties;
|
||||
import org.hswebframework.web.authorization.basic.embed.EmbedReactiveAuthenticationManager;
|
||||
import org.hswebframework.web.authorization.basic.handler.AuthorizationLoginLoggerInfoHandler;
|
||||
import org.hswebframework.web.authorization.basic.handler.DefaultAuthorizingHandler;
|
||||
import org.hswebframework.web.authorization.basic.handler.UserAllowPermissionHandler;
|
||||
import org.hswebframework.web.authorization.basic.handler.access.DefaultDataAccessController;
|
||||
import org.hswebframework.web.authorization.basic.twofactor.TwoFactorHandlerInterceptorAdapter;
|
||||
import org.hswebframework.web.authorization.basic.web.*;
|
||||
import org.hswebframework.web.authorization.token.UserTokenManager;
|
||||
import org.hswebframework.web.authorization.twofactor.TwoFactorValidatorManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.*;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 权限控制自动配置类
|
||||
@@ -94,26 +80,6 @@ public class AuthorizingHandlerAutoConfiguration {
|
||||
return new BearerTokenParser();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class DataAccessHandlerProcessor implements BeanPostProcessor {
|
||||
|
||||
@Autowired
|
||||
private DefaultDataAccessController defaultDataAccessController;
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) {
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) {
|
||||
if (bean instanceof DataAccessHandler) {
|
||||
defaultDataAccessController.addHandler(((DataAccessHandler) bean));
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "hsweb.authorize", name = "basic-authorization", havingValue = "true")
|
||||
|
||||
@@ -43,41 +43,46 @@ public class UserTokenWebFilter implements WebFilter, BeanPostProcessor {
|
||||
public Mono<Void> filter(@NonNull ServerWebExchange exchange, WebFilterChain chain) {
|
||||
|
||||
return Flux
|
||||
.fromIterable(parsers)
|
||||
.flatMap(parser -> parser.parseToken(exchange))
|
||||
.next()
|
||||
.map(token -> chain
|
||||
.filter(exchange)
|
||||
.contextWrite(Context.of(ParsedToken.class, token)))
|
||||
.defaultIfEmpty(chain.filter(exchange))
|
||||
.flatMap(Function.identity())
|
||||
.contextWrite(ReactiveLogger.start("requestId", exchange.getRequest().getId()));
|
||||
.fromIterable(parsers)
|
||||
.flatMap(parser -> parser.parseToken(exchange))
|
||||
.next()
|
||||
.map(token -> chain
|
||||
.filter(exchange)
|
||||
.contextWrite(Context.of(ParsedToken.class, token)))
|
||||
.defaultIfEmpty(chain.filter(exchange))
|
||||
.flatMap(Function.identity())
|
||||
.contextWrite(ReactiveLogger.start("requestId", exchange.getRequest().getId()));
|
||||
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void handleUserSign(AuthorizationSuccessEvent event) {
|
||||
ReactiveUserTokenGenerator generator = event
|
||||
.<String>getParameter("tokenType")
|
||||
.map(tokenGeneratorMap::get)
|
||||
.orElseGet(() -> tokenGeneratorMap.get("default"));
|
||||
.<String>getParameter("tokenType")
|
||||
.map(tokenGeneratorMap::get)
|
||||
.orElseGet(() -> tokenGeneratorMap.get("default"));
|
||||
if (generator != null) {
|
||||
GeneratedToken token = generator.generate(event.getAuthentication());
|
||||
event.getResult().putAll(token.getResponse());
|
||||
if (StringUtils.hasText(token.getToken())) {
|
||||
event.getResult().put("token", token.getToken());
|
||||
long expires = event.getParameter("expires")
|
||||
.map(String::valueOf)
|
||||
.map(Long::parseLong)
|
||||
.orElse(token.getTimeout());
|
||||
event.getResult().put("expires", expires);
|
||||
event.async(userTokenManager
|
||||
.signIn(token.getToken(), token.getType(), event
|
||||
.getAuthentication()
|
||||
.getUser()
|
||||
.getId(), expires)
|
||||
.doOnNext(t -> log.debug("user [{}] sign in", t.getUserId()))
|
||||
.then());
|
||||
long expires = event
|
||||
.getParameter("expires")
|
||||
.map(String::valueOf)
|
||||
.map(Long::parseLong)
|
||||
.orElse(token.getTimeout());
|
||||
|
||||
event.async(
|
||||
userTokenManager
|
||||
.signIn(token.getToken(), token.getType(), event
|
||||
.getAuthentication()
|
||||
.getUser()
|
||||
.getId(), expires)
|
||||
.doOnNext(t -> {
|
||||
event.getResult().put("expires", t.getMaxInactiveInterval());
|
||||
log.debug("user [{}] sign in", t.getUserId());
|
||||
})
|
||||
.then());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user