jdbc.adoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. [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. Oracle is a popular database choice, but requires a slightly different schema.
  39. You can find the default Oracle Schema for users below.
  40. .Default User Schema for Oracle Databases
  41. [source,sql]
  42. ----
  43. CREATE TABLE USERS (
  44. USERNAME NVARCHAR2(128) PRIMARY KEY,
  45. PASSWORD NVARCHAR2(128) NOT NULL,
  46. ENABLED CHAR(1) CHECK (ENABLED IN ('Y','N') ) NOT NULL
  47. );
  48. CREATE TABLE AUTHORITIES (
  49. USERNAME NVARCHAR2(128) NOT NULL,
  50. AUTHORITY NVARCHAR2(128) NOT NULL
  51. );
  52. ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_UNIQUE UNIQUE (USERNAME, AUTHORITY);
  53. ALTER TABLE AUTHORITIES ADD CONSTRAINT AUTHORITIES_FK1 FOREIGN KEY (USERNAME) REFERENCES USERS (USERNAME) ENABLE;
  54. ----
  55. [[servlet-authentication-jdbc-schema-group]]
  56. === Group Schema
  57. If your application is leveraging groups, you will need to provide the groups schema.
  58. The default schema for groups can be found below.
  59. .Default Group Schema
  60. [source,sql]
  61. ----
  62. create table groups (
  63. id bigint generated by default as identity(start with 0) primary key,
  64. group_name varchar_ignorecase(50) not null
  65. );
  66. create table group_authorities (
  67. group_id bigint not null,
  68. authority varchar(50) not null,
  69. constraint fk_group_authorities_group foreign key(group_id) references groups(id)
  70. );
  71. create table group_members (
  72. id bigint generated by default as identity(start with 0) primary key,
  73. username varchar(50) not null,
  74. group_id bigint not null,
  75. constraint fk_group_members_group foreign key(group_id) references groups(id)
  76. );
  77. ----
  78. [[servlet-authentication-jdbc-datasource]]
  79. == Setting up a DataSource
  80. Before we configure `JdbcUserDetailsManager`, we must create a `DataSource`.
  81. 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>>.
  82. .Embedded Data Source
  83. [tabs]
  84. ======
  85. Java::
  86. +
  87. [source,java,role="primary"]
  88. ----
  89. @Bean
  90. DataSource dataSource() {
  91. return new EmbeddedDatabaseBuilder()
  92. .setType(H2)
  93. .addScript(JdbcDaoImpl.DEFAULT_USER_SCHEMA_DDL_LOCATION)
  94. .build();
  95. }
  96. ----
  97. XML::
  98. +
  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. +
  107. [source,kotlin,role="secondary"]
  108. ----
  109. @Bean
  110. fun dataSource(): DataSource {
  111. return EmbeddedDatabaseBuilder()
  112. .setType(H2)
  113. .addScript(JdbcDaoImpl.DEFAULT_USER_SCHEMA_DDL_LOCATION)
  114. .build()
  115. }
  116. ----
  117. ======
  118. In a production environment, you will want to ensure you setup a connection to an external database.
  119. [[servlet-authentication-jdbc-bean]]
  120. == JdbcUserDetailsManager Bean
  121. 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+`.
  122. See the xref:features/authentication/password-storage.adoc#authentication-password-storage[PasswordEncoder] section for more details about how to store passwords.
  123. .JdbcUserDetailsManager
  124. [tabs]
  125. ======
  126. Java::
  127. +
  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. +
  150. [source,xml,role="secondary",attrs="-attributes"]
  151. ----
  152. <jdbc-user-service>
  153. <user name="user"
  154. password="{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW"
  155. authorities="ROLE_USER" />
  156. <user name="admin"
  157. password="{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW"
  158. authorities="ROLE_USER,ROLE_ADMIN" />
  159. </jdbc-user-service>
  160. ----
  161. Kotlin::
  162. +
  163. [source,kotlin,role="secondary",attrs="-attributes"]
  164. ----
  165. @Bean
  166. fun users(dataSource: DataSource): UserDetailsManager {
  167. val user = User.builder()
  168. .username("user")
  169. .password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
  170. .roles("USER")
  171. .build();
  172. val admin = User.builder()
  173. .username("admin")
  174. .password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
  175. .roles("USER", "ADMIN")
  176. .build();
  177. val users = JdbcUserDetailsManager(dataSource)
  178. users.createUser(user)
  179. users.createUser(admin)
  180. return users
  181. }
  182. ----
  183. ======