Skip to content

Instantly share code, notes, and snippets.

@dzmitry-savitski
Created March 31, 2025 20:56
Show Gist options
  • Save dzmitry-savitski/f49997cb5cc8e8d41ad27a51ecd3692d to your computer and use it in GitHub Desktop.
Save dzmitry-savitski/f49997cb5cc8e8d41ad27a51ecd3692d to your computer and use it in GitHub Desktop.
GSS groovy check
import javax.security.auth.login.LoginContext
import javax.security.auth.login.Configuration
import javax.security.auth.callback.*
import com.sun.security.auth.callback.TextCallbackHandler
class PasswordCallbackHandler implements CallbackHandler {
private String username
private String password
PasswordCallbackHandler(String username, String password) {
this.username = username
this.password = password
}
void handle(Callback[] callbacks) {
callbacks.each { callback ->
if (callback instanceof NameCallback) {
callback.setName(username)
} else if (callback instanceof PasswordCallback) {
callback.setPassword(password.toCharArray())
} else {
throw new UnsupportedCallbackException(callback, "Unsupported callback")
}
}
}
}
// Set system properties for Kerberos config
System.setProperty("java.security.krb5.conf", "/etc/krb5.conf") // adjust for your OS
System.setProperty("java.security.auth.login.config", "login.conf")
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false")
def username = "HTTP/[email protected]"
def password = "yourSPNPassword"
try {
def loginContext = new LoginContext("KrbLogin", new PasswordCallbackHandler(username, password))
loginContext.login()
println "✅ Authentication successful"
} catch (Exception e) {
println "❌ Authentication failed: ${e.message}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment