Я создал вкладку ключей, jaas.conf. также krb5.conf.
Keytab действителен, я могу kinit keytab на своем компьютере с Windows
C:\Users\rikim>kinit -k -t C:\Windows\oneapp .keytab HTTP/[email protected]
Новый билет хранится в файле кэша C:\Users\rikim\krb5cc_rikim
но теперь мои программы кажутся согласованными токен недействителен в gsscontext,
я хочу получить информацию о пользователе на основе имени пользователя, указанного в keytab
Код: Выделить всё
package my.com.toyota;
import org.ietf.jgss.*;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import java.util.Base64;
public class Main {
public static void main(String[] args) {
// Set system properties for Kerberos configuration
System.setProperty("java.security.krb5.conf", "C:/Windows/krb5.conf"); // Path to krb5.conf
System.setProperty("java.security.auth.login.config", "C:/Windows/jaas.conf"); // Path to jaas.conf
System.setProperty("sun.security.krb5.debug", "true");
System.setProperty("sun.security.spnego.debug", "true");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false"); // Allow use of credentials outside the subject
System.setProperty("sun.security.krb5.disableReferrals", "true"); // Disable DNS canonicalization to avoid suffix issues
try {
// Initiate Kerberos login with LoginContext based on JAAS configuration
LoginContext loginContext = new LoginContext("com.sun.security.jgss.krb5.initiate");
loginContext.login();
System.out.println("Authenticated successfully with UAT AD!");
// Proceed with SPNEGO negotiation
String negotiateToken = performSpnegoNegotiation();
} catch (LoginException e) {
System.err.println("Login failed: " + e.getMessage());
e.printStackTrace();
} catch (GSSException e) {
throw new RuntimeException(e);
}
}
private static String performSpnegoNegotiation() throws GSSException {
// Create GSSManager instance for GSS-API
GSSManager gssManager = GSSManager.getInstance();
Oid spnegoOid = new Oid("1.3.6.1.5.5.2");
GSSName serviceName = gssManager.createName("HTTP/[email protected]", GSSName.NT_HOSTBASED_SERVICE);
// Canonicalize to mechanism-specific name
GSSName mechSpecificName = serviceName.canonicalize(spnegoOid);
// Establish the GSSContext for SPNEGO/Kerberos
GSSContext gssContext = gssManager.createContext(mechSpecificName, spnegoOid, null, GSSContext.DEFAULT_LIFETIME);
gssContext.requestMutualAuth(false);
gssContext.requestCredDeleg(true);
// Initialize the SPNEGO context
byte[] token = new byte[0];
if (!gssContext.isEstablished()) {
token = gssContext.initSecContext(token, 0, token.length);
}
// Print the SPNEGO token (Negotiate header) in Base64 format
String negotiateToken = "";
if (token != null && token.length > 0) {
negotiateToken = Base64.getEncoder().encodeToString(token);
System.out.println("SPNEGO Negotiate Token: " + negotiateToken);
} else {
System.out.println("SPNEGO negotiation completed without token.");
}
// Check if context is established and print user principal name
if (gssContext.isEstablished()) {
// Retrieve the authenticated client's principal name
System.out.println("Authenticated : " + gssContext.getSrcName().toString());
} else {
System.out.println("GSSContext not established.");
}
// Clean up the GSS context
gssContext.dispose();
return negotiateToken;
}
}
Позже я хочу, чтобы мой сервер проверил токен SPNEGO и получил информацию о пользователе на основе того, кто вошел в систему . Пожалуйста, помогите
Подробнее здесь: https://stackoverflow.com/questions/791 ... ros-spnego