digest.adoc 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [[servlet-authentication-digest]]
  2. = Digest Authentication
  3. This section provides details on how Spring Security provides support for https://tools.ietf.org/html/rfc2617[Digest Authentication], which is provided `DigestAuthenticationFilter`.
  4. [WARNING]
  5. ====
  6. You should not use Digest Authentication in modern applications, because it is not considered to be secure.
  7. The most obvious problem is that you must store your passwords in plaintext or an encrypted or MD5 format.
  8. All of these storage formats are considered insecure.
  9. 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.
  10. ====
  11. 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.
  12. Many https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Digest#Browser_compatibility[browsers support Digest Authentication].
  13. 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].
  14. Most user agents implement RFC 2617.
  15. 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.
  16. 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.
  17. However, everyone should use xref:features/exploits/http.adoc#http[HTTPS].
  18. Central to Digest Authentication is a "`nonce`".
  19. This is a value the server generates.
  20. Spring Security's nonce adopts the following format:
  21. .Digest Syntax
  22. ====
  23. [source,txt]
  24. ----
  25. base64(expirationTime + ":" + md5Hex(expirationTime + ":" + key))
  26. expirationTime: The date and time when the nonce expires, expressed in milliseconds
  27. key: A private key to prevent modification of the nonce token
  28. ----
  29. ====
  30. 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`.
  31. (See the {security-api-url}org/springframework/security/crypto/password/NoOpPasswordEncoder.html[`NoOpPasswordEncoder`] class in the Javadoc.)
  32. The following provides an example of configuring Digest Authentication with Java Configuration:
  33. .Digest Authentication
  34. ====
  35. .Java
  36. [source,java,role="primary"]
  37. ----
  38. @Autowired
  39. UserDetailsService userDetailsService;
  40. DigestAuthenticationEntryPoint entryPoint() {
  41. DigestAuthenticationEntryPoint result = new DigestAuthenticationEntryPoint();
  42. result.setRealmName("My App Relam");
  43. result.setKey("3028472b-da34-4501-bfd8-a355c42bdf92");
  44. }
  45. DigestAuthenticationFilter digestAuthenticationFilter() {
  46. DigestAuthenticationFilter result = new DigestAuthenticationFilter();
  47. result.setUserDetailsService(userDetailsService);
  48. result.setAuthenticationEntryPoint(entryPoint());
  49. }
  50. @Bean
  51. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  52. http
  53. // ...
  54. .exceptionHandling(e -> e.authenticationEntryPoint(authenticationEntryPoint()))
  55. .addFilterBefore(digestFilter());
  56. return http.build();
  57. }
  58. ----
  59. .XML
  60. [source,xml,role="secondary"]
  61. ----
  62. <b:bean id="digestFilter"
  63. class="org.springframework.security.web.authentication.www.DigestAuthenticationFilter"
  64. p:userDetailsService-ref="jdbcDaoImpl"
  65. p:authenticationEntryPoint-ref="digestEntryPoint"
  66. />
  67. <b:bean id="digestEntryPoint"
  68. class="org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint"
  69. p:realmName="My App Realm"
  70. p:key="3028472b-da34-4501-bfd8-a355c42bdf92"
  71. />
  72. <http>
  73. <!-- ... -->
  74. <custom-filter ref="userFilter" position="DIGEST_AUTH_FILTER"/>
  75. </http>
  76. ----
  77. ====