AccessKeyAuthenticator implements Authenticator { @Override public void authenticate(Invocation invocation, URL url) throws RpcAuthenticationException { String accessKeyId = String.valueOf(invocation.getAttachment(Constants.AK_KEY)); String requestTimestamp = String.valueOf(invocation.getAttachment(Constants.REQUEST_TIMESTAMP_KEY)); String originSignature = String.valueOf(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY)); String consumer = String.valueOf(invocation.getAttachment(CommonConstants.CONSUMER)); if (StringUtils.isEmpty(accessKeyId) || StringUtils.isEmpty(consumer) || StringUtils.isEmpty(requestTimestamp) || StringUtils.isEmpty(originSignature)) { throw new RpcAuthenticationException("Failed to authenticate, maybe consumer not enable the auth"); } AccessKeyPair accessKeyPair = null; try { accessKeyPair = getAccessKeyPair(invocation, url); } catch (Exception e) { throw new RpcAuthenticationException("Failed to authenticate , can't load the accessKeyPair", e); } String computeSignature = getSignature(url, invocation, accessKeyPair.getSecretKey(), requestTimestamp); boolean success = computeSignature.equals(originSignature); if (!success) { throw new RpcAuthenticationException("Failed to authenticate, signature is not correct"); } }  @Override void sign(Invocation invocation, URL url); @Override void authenticate(Invocation invocation, URL url);  }
@Test void testAuthenticateRequestNoSignature() { URL url = URL.valueOf("dubbo: .addParameter(Constants.ACCESS_KEY_ID_KEY, "ak") .addParameter(CommonConstants.APPLICATION_KEY, "test") .addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk"); Invocation invocation = new RpcInvocation(); AccessKeyAuthenticator helper = new AccessKeyAuthenticator(); assertThrows(RpcAuthenticationException.class, () -> helper.authenticate(invocation, url)); }