diff --git a/auth/api/src/main/java/com/tencent/supersonic/auth/api/authentication/config/AuthenticationConfig.java b/auth/api/src/main/java/com/tencent/supersonic/auth/api/authentication/config/AuthenticationConfig.java index c014524c6..e1d2b3cc9 100644 --- a/auth/api/src/main/java/com/tencent/supersonic/auth/api/authentication/config/AuthenticationConfig.java +++ b/auth/api/src/main/java/com/tencent/supersonic/auth/api/authentication/config/AuthenticationConfig.java @@ -18,6 +18,9 @@ public class AuthenticationConfig { @Value("${s2.authentication.include.path:/api}") private String includePath; + @Value("${s2.authentication.strategy:http}") + private String strategy; + @Value("${s2.authentication.enable:false}") private boolean enabled; diff --git a/auth/api/src/main/java/com/tencent/supersonic/auth/api/authentication/service/UserStrategy.java b/auth/api/src/main/java/com/tencent/supersonic/auth/api/authentication/service/UserStrategy.java index 60a1130b9..0ddb6567a 100644 --- a/auth/api/src/main/java/com/tencent/supersonic/auth/api/authentication/service/UserStrategy.java +++ b/auth/api/src/main/java/com/tencent/supersonic/auth/api/authentication/service/UserStrategy.java @@ -7,6 +7,8 @@ import com.tencent.supersonic.common.pojo.User; public interface UserStrategy { + String getStrategyName(); + boolean accept(boolean isEnableAuthentication); User findUser(HttpServletRequest request, HttpServletResponse response); diff --git a/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/FakeUserStrategy.java b/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/FakeUserStrategy.java index 576f56d54..8008e2c24 100644 --- a/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/FakeUserStrategy.java +++ b/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/FakeUserStrategy.java @@ -10,6 +10,13 @@ import org.springframework.stereotype.Service; @Service public class FakeUserStrategy implements UserStrategy { + public static final String STRATEGY_NAME = "fake"; + + @Override + public String getStrategyName() { + return STRATEGY_NAME; + } + @Override public boolean accept(boolean isEnableAuthentication) { return !isEnableAuthentication; diff --git a/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/HttpHeaderUserStrategy.java b/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/HttpHeaderUserStrategy.java index 970392724..9d274964b 100644 --- a/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/HttpHeaderUserStrategy.java +++ b/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/HttpHeaderUserStrategy.java @@ -15,12 +15,18 @@ import java.util.Optional; @Service public class HttpHeaderUserStrategy implements UserStrategy { + public static final String STRATEGY_NAME = "http"; private final TokenService tokenService; public HttpHeaderUserStrategy(TokenService tokenService) { this.tokenService = tokenService; } + @Override + public String getStrategyName() { + return STRATEGY_NAME; + } + @Override public boolean accept(boolean isEnableAuthentication) { return isEnableAuthentication; diff --git a/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/UserStrategyFactory.java b/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/UserStrategyFactory.java index b7becf8cd..59e6db441 100644 --- a/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/UserStrategyFactory.java +++ b/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/strategy/UserStrategyFactory.java @@ -9,6 +9,7 @@ import lombok.Data; import org.springframework.context.annotation.Configuration; import java.util.List; +import java.util.Optional; @Configuration @Data @@ -26,10 +27,26 @@ public class UserStrategyFactory { @PostConstruct public void setUserStrategy() { - for (UserStrategy userStrategy : userStrategyList) { - if (userStrategy.accept(authenticationConfig.isEnabled())) { - UserHolder.setStrategy(userStrategy); + + boolean enabled = authenticationConfig.isEnabled(); + if (!enabled) { + for (UserStrategy userStrategy : userStrategyList) { + if (userStrategy.accept(authenticationConfig.isEnabled())) { + UserHolder.setStrategy(userStrategy); + } } + return; + } + + String strategy = authenticationConfig.getStrategy(); + Optional strategyOptional = userStrategyList.stream() + .filter(t -> t.accept(true) && strategy.equalsIgnoreCase(t.getStrategyName())) + .findAny(); + + if (strategyOptional.isPresent()) { + UserHolder.setStrategy(strategyOptional.get()); + } else { + throw new IllegalStateException("strategy is not found: " + strategy); } } }