1
0
mirror of synced 2026-08-31 22:46:02 +00:00
Files

95 lines
4.1 KiB
Plaintext
Raw Permalink Normal View History

[[servlet-authentication-digest]]
2019-12-06 10:39:55 -06:00
= Digest Authentication
2021-04-21 16:01:26 -05:00
This section provides details on how Spring Security provides support for https://tools.ietf.org/html/rfc2617[Digest Authentication], which is provided `DigestAuthenticationFilter`.
2019-12-06 10:39:55 -06:00
[WARNING]
====
2021-06-04 14:12:59 -05:00
You should not use Digest Authentication in modern applications, because it is not considered to be secure.
2021-04-21 16:01:26 -05:00
The most obvious problem is that you must store your passwords in plaintext or an encrypted or MD5 format.
2019-12-06 10:39:55 -06:00
All of these storage formats are considered insecure.
2021-04-21 16:01:26 -05:00
Instead, you should store credentials by using a one way adaptive password hash (bCrypt, PBKDF2, SCrypt, and others), which is not supported by Digest Authentication.
2019-12-06 10:39:55 -06:00
====
2021-12-13 16:57:36 -06:00
Digest Authentication tries to solve many of the weaknesses of xref:servlet/authentication/passwords/basic.adoc#servlet-authentication-basic[Basic authentication], specifically by ensuring credentials are never sent in clear text across the wire.
2019-12-06 10:39:55 -06:00
Many https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Digest#Browser_compatibility[browsers support Digest Authentication].
The standard governing HTTP Digest Authentication is defined by https://tools.ietf.org/html/rfc2617[RFC 2617], which updates an earlier version of the Digest Authentication standard prescribed by https://tools.ietf.org/html/rfc2069[RFC 2069].
Most user agents implement RFC 2617.
Spring Security's Digest Authentication support is compatible with the "`auth`" quality of protection (`qop`) prescribed by RFC 2617, which also provides backward compatibility with RFC 2069.
2021-04-21 16:01:26 -05:00
Digest Authentication was seen as a more attractive option if you need to use unencrypted HTTP (no TLS or HTTPS) and wish to maximize security of the authentication process.
2021-08-10 15:21:42 -05:00
However, everyone should use xref:features/exploits/http.adoc#http[HTTPS].
2019-12-06 10:39:55 -06:00
2021-04-21 16:01:26 -05:00
Central to Digest Authentication is a "`nonce`".
2019-12-06 10:39:55 -06:00
This is a value the server generates.
Spring Security's nonce adopts the following format:
.Digest Syntax
[source,txt]
----
base64(expirationTime + ":" + md5Hex(expirationTime + ":" + key))
expirationTime: The date and time when the nonce expires, expressed in milliseconds
key: A private key to prevent modification of the nonce token
----
2021-12-13 16:57:36 -06:00
You need to ensure that you xref:features/authentication/password-storage.adoc#authentication-password-storage-configuration[configure] insecure plain text xref:features/authentication/password-storage.adoc#authentication-password-storage[Password Storage] using `NoOpPasswordEncoder`.
2024-07-09 13:23:24 -05:00
(See the javadoc:org.springframework.security.crypto.password.NoOpPasswordEncoder[] class in the Javadoc.)
2019-12-06 10:39:55 -06:00
The following provides an example of configuring Digest Authentication with Java Configuration:
2020-01-09 20:12:19 -06:00
.Digest Authentication
2023-06-18 21:30:41 -05:00
[tabs]
======
Java::
+
2020-01-09 20:12:19 -06:00
[source,java,role="primary"]
2019-12-06 10:39:55 -06:00
----
@Autowired
UserDetailsService userDetailsService;
2024-05-10 22:24:46 +03:00
DigestAuthenticationEntryPoint authenticationEntryPoint() {
2019-12-06 10:39:55 -06:00
DigestAuthenticationEntryPoint result = new DigestAuthenticationEntryPoint();
2022-11-01 20:19:21 +01:00
result.setRealmName("My App Realm");
2019-12-06 10:39:55 -06:00
result.setKey("3028472b-da34-4501-bfd8-a355c42bdf92");
2024-05-10 22:24:46 +03:00
return result;
2019-12-06 10:39:55 -06:00
}
DigestAuthenticationFilter digestAuthenticationFilter() {
DigestAuthenticationFilter result = new DigestAuthenticationFilter();
result.setUserDetailsService(userDetailsService);
2024-05-10 22:24:46 +03:00
result.setAuthenticationEntryPoint(authenticationEntryPoint());
return result;
2019-12-06 10:39:55 -06:00
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
2019-12-06 10:39:55 -06:00
http
// ...
2025-06-19 14:34:48 -06:00
.exceptionHandling((e) -> e.authenticationEntryPoint(authenticationEntryPoint()))
2024-05-10 22:24:46 +03:00
.addFilter(digestAuthenticationFilter());
return http.build();
2019-12-06 10:39:55 -06:00
}
----
2023-06-18 21:30:41 -05:00
XML::
+
2020-01-09 20:12:19 -06:00
[source,xml,role="secondary"]
2019-12-06 10:39:55 -06:00
----
<b:bean id="digestFilter"
class="org.springframework.security.web.authentication.www.DigestAuthenticationFilter"
p:userDetailsService-ref="jdbcDaoImpl"
p:authenticationEntryPoint-ref="digestEntryPoint"
/>
<b:bean id="digestEntryPoint"
class="org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint"
2020-05-03 23:51:03 +03:00
p:realmName="My App Realm"
2019-12-06 10:39:55 -06:00
p:key="3028472b-da34-4501-bfd8-a355c42bdf92"
/>
<http>
<!-- ... -->
<custom-filter ref="userFilter" position="DIGEST_AUTH_FILTER"/>
</http>
----
2023-06-18 21:30:41 -05:00
======