mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 11:07:06 +00:00
(improvement)(auth) 增强UserStrategy的可配置性 (#1949)
This commit is contained in:
@@ -18,6 +18,9 @@ public class AuthenticationConfig {
|
|||||||
@Value("${s2.authentication.include.path:/api}")
|
@Value("${s2.authentication.include.path:/api}")
|
||||||
private String includePath;
|
private String includePath;
|
||||||
|
|
||||||
|
@Value("${s2.authentication.strategy:http}")
|
||||||
|
private String strategy;
|
||||||
|
|
||||||
@Value("${s2.authentication.enable:false}")
|
@Value("${s2.authentication.enable:false}")
|
||||||
private boolean enabled;
|
private boolean enabled;
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import com.tencent.supersonic.common.pojo.User;
|
|||||||
|
|
||||||
public interface UserStrategy {
|
public interface UserStrategy {
|
||||||
|
|
||||||
|
String getStrategyName();
|
||||||
|
|
||||||
boolean accept(boolean isEnableAuthentication);
|
boolean accept(boolean isEnableAuthentication);
|
||||||
|
|
||||||
User findUser(HttpServletRequest request, HttpServletResponse response);
|
User findUser(HttpServletRequest request, HttpServletResponse response);
|
||||||
|
|||||||
@@ -10,6 +10,13 @@ import org.springframework.stereotype.Service;
|
|||||||
@Service
|
@Service
|
||||||
public class FakeUserStrategy implements UserStrategy {
|
public class FakeUserStrategy implements UserStrategy {
|
||||||
|
|
||||||
|
public static final String STRATEGY_NAME = "fake";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getStrategyName() {
|
||||||
|
return STRATEGY_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(boolean isEnableAuthentication) {
|
public boolean accept(boolean isEnableAuthentication) {
|
||||||
return !isEnableAuthentication;
|
return !isEnableAuthentication;
|
||||||
|
|||||||
@@ -15,12 +15,18 @@ import java.util.Optional;
|
|||||||
@Service
|
@Service
|
||||||
public class HttpHeaderUserStrategy implements UserStrategy {
|
public class HttpHeaderUserStrategy implements UserStrategy {
|
||||||
|
|
||||||
|
public static final String STRATEGY_NAME = "http";
|
||||||
private final TokenService tokenService;
|
private final TokenService tokenService;
|
||||||
|
|
||||||
public HttpHeaderUserStrategy(TokenService tokenService) {
|
public HttpHeaderUserStrategy(TokenService tokenService) {
|
||||||
this.tokenService = tokenService;
|
this.tokenService = tokenService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getStrategyName() {
|
||||||
|
return STRATEGY_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(boolean isEnableAuthentication) {
|
public boolean accept(boolean isEnableAuthentication) {
|
||||||
return isEnableAuthentication;
|
return isEnableAuthentication;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import lombok.Data;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Data
|
@Data
|
||||||
@@ -26,10 +27,26 @@ public class UserStrategyFactory {
|
|||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void setUserStrategy() {
|
public void setUserStrategy() {
|
||||||
for (UserStrategy userStrategy : userStrategyList) {
|
|
||||||
if (userStrategy.accept(authenticationConfig.isEnabled())) {
|
boolean enabled = authenticationConfig.isEnabled();
|
||||||
UserHolder.setStrategy(userStrategy);
|
if (!enabled) {
|
||||||
|
for (UserStrategy userStrategy : userStrategyList) {
|
||||||
|
if (userStrategy.accept(authenticationConfig.isEnabled())) {
|
||||||
|
UserHolder.setStrategy(userStrategy);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String strategy = authenticationConfig.getStrategy();
|
||||||
|
Optional<UserStrategy> 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user