我相信这是一个错误。但是,我找到了一种解决方法:只需将 AwsVerifier 子类化并覆盖 verify() 方法。确保将代码从超类 verify() 复制到子类中,但按如下方式更改:
public class NewAwsVerifier extends AwsVerifier {
public NewAwsVerifier(LocalVerifier wrappedVerifier) {
super(wrappedVerifier);
}
...
@Override
public int verify(Request request, Response response) {
...
char[] userSecret = getLocalSecret(userId);
if (userSecret == null) {
// If there is no userSecret for the given userId then the
// request probably specified a user that doesn't exist
// and using that userID in the getS3Signature call
// will result in a NullPointerException, so we intercept it here
return RESULT_INVALID;
}
char[] signature = getSecret(request, response);
String sigToCompare = AwsUtils.getS3Signature(request, userSecret);
...
}
}
然后确保你使用这个新的验证器:
MapVerifier verifier = new MapVerifier();
NewAwsVerifier newVerifier = new NewAwsVerifier(verifier);
// Get passwords from a more secure source (only here for illustration)!
verifier.getLocalSecrets().put("accessKey", "secretKey".toCharArray());
auth.setVerifier(newVerifier);
现在,如果您指定服务器机密中不存在的访问密钥,您将被优雅地拒绝访问,而不是收到 HTTP 500 内部服务器错误。