Security Top

I just announced the new Learn Spring Security course, including the full material focused on the new OAuth2 stack in Spring Security 5:

>> CHECK OUT THE COURSE

1. Overview

This tutorial shows how to set up, configure and customize Digest Authentication with Spring. Similar to the previous article covering Basic Authentication, we’re going to built on top of the Spring MVC tutorial, and secure the application with the Digest Auth mechanism provided by Spring Security.

2. The Security XML Configuration

First thing to understand about the configuration is that, while Spring Security does have full out of the box support for the Digest authentication mechanism, this support is not as well integrated into the namespace as Basic Authentication was.

In this case, we need to manually define the raw beans that are going to make up the security configuration – the DigestAuthenticationFilter and the DigestAuthenticationEntryPoint:

[prismatic_encoded class=%%language-xml%%]Jmx0O2JlYW5zOmJlYW4gaWQ9ImRpZ2VzdEZpbHRlciIgDQogIGNsYXNzPSJvcmcuc3ByaW5nZnJhbWV3b3JrLnNlY3VyaXR5LndlYi5hdXRoZW50aWNhdGlvbi53d3cuRGlnZXN0QXV0aGVudGljYXRpb25GaWx0ZXIiJmd0Ow0KICAgICZsdDtiZWFuczpwcm9wZXJ0eSBuYW1lPSJ1c2VyRGV0YWlsc1NlcnZpY2UiIHJlZj0idXNlclNlcnZpY2UiIC8mZ3Q7DQogICAgJmx0O2JlYW5zOnByb3BlcnR5IG5hbWU9ImF1dGhlbnRpY2F0aW9uRW50cnlQb2ludCIgcmVmPSJkaWdlc3RFbnRyeVBvaW50IiAvJmd0Ow0KJmx0Oy9iZWFuczpiZWFuJmd0Ow0KJmx0O2JlYW5zOmJlYW4gaWQ9ImRpZ2VzdEVudHJ5UG9pbnQiIA0KICBjbGFzcz0ib3JnLnNwcmluZ2ZyYW1ld29yay5zZWN1cml0eS53ZWIuYXV0aGVudGljYXRpb24ud3d3LkRpZ2VzdEF1dGhlbnRpY2F0aW9uRW50cnlQb2ludCImZ3Q7DQogICAgJmx0O2JlYW5zOnByb3BlcnR5IG5hbWU9InJlYWxtTmFtZSIgdmFsdWU9IkNvbnRhY3RzIFJlYWxtIHZpYSBEaWdlc3QgQXV0aGVudGljYXRpb24iIC8mZ3Q7DQogICAgJmx0O2JlYW5zOnByb3BlcnR5IG5hbWU9ImtleSIgdmFsdWU9ImFjZWdpIiAvJmd0Ow0KJmx0Oy9iZWFuczpiZWFuJmd0Ow0KDQombHQ7IS0tIHRoZSBzZWN1cml0eSBuYW1lc3BhY2UgY29uZmlndXJhdGlvbiAtLSZndDsNCiZsdDtodHRwIHVzZS1leHByZXNzaW9ucz0idHJ1ZSIgZW50cnktcG9pbnQtcmVmPSJkaWdlc3RFbnRyeVBvaW50IiZndDsNCiAgICAmbHQ7aW50ZXJjZXB0LXVybCBwYXR0ZXJuPSIvKioiIGFjY2Vzcz0iaXNBdXRoZW50aWNhdGVkKCkiIC8mZ3Q7DQoNCiAgICAmbHQ7Y3VzdG9tLWZpbHRlciByZWY9ImRpZ2VzdEZpbHRlciIgYWZ0ZXI9IkJBU0lDX0FVVEhfRklMVEVSIiAvJmd0Ow0KJmx0Oy9odHRwJmd0Ow0KDQombHQ7YXV0aGVudGljYXRpb24tbWFuYWdlciZndDsNCiAgICAmbHQ7YXV0aGVudGljYXRpb24tcHJvdmlkZXImZ3Q7DQogICAgICAgICZsdDt1c2VyLXNlcnZpY2UgaWQ9InVzZXJTZXJ2aWNlIiZndDsNCiAgICAgICAgICAgICZsdDt1c2VyIG5hbWU9InVzZXIxIiBwYXNzd29yZD0idXNlcjFQYXNzIiBhdXRob3JpdGllcz0iUk9MRV9VU0VSIiAvJmd0Ow0KICAgICAgICAmbHQ7L3VzZXItc2VydmljZSZndDsNCiAgICAmbHQ7L2F1dGhlbnRpY2F0aW9uLXByb3ZpZGVyJmd0Ow0KJmx0Oy9hdXRoZW50aWNhdGlvbi1tYW5hZ2VyJmd0Ow==[/prismatic_encoded]

Next, we need to integrate these beans into the overall security configuration – and in this case, the namespace is still flexible enough to allow us to do that.

The first part of this is pointing to the custom entry point bean, via the entry-point-ref attribute of the main <http> element.

The second part is adding the newly defined digest filter into the security filter chain. Since this filter is functionally equivalent to the BasicAuthenticationFilter, we are using the same relative position in the chain – this is specified by the BASIC_AUTH_FILTER alias in the overall Spring Security Standard Filters.

Finally, notice that the Digest Filter is configured to point to the user service bean – and here, the namespace is again very useful as it allows us to specify a bean name for the default user service created by the <user-service> element:

<user-service id="userService">

3. Consuming the Secured Application

We're going to be using the curl command to consume the secured application and understand how a client can interact with it.

Let's start by requesting the homepage – without providing security credentials in the request:

curl -i http://localhost/spring-security-mvc-digest-auth/homepage.html

As expected, we get back a response with a 401 Unauthorized status code:

HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=CF0233C...; Path=/spring-security-mvc-digest-auth/; HttpOnly
WWW-Authenticate: Digest realm="Contacts Realm via Digest Authentication", qop="auth", 
  nonce="MTM3MzYzODE2NTg3OTo3MmYxN2JkOWYxZTc4MzdmMzBiN2Q0YmY0ZTU0N2RkZg=="
Content-Type: text/html;charset=utf-8
Content-Length: 1061
Date: Fri, 12 Jul 2013 14:04:25 GMT

If this request were sent by the browser, the authentication challenge would prompt the user for credentials using a simple user/password dialog.

Let's now provide the correct credentials and send the request again:

curl -i --digest --user 
   user1:user1Pass http://localhost/spring-security-mvc-digest-auth/homepage.html

Notice that we are enabling Digest Authentication for the curl command via the –digest flag.

The first response from the server will be the same – the 401 Unauthorized – but the challenge will now be interpreted and acted upon by a second request – which will succeed with a 200 OK:

HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=A961E0D...; Path=/spring-security-mvc-digest-auth/; HttpOnly
WWW-Authenticate: Digest realm="Contacts Realm via Digest Authentication", qop="auth", 
  nonce="MTM3MzYzODgyOTczMTo3YjM4OWQzMGU0YTgwZDg0YmYwZjRlZWJjMDQzZWZkOA=="
Content-Type: text/html;charset=utf-8
Content-Length: 1061
Date: Fri, 12 Jul 2013 14:15:29 GMT

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=55F996B...; Path=/spring-security-mvc-digest-auth/; HttpOnly
Content-Type: text/html;charset=ISO-8859-1
Content-Language: en-US
Content-Length: 90
Date: Fri, 12 Jul 2013 14:15:29 GMT

<html>
<head></head>

<body>
	<h1>This is the homepage</h1>
</body>
</html>

A final note on this interaction is that a client can preemptively send the correct Authorization header with the first request, and thus entirely avoid the server security challenge and the second request.

4. The Maven Dependencies

The security dependencies are discussed in depth in the Spring Security Maven tutorial. In short, we will need to define spring-security-web and spring-security-config as dependencies in our pom.xml.

5. Conclusion

In this tutorial we introduce security into a simple Spring MVC project by leveraging the Digest Authentication support in the framework.

The implementation of these examples can be found in the Github project – this is an Eclipse-based project, so it should be easy to import and run as it is.

When the project runs locally, the homepage html can be accessed at (or, with minimal Tomcat configuration, on port 80):

http://localhost:8080/spring-security-mvc-digest-auth/homepage.html

Finally, there is no reason an application needs to choose between Basic and Digest authenticationboth can be set up simultaneously on the same URI structure, in such a way that the client can pick between the two mechanisms when consuming the web application.

Security bottom

I just announced the new Learn Spring Security course, including the full material focused on the new OAuth2 stack in Spring Security 5:

>> CHECK OUT THE COURSE
Security footer banner
Comments are closed on this article!