database-schema.adoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. [[appendix-schema]]
  2. == Security Database Schema
  3. There are various database schema used by the framework and this appendix provides a single reference point to them all.
  4. You only need to provide the tables for the areas of functionality you require.
  5. DDL statements are given for the HSQLDB database.
  6. You can use these as a guideline for defining the schema for the database you are using.
  7. === User Schema
  8. The standard JDBC implementation of the `UserDetailsService` (`JdbcDaoImpl`) requires tables to load the password, account status (enabled or disabled) and a list of authorities (roles) for the user.
  9. You will need to adjust this schema to match the database dialect you are using.
  10. [source]
  11. ----
  12. create table users(
  13. username varchar_ignorecase(50) not null primary key,
  14. password varchar_ignorecase(50) not null,
  15. enabled boolean not null
  16. );
  17. create table authorities (
  18. username varchar_ignorecase(50) not null,
  19. authority varchar_ignorecase(50) not null,
  20. constraint fk_authorities_users foreign key(username) references users(username)
  21. );
  22. create unique index ix_auth_username on authorities (username,authority);
  23. ----
  24. ==== Group Authorities
  25. Spring Security 2.0 introduced support for group authorities in `JdbcDaoImpl`.
  26. The table structure if groups are enabled is as follows.
  27. You will need to adjust this schema to match the database dialect you are using.
  28. [source]
  29. ----
  30. create table groups (
  31. id bigint generated by default as identity(start with 0) primary key,
  32. group_name varchar_ignorecase(50) not null
  33. );
  34. create table group_authorities (
  35. group_id bigint not null,
  36. authority varchar(50) not null,
  37. constraint fk_group_authorities_group foreign key(group_id) references groups(id)
  38. );
  39. create table group_members (
  40. id bigint generated by default as identity(start with 0) primary key,
  41. username varchar(50) not null,
  42. group_id bigint not null,
  43. constraint fk_group_members_group foreign key(group_id) references groups(id)
  44. );
  45. ----
  46. Remember that these tables are only required if you are using the provided JDBC `UserDetailsService` implementation.
  47. If you write your own or choose to implement `AuthenticationProvider` without a `UserDetailsService`, then you have complete freedom over how you store the data, as long as the interface contract is satisfied.
  48. === Persistent Login (Remember-Me) Schema
  49. This table is used to store data used by the more secure <<remember-me-persistent-token,persistent token>> remember-me implementation.
  50. If you are using `JdbcTokenRepositoryImpl` either directly or through the namespace, then you will need this table.
  51. Remember to adjust this schema to match the database dialect you are using.
  52. [source]
  53. ----
  54. create table persistent_logins (
  55. username varchar(64) not null,
  56. series varchar(64) primary key,
  57. token varchar(64) not null,
  58. last_used timestamp not null
  59. );
  60. ----
  61. [[dbschema-acl]]
  62. === ACL Schema
  63. There are four tables used by the Spring Security <<domain-acls,ACL>> implementation.
  64. . `acl_sid` stores the security identities recognised by the ACL system.
  65. These can be unique principals or authorities which may apply to multiple principals.
  66. . `acl_class` defines the domain object types to which ACLs apply.
  67. The `class` column stores the Java class name of the object.
  68. . `acl_object_identity` stores the object identity definitions of specific domain objects.
  69. . `acl_entry` stores the ACL permissions which apply to a specific object identity and security identity.
  70. It is assumed that the database will auto-generate the primary keys for each of the identities.
  71. The `JdbcMutableAclService` has to be able to retrieve these when it has created a new row in the `acl_sid` or `acl_class` tables.
  72. It has two properties which define the SQL needed to retrieve these values `classIdentityQuery` and `sidIdentityQuery`.
  73. Both of these default to `call identity()`
  74. The ACL artifact JAR contains files for creating the ACL schema in HyperSQL (HSQLDB), PostgreSQL, MySQL/MariaDB, Microsoft SQL Server, and Oracle Database.
  75. These schemas are also demonstrated in the following sections.
  76. ==== HyperSQL
  77. The default schema works with the embedded HSQLDB database that is used in unit tests within the framework.
  78. [source,ddl]
  79. ----
  80. create table acl_sid(
  81. id bigint generated by default as identity(start with 100) not null primary key,
  82. principal boolean not null,
  83. sid varchar_ignorecase(100) not null,
  84. constraint unique_uk_1 unique(sid,principal)
  85. );
  86. create table acl_class(
  87. id bigint generated by default as identity(start with 100) not null primary key,
  88. class varchar_ignorecase(100) not null,
  89. constraint unique_uk_2 unique(class)
  90. );
  91. create table acl_object_identity(
  92. id bigint generated by default as identity(start with 100) not null primary key,
  93. object_id_class bigint not null,
  94. object_id_identity varchar_ignorecase(36) not null,
  95. parent_object bigint,
  96. owner_sid bigint,
  97. entries_inheriting boolean not null,
  98. constraint unique_uk_3 unique(object_id_class,object_id_identity),
  99. constraint foreign_fk_1 foreign key(parent_object)references acl_object_identity(id),
  100. constraint foreign_fk_2 foreign key(object_id_class)references acl_class(id),
  101. constraint foreign_fk_3 foreign key(owner_sid)references acl_sid(id)
  102. );
  103. create table acl_entry(
  104. id bigint generated by default as identity(start with 100) not null primary key,
  105. acl_object_identity bigint not null,
  106. ace_order int not null,
  107. sid bigint not null,
  108. mask integer not null,
  109. granting boolean not null,
  110. audit_success boolean not null,
  111. audit_failure boolean not null,
  112. constraint unique_uk_4 unique(acl_object_identity,ace_order),
  113. constraint foreign_fk_4 foreign key(acl_object_identity) references acl_object_identity(id),
  114. constraint foreign_fk_5 foreign key(sid) references acl_sid(id)
  115. );
  116. ----
  117. ==== PostgreSQL
  118. [source,ddl]
  119. ----
  120. create table acl_sid(
  121. id bigserial not null primary key,
  122. principal boolean not null,
  123. sid varchar(100) not null,
  124. constraint unique_uk_1 unique(sid,principal)
  125. );
  126. create table acl_class(
  127. id bigserial not null primary key,
  128. class varchar(100) not null,
  129. constraint unique_uk_2 unique(class)
  130. );
  131. create table acl_object_identity(
  132. id bigserial primary key,
  133. object_id_class bigint not null,
  134. object_id_identity varchar(36) not null,
  135. parent_object bigint,
  136. owner_sid bigint,
  137. entries_inheriting boolean not null,
  138. constraint unique_uk_3 unique(object_id_class,object_id_identity),
  139. constraint foreign_fk_1 foreign key(parent_object)references acl_object_identity(id),
  140. constraint foreign_fk_2 foreign key(object_id_class)references acl_class(id),
  141. constraint foreign_fk_3 foreign key(owner_sid)references acl_sid(id)
  142. );
  143. create table acl_entry(
  144. id bigserial primary key,
  145. acl_object_identity bigint not null,
  146. ace_order int not null,
  147. sid bigint not null,
  148. mask integer not null,
  149. granting boolean not null,
  150. audit_success boolean not null,
  151. audit_failure boolean not null,
  152. constraint unique_uk_4 unique(acl_object_identity,ace_order),
  153. constraint foreign_fk_4 foreign key(acl_object_identity) references acl_object_identity(id),
  154. constraint foreign_fk_5 foreign key(sid) references acl_sid(id)
  155. );
  156. ----
  157. You will have to set the `classIdentityQuery` and `sidIdentityQuery` properties of `JdbcMutableAclService` to the following values, respectively:
  158. * `select currval(pg_get_serial_sequence('acl_class', 'id'))`
  159. * `select currval(pg_get_serial_sequence('acl_sid', 'id'))`
  160. ==== MySQL and MariaDB
  161. [source,ddl]
  162. ----
  163. CREATE TABLE acl_sid (
  164. id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  165. principal BOOLEAN NOT NULL,
  166. sid VARCHAR(100) NOT NULL,
  167. UNIQUE KEY unique_acl_sid (sid, principal)
  168. ) ENGINE=InnoDB;
  169. CREATE TABLE acl_class (
  170. id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  171. class VARCHAR(100) NOT NULL,
  172. UNIQUE KEY uk_acl_class (class)
  173. ) ENGINE=InnoDB;
  174. CREATE TABLE acl_object_identity (
  175. id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  176. object_id_class BIGINT UNSIGNED NOT NULL,
  177. object_id_identity VARCHAR(36) NOT NULL,
  178. parent_object BIGINT UNSIGNED,
  179. owner_sid BIGINT UNSIGNED,
  180. entries_inheriting BOOLEAN NOT NULL,
  181. UNIQUE KEY uk_acl_object_identity (object_id_class, object_id_identity),
  182. CONSTRAINT fk_acl_object_identity_parent FOREIGN KEY (parent_object) REFERENCES acl_object_identity (id),
  183. CONSTRAINT fk_acl_object_identity_class FOREIGN KEY (object_id_class) REFERENCES acl_class (id),
  184. CONSTRAINT fk_acl_object_identity_owner FOREIGN KEY (owner_sid) REFERENCES acl_sid (id)
  185. ) ENGINE=InnoDB;
  186. CREATE TABLE acl_entry (
  187. id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  188. acl_object_identity BIGINT UNSIGNED NOT NULL,
  189. ace_order INTEGER NOT NULL,
  190. sid BIGINT UNSIGNED NOT NULL,
  191. mask INTEGER UNSIGNED NOT NULL,
  192. granting BOOLEAN NOT NULL,
  193. audit_success BOOLEAN NOT NULL,
  194. audit_failure BOOLEAN NOT NULL,
  195. UNIQUE KEY unique_acl_entry (acl_object_identity, ace_order),
  196. CONSTRAINT fk_acl_entry_object FOREIGN KEY (acl_object_identity) REFERENCES acl_object_identity (id),
  197. CONSTRAINT fk_acl_entry_acl FOREIGN KEY (sid) REFERENCES acl_sid (id)
  198. ) ENGINE=InnoDB;
  199. ----
  200. ==== Microsoft SQL Server
  201. [source,ddl]
  202. ----
  203. CREATE TABLE acl_sid (
  204. id BIGINT NOT NULL IDENTITY PRIMARY KEY,
  205. principal BIT NOT NULL,
  206. sid VARCHAR(100) NOT NULL,
  207. CONSTRAINT unique_acl_sid UNIQUE (sid, principal)
  208. );
  209. CREATE TABLE acl_class (
  210. id BIGINT NOT NULL IDENTITY PRIMARY KEY,
  211. class VARCHAR(100) NOT NULL,
  212. CONSTRAINT uk_acl_class UNIQUE (class)
  213. );
  214. CREATE TABLE acl_object_identity (
  215. id BIGINT NOT NULL IDENTITY PRIMARY KEY,
  216. object_id_class BIGINT NOT NULL,
  217. object_id_identity VARCHAR(36) NOT NULL,
  218. parent_object BIGINT,
  219. owner_sid BIGINT,
  220. entries_inheriting BIT NOT NULL,
  221. CONSTRAINT uk_acl_object_identity UNIQUE (object_id_class, object_id_identity),
  222. CONSTRAINT fk_acl_object_identity_parent FOREIGN KEY (parent_object) REFERENCES acl_object_identity (id),
  223. CONSTRAINT fk_acl_object_identity_class FOREIGN KEY (object_id_class) REFERENCES acl_class (id),
  224. CONSTRAINT fk_acl_object_identity_owner FOREIGN KEY (owner_sid) REFERENCES acl_sid (id)
  225. );
  226. CREATE TABLE acl_entry (
  227. id BIGINT NOT NULL IDENTITY PRIMARY KEY,
  228. acl_object_identity BIGINT NOT NULL,
  229. ace_order INTEGER NOT NULL,
  230. sid BIGINT NOT NULL,
  231. mask INTEGER NOT NULL,
  232. granting BIT NOT NULL,
  233. audit_success BIT NOT NULL,
  234. audit_failure BIT NOT NULL,
  235. CONSTRAINT unique_acl_entry UNIQUE (acl_object_identity, ace_order),
  236. CONSTRAINT fk_acl_entry_object FOREIGN KEY (acl_object_identity) REFERENCES acl_object_identity (id),
  237. CONSTRAINT fk_acl_entry_acl FOREIGN KEY (sid) REFERENCES acl_sid (id)
  238. );
  239. ----
  240. ==== Oracle Database
  241. [source,ddl]
  242. ----
  243. CREATE TABLE acl_sid (
  244. id NUMBER(38) NOT NULL PRIMARY KEY,
  245. principal NUMBER(1) NOT NULL CHECK (principal in (0, 1)),
  246. sid NVARCHAR2(100) NOT NULL,
  247. CONSTRAINT unique_acl_sid UNIQUE (sid, principal)
  248. );
  249. CREATE SEQUENCE acl_sid_sequence START WITH 1 INCREMENT BY 1 NOMAXVALUE;
  250. CREATE OR REPLACE TRIGGER acl_sid_id_trigger
  251. BEFORE INSERT ON acl_sid
  252. FOR EACH ROW
  253. BEGIN
  254. SELECT acl_sid_sequence.nextval INTO :new.id FROM dual;
  255. END;
  256. CREATE TABLE acl_class (
  257. id NUMBER(38) NOT NULL PRIMARY KEY,
  258. class NVARCHAR2(100) NOT NULL,
  259. CONSTRAINT uk_acl_class UNIQUE (class)
  260. );
  261. CREATE SEQUENCE acl_class_sequence START WITH 1 INCREMENT BY 1 NOMAXVALUE;
  262. CREATE OR REPLACE TRIGGER acl_class_id_trigger
  263. BEFORE INSERT ON acl_class
  264. FOR EACH ROW
  265. BEGIN
  266. SELECT acl_class_sequence.nextval INTO :new.id FROM dual;
  267. END;
  268. CREATE TABLE acl_object_identity (
  269. id NUMBER(38) NOT NULL PRIMARY KEY,
  270. object_id_class NUMBER(38) NOT NULL,
  271. object_id_identity NVARCHAR2(36) NOT NULL,
  272. parent_object NUMBER(38),
  273. owner_sid NUMBER(38),
  274. entries_inheriting NUMBER(1) NOT NULL CHECK (entries_inheriting in (0, 1)),
  275. CONSTRAINT uk_acl_object_identity UNIQUE (object_id_class, object_id_identity),
  276. CONSTRAINT fk_acl_object_identity_parent FOREIGN KEY (parent_object) REFERENCES acl_object_identity (id),
  277. CONSTRAINT fk_acl_object_identity_class FOREIGN KEY (object_id_class) REFERENCES acl_class (id),
  278. CONSTRAINT fk_acl_object_identity_owner FOREIGN KEY (owner_sid) REFERENCES acl_sid (id)
  279. );
  280. CREATE SEQUENCE acl_object_identity_sequence START WITH 1 INCREMENT BY 1 NOMAXVALUE;
  281. CREATE OR REPLACE TRIGGER acl_object_identity_id_trigger
  282. BEFORE INSERT ON acl_object_identity
  283. FOR EACH ROW
  284. BEGIN
  285. SELECT acl_object_identity_sequence.nextval INTO :new.id FROM dual;
  286. END;
  287. CREATE TABLE acl_entry (
  288. id NUMBER(38) NOT NULL PRIMARY KEY,
  289. acl_object_identity NUMBER(38) NOT NULL,
  290. ace_order INTEGER NOT NULL,
  291. sid NUMBER(38) NOT NULL,
  292. mask INTEGER NOT NULL,
  293. granting NUMBER(1) NOT NULL CHECK (granting in (0, 1)),
  294. audit_success NUMBER(1) NOT NULL CHECK (audit_success in (0, 1)),
  295. audit_failure NUMBER(1) NOT NULL CHECK (audit_failure in (0, 1)),
  296. CONSTRAINT unique_acl_entry UNIQUE (acl_object_identity, ace_order),
  297. CONSTRAINT fk_acl_entry_object FOREIGN KEY (acl_object_identity) REFERENCES acl_object_identity (id),
  298. CONSTRAINT fk_acl_entry_acl FOREIGN KEY (sid) REFERENCES acl_sid (id)
  299. );
  300. CREATE SEQUENCE acl_entry_sequence START WITH 1 INCREMENT BY 1 NOMAXVALUE;
  301. CREATE OR REPLACE TRIGGER acl_entry_id_trigger
  302. BEFORE INSERT ON acl_entry
  303. FOR EACH ROW
  304. BEGIN
  305. SELECT acl_entry_sequence.nextval INTO :new.id FROM dual;
  306. END;
  307. ----