首页 > 解决方案 > 使用 Java 和 Kerberos 以不同用户身份创建进程

问题描述

我正在开发一个 Java 服务器应用程序(在 Windows 下作为服务运行)并希望实现以下场景:

工作概念验证:

缺少什么并且不起作用

我的问题:

服务器 - 身份验证(缩短,从 Waffle 中提取):

final byte[] tokenBuffer = authorizationHeader.getTokenBytes();
CredHandle serverCredHandle = new CredHandle();
TimeStamp clientLifetime = new TimeStamp();
int rc = Secur32.INSTANCE.AcquireCredentialsHandle(
                                null, 
                                "Negotiate", 
                                Sspi.SECPKG_CRED_INBOUND, 
                                null, 
                                null, 
                                null, 
                                null, 
                                serverCredHandle, 
                                clientLifetime);

SecBufferDesc pbClientToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, tokenBuffer);
CtxtHandle phNewServerContext = new CtxtHandle();
SecBufferDesc pbServerToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, Sspi.MAX_TOKEN_SIZE);
IntByReference pfClientContextAttr = new IntByReference();

rc = Secur32.INSTANCE.AcceptSecurityContext(
                                serverCredHandle, 
                                null, 
                                pbClientToken, 
                                Sspi.ISC_REQ_CONNECTION, 
                                Sspi.SECURITY_NATIVE_DREP, 
                                phNewServerContext, 
                                pbServerToken, 
                                pfClientContextAttr, 
                                null);


rc = Advapi32.INSTANCE.ImpersonateLoggedOnUser(/* provide the security context token */)

服务器 - CreateProcessAsUser:

// get impersonation token of user
WinNT.HANDLEByReference threadToken = new WinNT.HANDLEByReference();
WinNT.HANDLE threadHandle = Kernel32.INSTANCE.GetCurrentThread();

boolean threadTokenResult = Advapi32.INSTANCE.OpenThreadToken(
        threadHandle,
        WinNT.TOKEN_QUERY | WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_ASSIGN_PRIMARY,
        false, /* TRUE if the access check is to be made against the process-level security context. FALSE if the access check is to be made against the current security context of the thread calling the OpenThreadToken function. */
        threadToken);

// create primary token by duplicating impersonation token
WinNT.HANDLEByReference primaryToken = new WinNT.HANDLEByReference();

boolean primaryTokenResult = Advapi32.INSTANCE.DuplicateTokenEx(
        threadToken.getValue(),                                     /* hExistingToken */
        WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY | WinNT.TOKEN_ASSIGN_PRIMARY, /* dwDesiredAccess */
        null,                                           /* lpTokenAttributes */
        WinNT.SECURITY_IMPERSONATION_LEVEL.SecurityDelegation,      /* ImpersonationLevel */
        WinNT.TOKEN_TYPE.TokenPrimary,                              /* TokenType */
        primaryToken);                                              /* phNewToken */

String environment = createEnvironment(primaryToken);

WinBase.STARTUPINFO startupInfo = new WinBase.STARTUPINFO();
WinBase.PROCESS_INFORMATION processInfo = new WinBase.PROCESS_INFORMATION();

boolean createProcessResult = Advapi32.INSTANCE.CreateProcessAsUser(
        primaryToken.getValue(),    /* hToken */
        null,           /* lpApplicationName */
        command,                    /* lpCommandLine */
        null,       /* lpProcessAttributes */
        null,           /* lpThreadAttributes */
        false,          /* bInheritHandles */
        WinNT.CREATE_NEW_CONSOLE | WinNT.CREATE_UNICODE_ENVIRONMENT, /* dwCreationFlags */
        environment,                /* lpEnvironment */
        processDirectory,           /* lpCurrentDirectory */
        startupInfo,                /* lpStartupInfo */
        processInfo);               /* lpProcessInformation */

标签: kerberosjnacreateprocessasuserkerberos-delegationadvapi32

解决方案


推荐阅读