2014-09-29 57 views
4

使用鬆餅我使用下面的代碼獲取實際登錄用戶:的Java SSO與Tomcat的

IWindowsSecurityContext clientContext = WindowsSecurityContextImpl.getCurrent("Negotiate", "localhost"); 
    WindowsAuthProviderImpl provider = new WindowsAuthProviderImpl(); 
    IWindowsSecurityContext serverContext = null; 

    do { 
     if (serverContext != null) { 
      byte[] tokenForTheClientOnTheServer = serverContext.getToken(); 
      SecBufferDesc continueToken = new SecBufferDesc(Sspi.SECBUFFER_TOKEN, tokenForTheClientOnTheServer); 
      clientContext.initialize(clientContext.getHandle(), continueToken, "localhost"); 
     } 
     byte[] tokenForTheServerOnTheClient = clientContext.getToken(); 
     serverContext = provider.acceptSecurityToken("server-connection", tokenForTheServerOnTheClient, "Negotiate"); 

     System.out.println("SSO-Identity: " + serverContext.getIdentity().getFqn()); 

    } while (clientContext.getContinue()); 

    System.out.println("Identity: " + serverContext.getIdentity().getFqn()); 

它正常工作時,我開始它的Eclipse,並返回我的用戶名。

當我部署我的Web應用程序並在Tomcat中啓動它時,它會返回nt-authority \ system。但我需要實際登錄用戶的名稱。 (在Tomcat中使用Waffle SSO工作正常,但我不可能獲得用戶的名字)

請問任何人有想法?

編輯:eclipse中的用戶主體也是正確的。當我在Tomcat中啓動時,它始終爲空。

回答

1

它讓您獲得在服務器上運行Tomcat服務的用戶標識。如果您需要客戶端的用戶標識,您應該使用request.getUserPrincipal().getName());

1

如果您嘗試在tomcat中使用華夫餅認證,則應該使用過濾器而不是獨立代碼。 現在你的代碼對於一些獨立的應用程序是很好的,例如Swing/win表單等。 但是通過HTTP,它應該使用過濾器來完成。看看Using Spring security + tomcat