AccessKeyAuthenticator implements Authenticator { String getSignature(URL url, Invocation invocation, String secretKey, String time) { boolean parameterEncrypt = url.getParameter(Constants.PARAMETER_SIGNATURE_ENABLE_KEY, false); String signature; String requestString = String.format(Constants.SIGNATURE_STRING_FORMAT, url.getColonSeparatedKey(), invocation.getMethodName(), secretKey, time); if (parameterEncrypt) { signature = SignatureUtils.sign(invocation.getArguments(), requestString, secretKey); } else { signature = SignatureUtils.sign(requestString, secretKey); } return signature; }  @Override void sign(Invocation invocation, URL url); @Override void authenticate(Invocation invocation, URL url);  }
@Test void testGetSignatureNoParameter() { URL url = mock(URL.class); Invocation invocation = mock(Invocation.class); String secretKey = "123456"; AccessKeyAuthenticator helper = new AccessKeyAuthenticator(); String signature = helper.getSignature(url, invocation, secretKey, String.valueOf(System.currentTimeMillis())); assertNotNull(signature); }
@Test void testGetSignatureWithParameter() { URL url = mock(URL.class); when(url.getParameter(Constants.PARAMETER_SIGNATURE_ENABLE_KEY, false)).thenReturn(true); Invocation invocation = mock(Invocation.class); String secretKey = "123456"; Object[] params = {"dubbo", new ArrayList()}; when(invocation.getArguments()).thenReturn(params); AccessKeyAuthenticator helper = new AccessKeyAuthenticator(); String signature = helper.getSignature(url, invocation, secretKey, String.valueOf(System.currentTimeMillis())); assertNotNull(signature); Object[] fakeParams = {"dubbo1", new ArrayList<>()}; when(invocation.getArguments()).thenReturn(fakeParams); String signature1 = helper.getSignature(url, invocation, secretKey, String.valueOf(System.currentTimeMillis())); assertNotEquals(signature, signature1); }