diff --git a/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java b/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java index 7f887965ec..515bf63184 100644 --- a/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java +++ b/client/src/main/java/org/asynchttpclient/spnego/SpnegoEngine.java @@ -256,18 +256,18 @@ public String generateToken(String host) throws SpnegoEngineException { } } - protected String getCompleteServicePrincipalName(String host) { + String getCompleteServicePrincipalName(String host) { String name; if (servicePrincipalName == null) { if (useCanonicalHostname) { host = getCanonicalHostname(host); } - name = "HTTP/" + host; + name = "HTTP@" + host; } else { name = servicePrincipalName; - } - if (realmName != null) { - name += "@" + realmName; + if (realmName != null && !name.contains("@")) { + name += "@" + realmName; + } } log.debug("Service Principal Name is {}", name); return name; @@ -285,7 +285,7 @@ private String getCanonicalHostname(String hostname) { return canonicalHostname; } - public CallbackHandler getUsernamePasswordHandler() { + private CallbackHandler getUsernamePasswordHandler() { if (username == null) { return null; } diff --git a/client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java b/client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java index bd8fbf34ea..92ff4a4d78 100644 --- a/client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java +++ b/client/src/test/java/org/asynchttpclient/spnego/SpnegoEngineTest.java @@ -113,6 +113,44 @@ public void testSpnegoGenerateTokenWithCustomLoginConfig() throws Exception { Assert.assertTrue(token.startsWith("YII")); } + @Test + public void testGetCompleteServicePrincipalName() throws Exception { + { + SpnegoEngine spnegoEngine = new SpnegoEngine(null, + null, + "bob", + "service.ws.apache.org", + false, + null, + null, + null); + Assert.assertEquals("bob@service.ws.apache.org", spnegoEngine.getCompleteServicePrincipalName("localhost")); + } + { + SpnegoEngine spnegoEngine = new SpnegoEngine(null, + null, + null, + "service.ws.apache.org", + true, + null, + null, + null); + Assert.assertNotEquals("HTTP@localhost", spnegoEngine.getCompleteServicePrincipalName("localhost")); + Assert.assertTrue(spnegoEngine.getCompleteServicePrincipalName("localhost").startsWith("HTTP@")); + } + { + SpnegoEngine spnegoEngine = new SpnegoEngine(null, + null, + null, + "service.ws.apache.org", + false, + null, + null, + null); + Assert.assertEquals("HTTP@localhost", spnegoEngine.getCompleteServicePrincipalName("localhost")); + } + } + @AfterClass public static void cleanup() throws Exception { if (kerbyServer != null) {