jdbc.adoc 6.8 KB

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