authentication-manager.adoc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. [[nsa-authentication]]
  2. = Authentication Services
  3. Before Spring Security 3.0, an `AuthenticationManager` was automatically registered internally.
  4. Now you must register one explicitly by using the `<authentication-manager>` element.
  5. Doing so creates an instance of Spring Security's `ProviderManager` class, which needs to be configured with a list of one or more `AuthenticationProvider` instances.
  6. You can create these instances either by using syntax elements provided by the namespace or by using standard bean definitions, marked for addition to the list by using the `authentication-provider` element.
  7. [[nsa-authentication-manager]]
  8. == <authentication-manager>
  9. Every Spring Security application that uses the namespace must include the `<authentication-manager>` element somewhere.
  10. It is responsible for registering the `AuthenticationManager`, which provides authentication services to the application.
  11. All elements that create `AuthenticationProvider` instances should be children of this element.
  12. [[nsa-authentication-manager-attributes]]
  13. === <authentication-manager> Attributes
  14. The `<authentication-manager>` element has the following attributes:
  15. [[nsa-authentication-manager-alias]]
  16. `alias`::
  17. This attribute lets you define an alias name for the internal instance to use in your own configuration.
  18. [[nsa-authentication-manager-erase-credentials]]
  19. `erase-credentials`::
  20. If set to `true`, the `AuthenticationManager` tries to clear any credentials data in the returned `Authentication` object, once the user has been authenticated.
  21. Literally, it maps to the `eraseCredentialsAfterAuthentication` property of the xref:servlet/authentication/architecture.adoc#servlet-authentication-providermanager[`ProviderManager`].
  22. [[nsa-authentication-manager-id]]
  23. `id`::
  24. This attribute lets you define an ID for the internal instance to use in your own configuration.
  25. It is the same as the `alias` element but provides a more consistent experience with elements that use the `id` attribute.
  26. [[nsa-authentication-manager-children]]
  27. === Child Elements of <authentication-manager>
  28. The `<authentication-manager>` element has the following child elements:
  29. * <<nsa-authentication-provider,authentication-provider>>
  30. * xref:servlet/appendix/namespace/ldap.adoc#nsa-ldap-authentication-provider[ldap-authentication-provider]
  31. [[nsa-authentication-provider]]
  32. == <authentication-provider>
  33. Unless used with a `ref` attribute, the `<authentication-provider>` element is shorthand for configuring a `DaoAuthenticationProvider`.
  34. A `DaoAuthenticationProvider` loads user information from a `UserDetailsService` and compares the username and password combination with the values supplied at login.
  35. You can define the `UserDetailsService` instance either by using an available namespace element (`jdbc-user-service`) or by using the `user-service-ref` attribute to point to a bean defined elsewhere in the application context.
  36. [[nsa-authentication-provider-parents]]
  37. === Parent Elements of <authentication-provider>
  38. The parent element of the `<authentication-provider>` element is the <<nsa-authentication-manager,authentication-manager>> element.
  39. [[nsa-authentication-provider-attributes]]
  40. === <authentication-provider> Attributes
  41. The `<authentication-provider>` element has the following attributes:
  42. [[nsa-authentication-provider-ref]]
  43. ref::
  44. Defines a reference to a Spring bean that implements `AuthenticationProvider`.
  45. +
  46. If you have written your own `AuthenticationProvider` implementation (or want to configure one of Spring Security's implementations as a traditional bean for some reason), you can use the following syntax to add it to the internal list of `ProviderManager`:
  47. +
  48. ====
  49. [source,xml]
  50. ----
  51. <security:authentication-manager>
  52. <security:authentication-provider ref="myAuthenticationProvider" />
  53. </security:authentication-manager>
  54. <bean id="myAuthenticationProvider" class="com.something.MyAuthenticationProvider"/>
  55. ----
  56. ====
  57. [[nsa-authentication-provider-user-service-ref]]
  58. `user-service-ref`::
  59. A reference to a bean that implements `UserDetailsService`, which may be created by using the standard bean element or the custom user-service element.
  60. [[nsa-authentication-provider-children]]
  61. === Child Elements of <authentication-provider>
  62. The `<authentication-provider>` element has the following child elements:
  63. * <<nsa-jdbc-user-service,jdbc-user-service>>
  64. * xref:servlet/appendix/namespace/ldap.adoc#nsa-ldap-user-service[ldap-user-service]
  65. * <<nsa-password-encoder,password-encoder>>
  66. * <<nsa-user-service,user-service>>
  67. [[nsa-jdbc-user-service]]
  68. == <jdbc-user-service>
  69. The `<jdbc-user-service>` element causes the creation of a JDBC-based `UserDetailsService`.
  70. [[nsa-jdbc-user-service-attributes]]
  71. === <jdbc-user-service> Attributes
  72. The `<jdbc-user-service>` element has the following attributes:
  73. [[nsa-jdbc-user-service-authorities-by-username-query]]
  74. `authorities-by-username-query`::
  75. An SQL statement to query for a user's granted authorities given a username.
  76. +
  77. The default is as follows:
  78. ====
  79. [source]
  80. ----
  81. select username, authority from authorities where username = ?
  82. ----
  83. ====
  84. [[nsa-jdbc-user-service-cache-ref]]
  85. `cache-ref`::
  86. Defines a reference to a cache for use with a `UserDetailsService`.
  87. [[nsa-jdbc-user-service-data-source-ref]]
  88. `data-source-ref`::
  89. The bean ID of the DataSource that provides the required tables.
  90. [[nsa-jdbc-user-service-group-authorities-by-username-query]]
  91. `group-authorities-by-username-query`::
  92. An SQL statement to query user's group authorities, given a username.
  93. The default is as follows:
  94. +
  95. ====
  96. [source]
  97. ----
  98. select
  99. g.id, g.group_name, ga.authority
  100. from
  101. groups g, group_members gm, group_authorities ga
  102. where
  103. gm.username = ? and g.id = ga.group_id and g.id = gm.group_id
  104. ----
  105. ====
  106. [[nsa-jdbc-user-service-id]]
  107. `id`::
  108. A bean identifier, which is used for referring to the bean elsewhere in the context.
  109. [[nsa-jdbc-user-service-role-prefix]]
  110. `role-prefix`::
  111. A non-empty string prefix that is added to role strings loaded from persistent storage.
  112. Default: `ROLE_`
  113. Use a value of `none` for no prefix in cases where the default should be non-empty.
  114. [[nsa-jdbc-user-service-users-by-username-query]]
  115. `users-by-username-query`::
  116. An SQL statement to query a username, password, and enabled status, given a username.
  117. The default is as follows:
  118. +
  119. ====
  120. [source]
  121. ----
  122. select username, password, enabled from users where username = ?
  123. ----
  124. ====
  125. [[nsa-password-encoder]]
  126. == <password-encoder>
  127. Injects a bean with the appropriate `PasswordEncoder` instance.
  128. Authentication providers can optionally be configured to use a password encoder, as described in the xref:features/authentication/password-storage.adoc#authentication-password-storage[Password Storage].
  129. [[nsa-password-encoder-parents]]
  130. === Parent Elements of <password-encoder>
  131. The `<password-encoder>` element has the following parent elements:
  132. * <<nsa-authentication-provider,authentication-provider>>
  133. * xref:servlet/appendix/namespace/authentication-manager.adoc#nsa-password-compare[password-compare]
  134. [[nsa-password-encoder-attributes]]
  135. === <password-encoder> Attributes
  136. The `<password-encoder>` element has the following attributes:
  137. [[nsa-password-encoder-hash]]
  138. `hash`::
  139. Defines the hashing algorithm for user passwords.
  140. [IMPORTANT]
  141. ====
  142. We recommend strongly against using MD4, as it is a very weak hashing algorithm.
  143. ====
  144. [[nsa-password-encoder-ref]]
  145. `ref`::
  146. Defines a reference to a Spring bean that implements `PasswordEncoder`.
  147. [[nsa-user-service]]
  148. == <user-service>
  149. The `<user-service>` element creates an in-memory `UserDetailsService` from a properties file or a list of `<user>` child elements.
  150. Usernames are converted to lower case internally, to allow for case-insensitive lookups, so do not use this element if you need case-sensitivity.
  151. [[nsa-user-service-attributes]]
  152. === <user-service> Attributes
  153. The `<user-service>` element has the following attributes:
  154. [[nsa-user-service-id]]
  155. `id`::
  156. A bean identifier, used to refer to the bean elsewhere in the context.
  157. [[nsa-user-service-properties]]
  158. `properties`::
  159. The location of a properties file, in which each line is in the format of
  160. +
  161. ====
  162. [source]
  163. ----
  164. username=password,grantedAuthority[,grantedAuthority][,enabled|disabled]
  165. ----
  166. ====
  167. [[nsa-user-service-children]]
  168. === Child Elements of <user-service>
  169. The `<user-service>` element has a single child element: <<nsa-user,user>>.
  170. Multiple `<user>` elements can be present.
  171. [[nsa-user]]
  172. == <user>
  173. The `<user>` represents a user in the application.
  174. [[nsa-user-parents]]
  175. === Parent Elements of <user>
  176. The parent element of the `<user>` element is the <<nsa-user-service,user-service>> element.
  177. [[nsa-user-attributes]]
  178. === <user> Attributes
  179. [[nsa-user-authorities]]
  180. `authorities`::
  181. One of more authorities to be granted to the user.
  182. Separate authorities with a comma (but no space) -- for example, `ROLE_USER,ROLE_ADMINISTRATOR`.
  183. [[nsa-user-disabled]]
  184. `disabled`::
  185. Set to `true` to mark an account as disabled and unusable.
  186. [[nsa-user-locked]]
  187. `locked`::
  188. Set to `true` to mark an account as locked and unusable.
  189. [[nsa-user-name]]
  190. `name`::
  191. The username assigned to the user.
  192. [[nsa-user-password]]
  193. `password`::
  194. This value may be hashed if the corresponding authentication provider supports hashing (remember to set the `hash` attribute of the `user-service` element).
  195. You can omit this attribute when the data is not used for authentication but only for accessing authorities.
  196. If omitted, the namespace generates a random value, preventing its accidental use for authentication.
  197. This attribute cannot be empty.