Every now and then I get an assignment that includes an application that should use Kerberos authentication for access to a resource of some sort. One of the first things I like to try out is to use the program below to see that I have got the correct credentials and that the resource is up and running. Because I rarely work with this I thought it would be best to put the code and instructions here:
1. First we create a krb5.conf file with the settings for the domain in question. There are a few ways to do this but I usually use the following way:
3 | Credentials cache: C:\Users\niklas\krb5cc_niklas |
5 | Default principal: niklas@MYDOMAIN.SE, 1 entry found. |
7 | [1] Service Principal: krbtgt/MYDOMAIN.SE@MYDOMAIN.SE |
8 | Valid starting: Jun 09, 2023 10:12:50 |
9 | Expires: Jun 09, 2023 20:12:50 |
We can here see the domain to use: MYDOMAIN.SE. This will be used in the [realms] section of the krb5.conf file
Now we need the DNS/IP for a KDC to put in the [libdefaults] section
01 | > nslookup - type =srv _kerberos._tcp.MYDOMAIN.SE |
03 | Server: dns1.mydomain.se |
06 | _kerberos._tcp.MYDOMAIN.SE SRV service location: |
10 | svr hostname = kdc0093.mydomain.se |
11 | _kerberos._tcp.MYDOMAIN.SE SRV service location: |
15 | svr hostname = kdc0094.mydomain.se |
16 | _kerberos._tcp.MYDOMAIN.SE SRV service location: |
20 | svr hostname = kdc0099.mydomain.se |
Here we can pick one and fill the krb5.conf file like below:
2 | default_realm = MYDOMAIN.SE |
6 | kdc = kdc0093.mydomain.se |
After this we also need a login.conf with some settings needed for the demo program
2. Login.conf
1 | com.sun.security.jgss.krb5.initiate { |
2 | com.sun.security.auth.module.Krb5LoginModule required |
3 | doNotPrompt= false useTicketCache= true ; |
3. Now it is time for the program
01 | import java.io.BufferedReader; |
02 | import java.io.InputStream; |
03 | import java.io.InputStreamReader; |
04 | import java.net.Authenticator; |
05 | import java.net.PasswordAuthentication; |
08 | public class RunHttpKerberos { |
10 | static final String kuser = "admin" ; |
11 | static final String kpass = "admin" ; |
14 | static class MyAuthenticator extends Authenticator { |
15 | public PasswordAuthentication getPasswordAuthentication() { |
16 | return ( new PasswordAuthentication(kuser, kpass.toCharArray())); |
20 | public static void main(String[] args) throws Exception { |
21 | Authenticator.setDefault( new MyAuthenticator()); |
22 | URL url = new URL(kurl); |
23 | InputStream ins = url.openConnection().getInputStream(); |
24 | BufferedReader reader = new BufferedReader( new InputStreamReader(ins)); |
26 | while ((str = reader.readLine()) != null ) |
27 | System.out.println(str); |
4. Compile and run the program with the following arguments
2 | -Djava.security.krb5.conf=krb5.conf |
3 | -Djava.security.auth.login.config=login.conf |
4 | -Djavax.security.auth.useSubjectCredsOnly= false |
For DEBUG logging slap on a “-Dsun.security.krb5.debug=true”
If auth works you should see the resource printed in console
Tested om Java 1.8.0_352 and Windows 10