首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Java中的Waffle获取Kerberos服务票证

使用Java中的Waffle获取Kerberos服务票证
EN

Stack Overflow用户
提问于 2018-04-24 15:56:57
回答 1查看 1.7K关注 0票数 1

我正在开发运行在Windows 10上的Java应用程序中真正的SSO。我的应用程序已经有Kerberos使用Java的GSSAPI (但它显然不适用于任何现代Windows,尤其是严格的安全策略和域用户),所以我想用Waffle替换当前的授权系统,这对整个应用程序设计的影响最小--我认为如果我能够以某种方式获得KerberosTicket实例的话,这是可能的。我正在努力编写这个功能,到目前为止,我设法请求了一些令牌,但我不知道这个令牌是什么,它不匹配Kerberos票证格式。下面是我的(实际上更像在线代码):

代码语言:javascript
复制
public byte[] getServiceTicketSSPI() {
    final String securityPackage = "Kerberos";
    final String targetName = "<disclosed>";
    IWindowsCredentialsHandle clientCredentials = null;
    WindowsSecurityContextImpl clientContext = null;
    final String currentUser = WindowsAccountImpl.getCurrentUsername();
    try {
        clientCredentials = WindowsCredentialsHandleImpl.getCurrent(securityPackage);                               
        clientCredentials.initialize();
        // initial client security context
        clientContext = new WindowsSecurityContextImpl();
        clientContext.setPrincipalName(currentUser);
        clientContext.setCredentialsHandle(clientCredentials);
        clientContext.setSecurityPackage(securityPackage);
        final Sspi.SecBufferDesc continueToken = null;
        do {
            if(debug)
                System.out.println("Using target name: " + targetName);
            clientContext.initialize(clientContext.getHandle(), continueToken, targetName);
        } while(clientContext.isContinue());

        return clientContext.getToken();
    } finally {
        if (clientContext != null)
            clientContext.dispose();
        if (clientCredentials != null)
            clientCredentials.dispose();
    }
}

公平地说,我甚至不确定SSPI是否真的允许我看到真正的门票。我带着这段片段往正确的方向走了吗?我会很高兴的,所以看看我该做什么的任何线索。最终拥有KerberosTicket实例将是完美的。

EN

回答 1

Stack Overflow用户

发布于 2020-01-13 08:11:38

下面是在不使用服务器的情况下为独立的Java客户机使用Waffle的步骤。

使用initializeSecurityContext of WindowsSecurityContextImpl创建客户端凭据获取服务票证。使用accessSecurityContext of WindowsAuthProviderImpl原始链接https://exceptionshub.com/getting-kerberos-service-ticket-using-waffle-in-java.html获取WindowsAuthProviderImpl

对于客户机服务器sso,您应该遵循https://code.dblock.org/2010/04/08/pure-java-waffle.html,下面的代码使用kerberos描述独立的java。

代码语言:javascript
复制
import com.sun.jna.platform.win32.Sspi;
import waffle.windows.auth.IWindowsCredentialsHandle;
import waffle.windows.auth.IWindowsIdentity;
import waffle.windows.auth.IWindowsSecurityContext;
import waffle.windows.auth.impl.WindowsAccountImpl;
import waffle.windows.auth.impl.WindowsAuthProviderImpl;
import waffle.windows.auth.impl.WindowsCredentialsHandleImpl;
import waffle.windows.auth.impl.WindowsSecurityContextImpl;

public class KerberosSingleSignOn {
  public static void main() {
    try {
      System.out.println(getWindowsIdentity().getFqn());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static IWindowsIdentity getWindowsIdentity() throws Exception {
    try {
      byte[] kerberosToken = getServiceTicketSSPI();
      WindowsAuthProviderImpl provider = new WindowsAuthProviderImpl();
      IWindowsSecurityContext securityContext = provider
        .acceptSecurityToken("client-connection", kerberosToken, "Kerberos");
      return securityContext.getIdentity();
    }
    catch (Exception e) {
      throw new Exception("Failed to process kerberos token");
    }
  }

  public static byte[] getServiceTicketSSPI() throws Exception {
    final String securityPackage = "Kerberos";
    IWindowsCredentialsHandle clientCredentials = null;
    WindowsSecurityContextImpl clientContext = null;
    final String currentUser = WindowsAccountImpl.getCurrentUsername();
    try {
      clientCredentials = WindowsCredentialsHandleImpl.getCurrent(securityPackage);
      clientCredentials.initialize();
      // initial client security context
      clientContext = new WindowsSecurityContextImpl();
      clientContext.setCredentialsHandle(clientCredentials.getHandle());
      /*OR 
       clientContext.setCredentialsHandle(clientCredentials);
       */
      clientContext.setSecurityPackage(securityPackage);
      final Sspi.SecBufferDesc continueToken = null;
      do {
        System.out.println("Using current username: " + currentUser);
        clientContext.initialize(clientContext.getHandle(), continueToken, currentUser);
      }
      while (clientContext.isContinue());

      return clientContext.getToken();
    }
    catch (Exception e) {
      throw new Exception("Failed to process kerberos token");
    }
    finally {
      if (clientContext != null)
        clientContext.dispose();
      if (clientCredentials != null)
        clientCredentials.dispose();
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50006110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档