简体   繁体   中英

How to generate the Kerberos Security Token?

I am working on a web based application and my application supports SSO (We use Kerberos protocol. The Active Directory mapping and SPN configurations are all done).

We are using GWT 2.4 for the GUI part and we support standalone and SSO modes for the application (We can switch between these modes using a utility). In the normal standalone mode, we show the user a login screen where in the user needs to enter the credentials to login. In the SSO mode, we directly take the user to the application bypassing the login screen. At the client side, we used to control this behavior by making GWT RPC calls.

Everything was working fine with this. Later we figured out that the application always used to show a blank screen when opened only in the first tab of IE browser. To resolve this problem, we replaced the GWT RPC's with GWT RequestBuilder. The problem was resolved. However, SSO functionality was not working as expected.

I introduced traces and figured out that the Authorization header does not get set explicitly with RequestBuilders. So, I encoded the username:password combination using Base64 and set this as the Authorization header.

Eg : Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

But this also did not solve my issue and I figured out the Negotiate header was not set. How do we set the Negotiate header ? When I was using GWT RPC's with the same configurations as described earlier, I never used to set the Negotiate header explicitly.

Please explain the significance of this header and how to generate it ? Below is the sample of the Negotiate header generated when I was using GWT RPC instead of RequestBuilder.

Negotiate       YIIMgQYGKwYBBQUCoIIMdTCCDHGgMDAuBgkqhkiC9xIBAgIGCSqGSIb3EgECAgYKKwYBBAGCNwICHgYKKwYBBAGCNwICCqKCDDsEggw3YIIMMwYJKoZIhvcSAQICAQBuggwiMIIMHqADAgEFoQMCAQ6iBwMFACAAAACjggTXYYIE0zCCBM+gAwIBBaEfGx1URVNUQ09SRS5URVNULkRJUi5URUxTVFJBLkNPTaIuMCygAwIBAqElMCMbBEhUVFAbG2x4d2ViMDIyMS5pbi50ZWxzdHJhLmNvbS5hdaOCBHUwggRxoAMCARehAwIBDaKCBGMEggRfLXu0zawh9DwU3Stix9fB4HKqmDA/vuWFgB1k7sCNAf69MyQtNRWdjuWhXsN+yJQPXh6Zu1rKs7qD53NA5EvmVGQo29wrjtUA6KBvtC6L0IpvIbf32yl32oYQcWW0J8Zm5HSz4nNV7NhYo2hrPyDRqGzxTXGBql80W+9zKT+XmE8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pNZERTbAaObbOT40w1ovL82XOVJPK+fMwZapRayNyaq4jAFmFSE8z0HTUq4/2GN8QZG3ZUeresn8UU6jGffgD4xuUoxkBQzzoiRbO29lUaNiuyL3W/6QLruB40C4tc9gTbHZRCsTK7D1E/OuunmnlzY1V081wsRikVg6M7HZgSBGhRU4UOGLKAl9A2ZudAhJPOyRewHsq4hkm9tp7p6+ToD6hNMxOiuC8ltkwM/8KA7I5ticvVPV60M+TWOLAn1IMB5es2uWSouzqewomkfQ3pXMVCgjOgQdA1521GLD2ly4aIq6F2LNA9azOdM4NewJHJTUClRK+r2Z/Eb54jhrVwphLoQI+1FanWFiORz1tKmZljY0T+hwU77Vt0hhSv+At77hp4hL4jpKlm/EWCPHAi0W1k5OEHmxXdCc35TZrrlZ6/MMZG2fMJJ5FRe8UGUpqq8pwGZLgilvjHXRPWiXYwvhtDYYGhvCyAvOBvzSlpdA7xWmBABGQ6DHw== 

Any help on this will be greatly appreciated.

Ok so here goes.

You need to do two things.

1. Invoke the LoginContext functionality using JAAS and get the context associated with creating the token header.

2. Creating the negotiate header with it.

For part 1:-

1.1 Create the file jaas.conf and put the following info inside it following :-

com.sun.security.jgss.krb5.accept {
  com.sun.security.auth.module.Krb5LoginModule required
  doNotPrompt=false
  isInitiator=true
  debug=false;
};

1.2 Point to the file via java.security.auth.login.config to full path of this file.

1.3 LoginContext lc = new LoginContext(); lc.login(). 

1.3 will prompt for username and password and then create the appropriate context for part.

Now for actually creating the header:-

2.1 Create class TokenGenerator which implements PrivilegedExceptionAction<String>. In its run method do the following:-

   2.1.1  GSSName gssServerName = manager.createName(ServiceNameYouWannaConnectTo, null);
   2.1.2: GSSContext context = manager.createContext(gssServerName,
                spnegoOid, null, GSSCredential.DEFAULT_LIFETIME);

   2.1.3: byte spnegoToken[] = context.initSecContext(spnegoToken, 0, spnegoToken.length);

   2.1.3: byte[] encodedToken =  Base64.encodeBase64(spnegoToken);

   2.1.4: return new String(encodedToken, "UTF-8");

2.2 String negotiateHeaderBody = Subject.doAs(lc.getSubject(), new TokenGenerator());

After this you can prepend "negotiate" and viola you have the header. Important to note that my syntax isnt perfect but only approximately correct. I have also ommited steps like creating the gssmanager and spnegoOid, which is actually trivial. And finally this is the most basic form of creating the token. There are ways to make jaas.conf programmatic and even allow finer control over parameters used for token generation. Also this works only for Sun JDK, there is some tweaking required for jaas in case of IBM JDK.

If you have any issues let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM