In my previous post I was explaining how to use an Active Directory server to authenticate a user. Indeed, I was trying to make the system authenticate the user using the Windows credentials that she already entered when logging onto her workstation.
Some years ago I was working with IIS and it was only a matter of configuration of the server to enable that for browsers that were supporting the appropriate protocol (others would be using HTTP basic).
One of the advantages of that protocol is that the user’s password is never sent over the wire. I found out this protocol is named SPNEGO and is an extension to the HTTP Negotiate protocol.
Since negotiation must occur between the browser and the server, if the server does not natively implement that protocol you cannot use the standard security APIs like custom registries or JAAS.
The solution is then to disable the server standard authentication mechanism and implement a filter that will negotiate, using SPNEGO, with the browser.
In the principle it looks easy but one still need to implement SPNEGO and bridge with Windows, because it’s Windows that finally authenticates the user.
After some goggling I found that the jCIFS library and its extension jCIFS-Ext have the necessary support to help me do the job. In fact everything is already there, even the filter: jcifs.http.AuthenticationFilter
.
So first, let’s configure the security constraints for our web-app. In the web.xml we must have the following:
1 2 3 4 5 6 7 8 9 10 |
<security-constraint> <web-resource-collection> <web-resource-name>Any resource</web-resource-name> <description>Any resource</description> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security> |
I do not define any role nor any authentication method because I don’t actually want the server to do the authentication by himself. Nevertheless, I define that I want confidentiality on those URLs.
I do that because I will configure my filter to fall-back to HTTP Basic if the browser does not support SPNEGO or HTTP Negotiate and I do not want the password to travel unencrypted on the net.
I hope this imply that if the application is not served over HTTPS there will be a problem, but I actually correctly configured my server to serve the application over HTTPS so I did not test this behaviour.
The second step is to configure the filter itself, the jCIFS-Ext filter has undocumented parameters so I had to go through the code to find them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<filter> <filter-name>AuthenticationFilter</filter-name> <display-name>AuthenticationFilter</display-name> <description>SPNEGO Authentication Filter</description> <filter-class>jcifs.http.AuthenticationFilter</filter-class> <init-param> <param-name>jcifs.smb.client.domain</param-name> <param-value>MyDomain</param-value> <description>The name of the Windows domain.</description> </init-param> <init-param> <param-name>jcifs.http.domainController</param-name> <param-value>mydomain.com</param-value> <description>The address of the Windows domain controller.</description> </init-param> <init-param> <param-name>jcifs.http.enableNegotiate</param-name> <param-value>true</param-value> <description>If the browser does not support SPNEGO, fallback to HTTP Negotiate.</description> </init-param> <init-param> <param-name>jcifs.http.enableBasic</param-name> <param-value>true</param-value> <description>If the browser does not support SPNEGO nor HTTP Negotiate, fallback to HTTP Basic but only if the connection is secure.</description> </init-param> <init-param> <param-name>jcifs.http.insecureBasic</param-name> <param-value>false</param-value> <description>Never fallback to HTTP Basic when the connection is insecure.</description> </init-param> <init-param> <param-name>jcifs.http.basicRealm</param-name> <param-value>mydomain</param-value> <description>The name of the domain in case of HTTP Basic authentication. Used only for display to the user.</description> </init-param> </filter> <filter-mapping> <filter-name>AuthenticationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
“Et voilà “, now your application should automatically authenticate the user based on its Windows credentials. I said should because there are some prerequisites:
- on the browser side, Windows integrated security must be enabled
- on the server side your platform must actually support Kerberos for the filter to properly work.
However, the former is a matter of configuration and the latter is a matter of slightly changing the code of the filter.
Configuring an Internet Explorer Browser
To configure an Internet Explorer browser to use Windows authentication, follow these procedures in Internet Explorer:
- Configure Local Intranet Domains
- In Internet Explorer, select Tools > Internet Options.
- Select the Security tab.
- Select Local intranet and click Sites.
- In the Local intranet popup, ensure that the “Include all sites that bypass the proxy server” and “Include all local (intranet) sites not listed in other zones” options are checked.
- Click Advanced.
- In the Local intranet (Advanced) dialog box, add all relative domain names that will be used for Integrator server instances participating in the SSO configuration (for example, myhost.example.com) and click OK.
- Configure Intranet Authentication
- Select Tools > Internet Options.
- Select the Security tab.
- Select Local intranet and click Custom Level…
- In the Security Settings dialog box, scroll to the User Authentication section.
- Select Automatic logon only in Intranet zone. This option prevents users from having to re-enter logon credentials, which is a key piece to this solution.
- Click OK.
- Verify the Proxy Settings (If you have a proxy server enabled)
- Select Tools > Internet Options.
- Select the Connections tab and click LAN Settings.
- Verify that the proxy server address and port number are correct.
- Click Advanced.
- In the Proxy Settings dialog box, ensure that all desired domain names are entered in the Exceptions field.
- Click OK to close the Proxy Settings dialog box.
- Set Integrated Authentication for Internet Explorer 6.0 (In addition to the previous settings, one additional setting is required if you are running Internet Explorer 6.0)
- In Internet Explorer, select Tools > Internet Options.
- Select the Advanced tab.
- Scroll to the Security section.
- Make sure that Enable Integrated Windows Authentication option is checked and click OK.
- If this option was not checked, restart the computer.
Despite all of this configuration I encountered some cases where this was not working at all in IE and I was unable to spot the problem, so you might be falling into this category. The symptoms are that the negociation process takes place but the browser does not answer the last challenge and no error message is displayed at all.
Configuring a Mozilla Firefox Browser
To configure an Mozilla Firefox browser to use Windows authentication, follow these procedures in Mozilla Firefox:
- Type
about:config
in the address bar of the browser and press return (a big list of properties should be displayed in the browser window). - Type “network” in the filter box.
- Double-click on the
network.automatic-ntlm-auth.trusted-uris
property and enter “mydomain.com” (if there is already a value you can add a comma to separate both entries)
The value for this preference is a comma-separated list of URI fragments. This sample string shows the three legal kinds of fragments: https://, http://www.example.com, test.com
The first fragment says, “Trust all URLs with an https scheme.” The second fragment (a full URL) says, “Trust this particular web site.” The third fragment is interpreted to mean http://anything.test.com, so any web site that is a subdomain of test.com, including test.com itself, will also be trusted.
I did not encounter any problem with Firefox which is what I call a paradox…
Changing the filter to use NTLM instead of Kerberos
Actually the change must not occur in the filter but in the class jcifs.spnego.Authentication
which comes with jCIFS-Ext. This class tries to determine if the system supports Kerberos but uses introspection, looking for some Java classes that enable Kerberos support in Java.
Nevertheless, those classes can be there without the actual system supporting Kerberos (which is the case where I work).
Fortunately, modifying the behaviour is not too much complicated, just change line 57 of this class:
57 |
private static final boolean KERBEROS_SUPPORTED = getKerberosSupport(); |
to the following:
57 |
private static final boolean KERBEROS_SUPPORTED = false; |
And then the filter will use NTLM instead of Kerberos.
I hope the next posts will be shorter :-P