暂存提交
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
package com.baiyue.warehouse.security;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
@Component
|
||||
public class InternalServiceTokenGuard {
|
||||
private final byte[] expected;
|
||||
|
||||
public InternalServiceTokenGuard(
|
||||
@Value("${baiyue.internal-service.token:phase1-local-internal-token-at-least-32-bytes}") String token) {
|
||||
this.expected = token.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public void verify(String provided) {
|
||||
byte[] actual = provided == null ? new byte[0] : provided.getBytes(StandardCharsets.UTF_8);
|
||||
if (!MessageDigest.isEqual(expected, actual)) {
|
||||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid internal service token");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user