appendix-db-schema.xml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <appendix version="5.0" xml:id="appendix-schema" xmlns="http://docbook.org/ns/docbook"
  3. xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
  4. <info>
  5. <title>Security Database Schema</title>
  6. </info>
  7. <para> There are various database schema used by the framework and this appendix provides a single
  8. reference point to them all. You only need to provide the tables for the areas of functonality
  9. you require. </para>
  10. <para> DDL statements are given for the HSQLDB database. You can use these as a guideline for
  11. defining the schema for the database you are using. </para>
  12. <section>
  13. <title>User Schema</title>
  14. <para> The standard JDBC implementation of the <interfacename>UserDetailsService</interfacename>
  15. requires tables to load the password, account status (enabled or disabled) and a list of
  16. authorities (roles) for the user.
  17. <programlisting xml:id="db_schema_users_authorities">
  18. create table users(
  19. username varchar_ignorecase(50) not null primary key,
  20. password varchar_ignorecase(50) not null,
  21. enabled boolean not null);
  22. create table authorities (
  23. username varchar_ignorecase(50) not null,
  24. authority varchar_ignorecase(50) not null,
  25. constraint fk_authorities_users foreign key(username) references users(username));
  26. create unique index ix_auth_username on authorities (username,authority);;
  27. </programlisting></para>
  28. <section>
  29. <title>Group Authorities</title>
  30. <para> Spring Security 2.0 introduced support for group authorities
  31. <programlisting xml:id="db-schema-groups">
  32. create table groups (
  33. id bigint generated by default as identity(start with 0) primary key,
  34. group_name varchar_ignorecase(50) not null);
  35. create table group_authorities (
  36. group_id bigint not null,
  37. authority varchar(50) not null,
  38. constraint fk_group_authorities_group foreign key(group_id) references groups(id));
  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. </programlisting></para>
  45. </section>
  46. </section>
  47. <section>
  48. <title>Persistent Login (Remember-Me) Schema</title>
  49. <para> This table is used to store data used by the more secure <link
  50. xlink:href="#remember-me-persistent-token">persistent token</link> remember-me
  51. implementation. If you are using <classname>JdbcTokenRepositoryImpl</classname> either
  52. directly or through the namespace, then you will need this table.
  53. <programlisting xml:id="db-schema-remeber-me">
  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. </programlisting></para>
  60. </section>
  61. <section xml:id="dbschema-acl">
  62. <title>ACL Schema</title>
  63. <para>There are four tables used by the Spring Security <link xlink:href="#domain-acls"
  64. >ACL</link> implementation. <orderedlist>
  65. <listitem>
  66. <para><literal>acl_sid</literal> stores the security identities recognised by the ACL
  67. system. These can be unique principals or authorities which may apply to multiple
  68. principals.</para>
  69. </listitem>
  70. <listitem>
  71. <para><literal>acl_class</literal> defines the domain object types to which ACLs apply.
  72. The <literal>class</literal> column stores the Java class name of the object. </para>
  73. </listitem>
  74. <listitem>
  75. <para><literal>acl_object_identity</literal> stores the object identity definitions of
  76. specific domai objects.</para>
  77. </listitem>
  78. <listitem>
  79. <para><literal>acl_entry</literal> stores the ACL permissions which apply to a specific
  80. object identity and security identity.</para>
  81. </listitem>
  82. </orderedlist></para>
  83. <para>It is assumed that the database will auto-generate the primary keys for each of the
  84. identities. The <literal>JdbcMutableAclService</literal> has to be able to retrieve these when
  85. it has created a new row in the <literal>acl_sid</literal> or <literal>acl_class</literal>
  86. tables. It has two properties which define the SQL needed to retrieve these values
  87. <literal>classIdentityQuery</literal> and <literal>sidIdentityQuery</literal>. Both of these
  88. default to <literal>call identity()</literal></para>
  89. <section>
  90. <title>Hypersonic SQL</title>
  91. <para>The default schema works with the embedded HSQLDB database that is used in unit tests
  92. within the
  93. framework.<programlisting xml:id="dbschema-acl-hsql">
  94. create table acl_sid (
  95. id bigint generated by default as identity(start with 100) not null primary key,
  96. principal boolean not null,
  97. sid varchar_ignorecase(100) not null,
  98. constraint unique_uk_1 unique(sid,principal) );
  99. create table acl_class (
  100. id bigint generated by default as identity(start with 100) not null primary key,
  101. class varchar_ignorecase(100) not null,
  102. constraint unique_uk_2 unique(class) );
  103. create table acl_object_identity (
  104. id bigint generated by default as identity(start with 100) not null primary key,
  105. object_id_class bigint not null,
  106. object_id_identity bigint not null,
  107. parent_object bigint,
  108. owner_sid bigint not null,
  109. entries_inheriting boolean not null,
  110. constraint unique_uk_3 unique(object_id_class,object_id_identity),
  111. constraint foreign_fk_1 foreign key(parent_object)references acl_object_identity(id),
  112. constraint foreign_fk_2 foreign key(object_id_class)references acl_class(id),
  113. constraint foreign_fk_3 foreign key(owner_sid)references acl_sid(id) );
  114. create table acl_entry (
  115. id bigint generated by default as identity(start with 100) not null primary key,
  116. acl_object_identity bigint not null,ace_order int not null,sid bigint not null,
  117. mask integer not null,granting boolean not null,audit_success boolean not null,
  118. audit_failure boolean not null,constraint unique_uk_4 unique(acl_object_identity,ace_order),
  119. constraint foreign_fk_4 foreign key(acl_object_identity) references acl_object_identity(id),
  120. constraint foreign_fk_5 foreign key(sid) references acl_sid(id) );
  121. </programlisting></para>
  122. <section>
  123. <title>PostgreSQL</title>
  124. <para>
  125. <programlisting>create table acl_sid(
  126. id bigserial not null primary key,
  127. principal boolean not null,
  128. sid varchar(100) not null,
  129. constraint unique_uk_1 unique(sid,principal));
  130. create table acl_class(
  131. id bigserial not null primary key,
  132. class varchar(100) not null,
  133. constraint unique_uk_2 unique(class));
  134. create table acl_object_identity(
  135. id bigserial primary key,
  136. object_id_class bigint not null,
  137. object_id_identity bigint not null,
  138. parent_object bigint,
  139. owner_sid bigint,
  140. entries_inheriting boolean not null,
  141. constraint unique_uk_3 unique(object_id_class,object_id_identity),
  142. constraint foreign_fk_1 foreign key(parent_object)references acl_object_identity(id),
  143. constraint foreign_fk_2 foreign key(object_id_class)references acl_class(id),
  144. constraint foreign_fk_3 foreign key(owner_sid)references acl_sid(id));
  145. create table acl_entry(
  146. id bigserial primary key,
  147. acl_object_identity bigint not null,
  148. ace_order int not null,
  149. sid bigint not null,
  150. mask integer not null,
  151. granting boolean not null,
  152. audit_success boolean not null,
  153. audit_failure boolean not null,
  154. constraint unique_uk_4 unique(acl_object_identity,ace_order),
  155. constraint foreign_fk_4 foreign key(acl_object_identity) references acl_object_identity(id),
  156. constraint foreign_fk_5 foreign key(sid) references acl_sid(id));
  157. </programlisting>
  158. </para>
  159. <para>You will have to set the <literal>classIdentityQuery</literal> and
  160. <literal>sidIdentityQuery</literal> properties of
  161. <classname>JdbcMutableAclService</classname> to the following values, respectively: <itemizedlist>
  162. <listitem>
  163. <para><literal>select currval(pg_get_serial_sequence('acl_class',
  164. 'id'))</literal></para>
  165. </listitem>
  166. <listitem>
  167. <para><literal>select currval(pg_get_serial_sequence('acl_sid',
  168. 'id'))</literal></para>
  169. </listitem>
  170. </itemizedlist></para>
  171. </section>
  172. </section>
  173. </section>
  174. </appendix>