jdbc.adoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. [[servlet-authentication-jdbc]]
  2. = JDBC Authentication
  3. Spring Security's `JdbcDaoImpl` implements xref:servlet/authentication/passwords/user-details-service.adoc#servlet-authentication-userdetailsservice[`UserDetailsService`] to provide support for username-and-password-based authentication that is retrieved by using JDBC.
  4. `JdbcUserDetailsManager` extends `JdbcDaoImpl` to provide management of `UserDetails` through the `UserDetailsManager` interface.
  5. `UserDetails`-based authentication is used by Spring Security when it is configured to xref:servlet/authentication/passwords/index.adoc#servlet-authentication-unpwd-input[accept a username/password] for authentication.
  6. In the following sections, we discuss:
  7. * The <<servlet-authentication-jdbc-schema>> used by Spring Security JDBC Authentication
  8. * <<servlet-authentication-jdbc-datasource>>
  9. * <<servlet-authentication-jdbc-bean>>
  10. [[servlet-authentication-jdbc-schema]]
  11. == Default Schema
  12. Spring Security provides default queries for JDBC-based authentication.
  13. This section provides the corresponding default schemas used with the default queries.
  14. You need to adjust the schema to match any customizations to the queries and the database dialect you use.
  15. [[servlet-authentication-jdbc-schema-user]]
  16. === User Schema
  17. `JdbcDaoImpl` requires tables to load the password, account status (enabled or disabled) and a list of authorities (roles) for the user.
  18. [NOTE]
  19. ====
  20. The default schema is also exposed as a classpath resource named `org/springframework/security/core/userdetails/jdbc/users.ddl`.
  21. ====
  22. .Default User Schema
  23. ====
  24. [source,sql]
  25. ----
  26. create table users(
  27. username varchar_ignorecase(50) not null primary key,
  28. password varchar_ignorecase(500) not null,
  29. enabled boolean not null
  30. );
  31. create table authorities (
  32. username varchar_ignorecase(50) not null,
  33. authority varchar_ignorecase(50) not null,
  34. constraint fk_authorities_users foreign key(username) references users(username)
  35. );
  36. create unique index ix_auth_username on authorities (username,authority);
  37. ----
  38. ====
  39. Oracle is a popular database choice but requires a slightly different schema:
  40. .Default User Schema for Oracle Databases
  41. ====
  42. [source,sql]
  43. ----
  44. CREATE TABLE USERS (
  45. USERNAME NVARCHAR2(128) PRIMARY KEY,
  46. PASSWORD NVARCHAR2(128) NOT NULL,
  47. ENABLED CHAR(1) CHECK (ENABLED IN ('Y','N') ) NOT NULL
  48. );
  49. CREATE TABLE AUTHORITIES (
  50. USERNAME NVARCHAR2(128) NOT NULL,
  51. AUTHORITY NVARCHAR2(128) NOT NULL
  52. );
  53. ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_UNIQUE UNIQUE (USERNAME, AUTHORITY);
  54. ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_FK1 FOREIGN KEY (USERNAME) REFERENCES USERS (USERNAME) ENABLE;
  55. ----
  56. ====
  57. [[servlet-authentication-jdbc-schema-group]]
  58. === Group Schema
  59. If your application uses groups, you need to provide the groups schema:
  60. .Default Group Schema
  61. ====
  62. [source,sql]
  63. ----
  64. create table groups (
  65. id bigint generated by default as identity(start with 0) primary key,
  66. group_name varchar_ignorecase(50) not null
  67. );
  68. create table group_authorities (
  69. group_id bigint not null,
  70. authority varchar(50) not null,
  71. constraint fk_group_authorities_group foreign key(group_id) references groups(id)
  72. );
  73. create table group_members (
  74. id bigint generated by default as identity(start with 0) primary key,
  75. username varchar(50) not null,
  76. group_id bigint not null,
  77. constraint fk_group_members_group foreign key(group_id) references groups(id)
  78. );
  79. ----
  80. ====
  81. [[servlet-authentication-jdbc-datasource]]
  82. == Setting up a DataSource
  83. Before we configure `JdbcUserDetailsManager`, we must create a `DataSource`.
  84. In our example, we set up an https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/data-access.html#jdbc-embedded-database-support[embedded DataSource] that is initialized with the <<servlet-authentication-jdbc-schema,default user schema>>.
  85. .Embedded Data Source
  86. ====
  87. .Java
  88. [source,java,role="primary"]
  89. ----
  90. @Bean
  91. DataSource dataSource() {
  92. return new EmbeddedDatabaseBuilder()
  93. .setType(H2)
  94. .addScript("classpath:org/springframework/security/core/userdetails/jdbc/users.ddl")
  95. .build();
  96. }
  97. ----
  98. .XML
  99. [source,xml,role="secondary"]
  100. ----
  101. <jdbc:embedded-database>
  102. <jdbc:script location="classpath:org/springframework/security/core/userdetails/jdbc/users.ddl"/>
  103. </jdbc:embedded-database>
  104. ----
  105. .Kotlin
  106. [source,kotlin,role="secondary"]
  107. ----
  108. @Bean
  109. fun dataSource(): DataSource {
  110. return EmbeddedDatabaseBuilder()
  111. .setType(H2)
  112. .addScript("classpath:org/springframework/security/core/userdetails/jdbc/users.ddl")
  113. .build()
  114. }
  115. ----
  116. ====
  117. In a production environment, you want to ensure that you set up a connection to an external database.
  118. [[servlet-authentication-jdbc-bean]]
  119. == JdbcUserDetailsManager Bean
  120. In this sample, we use xref:features/authentication/password-storage.adoc#authentication-password-storage-boot-cli[Spring Boot CLI] to encode a password value of `password` and get the encoded password of `+{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW+`.
  121. See the xref:features/authentication/password-storage.adoc#authentication-password-storage[PasswordEncoder] section for more details about how to store passwords.
  122. .JdbcUserDetailsManager
  123. ====
  124. .Java
  125. [source,java,role="primary",attrs="-attributes"]
  126. ----
  127. @Bean
  128. UserDetailsManager users(DataSource dataSource) {
  129. UserDetails user = User.builder()
  130. .username("user")
  131. .password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
  132. .roles("USER")
  133. .build();
  134. UserDetails admin = User.builder()
  135. .username("admin")
  136. .password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
  137. .roles("USER", "ADMIN")
  138. .build();
  139. JdbcUserDetailsManager users = new JdbcUserDetailsManager(dataSource);
  140. users.createUser(user);
  141. users.createUser(admin);
  142. return users;
  143. }
  144. ----
  145. .XML
  146. [source,xml,role="secondary",attrs="-attributes"]
  147. ----
  148. <jdbc-user-service>
  149. <user name="user"
  150. password="{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW"
  151. authorities="ROLE_USER" />
  152. <user name="admin"
  153. password="{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW"
  154. authorities="ROLE_USER,ROLE_ADMIN" />
  155. </jdbc-user-service>
  156. ----
  157. .Kotlin
  158. [source,kotlin,role="secondary",attrs="-attributes"]
  159. ----
  160. @Bean
  161. fun users(dataSource: DataSource): UserDetailsManager {
  162. val user = User.builder()
  163. .username("user")
  164. .password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
  165. .roles("USER")
  166. .build();
  167. val admin = User.builder()
  168. .username("admin")
  169. .password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
  170. .roles("USER", "ADMIN")
  171. .build();
  172. val users = JdbcUserDetailsManager(dataSource)
  173. users.createUser(user)
  174. users.createUser(admin)
  175. return users
  176. }
  177. ----
  178. ====