优化
This commit is contained in:
+8
-2
@@ -12,13 +12,19 @@ import java.security.MessageDigest;
|
||||
|
||||
@Component
|
||||
public class InternalServiceTokenGuard {
|
||||
private static final String INSECURE_PLACEHOLDER =
|
||||
"change-me-to-an-internal-token-at-least-32-bytes-long";
|
||||
|
||||
@Value("${baiyue.security.internal-service-token}") private String token;
|
||||
private byte[] expectedToken;
|
||||
|
||||
@PostConstruct
|
||||
void initializeToken() {
|
||||
if (token == null || token.getBytes(StandardCharsets.UTF_8).length < 32) {
|
||||
throw new IllegalStateException("INTERNAL_SERVICE_TOKEN 至少需要 32 字节");
|
||||
if (token == null
|
||||
|| token.isBlank()
|
||||
|| INSECURE_PLACEHOLDER.equals(token)
|
||||
|| token.getBytes(StandardCharsets.UTF_8).length < 32) {
|
||||
throw new IllegalStateException("INTERNAL_SERVICE_TOKEN 必须配置为至少 32 字节的安全令牌");
|
||||
}
|
||||
this.expectedToken = token.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ spring:
|
||||
|
||||
baiyue:
|
||||
security:
|
||||
internal-service-token: ${INTERNAL_SERVICE_TOKEN:change-me-to-an-internal-token-at-least-32-bytes-long}
|
||||
internal-service-token: ${INTERNAL_SERVICE_TOKEN:}
|
||||
integration:
|
||||
baidu-ocr:
|
||||
enabled: ${BAIDU_OCR_ENABLED:false}
|
||||
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baiyue.integration.security;
|
||||
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
class InternalServiceTokenGuardTest {
|
||||
private static final String VALID_TOKEN = "integration-service-test-token-32-bytes";
|
||||
|
||||
@Test
|
||||
void rejectsMissingEmptyAndShortTokens() {
|
||||
assertInvalidConfiguration(null);
|
||||
assertInvalidConfiguration("");
|
||||
assertInvalidConfiguration(" ");
|
||||
assertInvalidConfiguration("too-short");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsKnownInsecurePlaceholder() {
|
||||
assertInvalidConfiguration("change-me-to-an-internal-token-at-least-32-bytes-long");
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsConfiguredTokenWithAtLeastThirtyTwoBytes() {
|
||||
InternalServiceTokenGuard guard = guardWithToken(VALID_TOKEN);
|
||||
|
||||
assertThatCode(guard::initializeToken).doesNotThrowAnyException();
|
||||
assertThatCode(() -> guard.verify(VALID_TOKEN)).doesNotThrowAnyException();
|
||||
assertThatThrownBy(() -> guard.verify("different-internal-service-token"))
|
||||
.isInstanceOf(BizException.class);
|
||||
}
|
||||
|
||||
private static void assertInvalidConfiguration(String token) {
|
||||
InternalServiceTokenGuard guard = guardWithToken(token);
|
||||
|
||||
assertThatThrownBy(guard::initializeToken)
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("INTERNAL_SERVICE_TOKEN");
|
||||
}
|
||||
|
||||
private static InternalServiceTokenGuard guardWithToken(String token) {
|
||||
InternalServiceTokenGuard guard = new InternalServiceTokenGuard();
|
||||
ReflectionTestUtils.setField(guard, "token", token);
|
||||
return guard;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user