appendix-db-schema.xml 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. (<classname>JdbcDaoImpl</classname>) requires tables to load the password, account status
  16. (enabled or disabled) and a list of 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 in
  31. <classname>JdbcDaoImpl</classname>. The table structure if groups are enabled is as
  32. follows:<programlisting xml:id="db-schema-groups">
  33. create table groups (
  34. id bigint generated by default as identity(start with 0) primary key,
  35. group_name varchar_ignorecase(50) not null);
  36. create table group_authorities (
  37. group_id bigint not null,
  38. authority varchar(50) not null,
  39. constraint fk_group_authorities_group foreign key(group_id) references groups(id));
  40. create table group_members (
  41. id bigint generated by default as identity(start with 0) primary key,
  42. username varchar(50) not null,
  43. group_id bigint not null,
  44. constraint fk_group_members_group foreign key(group_id) references groups(id));
  45. </programlisting></para>
  46. </section>
  47. </section>
  48. <section>
  49. <title>Persistent Login (Remember-Me) Schema</title>
  50. <para> This table is used to store data used by the more secure <link
  51. xlink:href="#remember-me-persistent-token">persistent token</link> remember-me
  52. implementation. If you are using <classname>JdbcTokenRepositoryImpl</classname> either
  53. directly or through the namespace, then you will need this table.
  54. <programlisting xml:id="db-schema-remeber-me">
  55. create table persistent_logins (
  56. username varchar(64) not null,
  57. series varchar(64) primary key,
  58. token varchar(64) not null,
  59. last_used timestamp not null);
  60. </programlisting></para>
  61. </section>
  62. <section xml:id="dbschema-acl">
  63. <title>ACL Schema</title>
  64. <para>There are four tables used by the Spring Security <link xlink:href="#domain-acls"
  65. >ACL</link> implementation. <orderedlist>
  66. <listitem>
  67. <para><literal>acl_sid</literal> stores the security identities recognised by the ACL
  68. system. These can be unique principals or authorities which may apply to multiple
  69. principals.</para>
  70. </listitem>
  71. <listitem>
  72. <para><literal>acl_class</literal> defines the domain object types to which ACLs apply.
  73. The <literal>class</literal> column stores the Java class name of the object. </para>
  74. </listitem>
  75. <listitem>
  76. <para><literal>acl_object_identity</literal> stores the object identity definitions of
  77. specific domai objects.</para>
  78. </listitem>
  79. <listitem>
  80. <para><literal>acl_entry</literal> stores the ACL permissions which apply to a specific
  81. object identity and security identity.</para>
  82. </listitem>
  83. </orderedlist></para>
  84. <para>It is assumed that the database will auto-generate the primary keys for each of the
  85. identities. The <literal>JdbcMutableAclService</literal> has to be able to retrieve these when
  86. it has created a new row in the <literal>acl_sid</literal> or <literal>acl_class</literal>
  87. tables. It has two properties which define the SQL needed to retrieve these values
  88. <literal>classIdentityQuery</literal> and <literal>sidIdentityQuery</literal>. Both of these
  89. default to <literal>call identity()</literal></para>
  90. <section>
  91. <title>Hypersonic SQL</title>
  92. <para>The default schema works with the embedded HSQLDB database that is used in unit tests
  93. within the
  94. framework.<programlisting xml:id="dbschema-acl-hsql">
  95. create table acl_sid (
  96. id bigint generated by default as identity(start with 100) not null primary key,
  97. principal boolean not null,
  98. sid varchar_ignorecase(100) not null,
  99. constraint unique_uk_1 unique(sid,principal) );
  100. create table acl_class (
  101. id bigint generated by default as identity(start with 100) not null primary key,
  102. class varchar_ignorecase(100) not null,
  103. constraint unique_uk_2 unique(class) );
  104. create table acl_object_identity (
  105. id bigint generated by default as identity(start with 100) not null primary key,
  106. object_id_class bigint not null,
  107. object_id_identity bigint not null,
  108. parent_object bigint,
  109. owner_sid bigint not null,
  110. entries_inheriting boolean not null,
  111. constraint unique_uk_3 unique(object_id_class,object_id_identity),
  112. constraint foreign_fk_1 foreign key(parent_object)references acl_object_identity(id),
  113. constraint foreign_fk_2 foreign key(object_id_class)references acl_class(id),
  114. constraint foreign_fk_3 foreign key(owner_sid)references acl_sid(id) );
  115. create table acl_entry (
  116. id bigint generated by default as identity(start with 100) not null primary key,
  117. acl_object_identity bigint not null,ace_order int not null,sid bigint not null,
  118. mask integer not null,granting boolean not null,audit_success boolean not null,
  119. audit_failure boolean not null,
  120. constraint unique_uk_4 unique(acl_object_identity,ace_order),
  121. constraint foreign_fk_4 foreign key(acl_object_identity)
  122. references acl_object_identity(id),
  123. constraint foreign_fk_5 foreign key(sid) references acl_sid(id) );
  124. </programlisting></para>
  125. <section>
  126. <title>PostgreSQL</title>
  127. <para>
  128. <programlisting>create table acl_sid(
  129. id bigserial not null primary key,
  130. principal boolean not null,
  131. sid varchar(100) not null,
  132. constraint unique_uk_1 unique(sid,principal));
  133. create table acl_class(
  134. id bigserial not null primary key,
  135. class varchar(100) not null,
  136. constraint unique_uk_2 unique(class));
  137. create table acl_object_identity(
  138. id bigserial primary key,
  139. object_id_class bigint not null,
  140. object_id_identity bigint not null,
  141. parent_object bigint,
  142. owner_sid bigint,
  143. entries_inheriting boolean not null,
  144. constraint unique_uk_3 unique(object_id_class,object_id_identity),
  145. constraint foreign_fk_1 foreign key(parent_object) references acl_object_identity(id),
  146. constraint foreign_fk_2 foreign key(object_id_class) references acl_class(id),
  147. constraint foreign_fk_3 foreign key(owner_sid) references acl_sid(id));
  148. create table acl_entry(
  149. id bigserial primary key,
  150. acl_object_identity bigint not null,
  151. ace_order int not null,
  152. sid bigint not null,
  153. mask integer not null,
  154. granting boolean not null,
  155. audit_success boolean not null,
  156. audit_failure boolean not null,
  157. constraint unique_uk_4 unique(acl_object_identity,ace_order),
  158. constraint foreign_fk_4 foreign key(acl_object_identity)
  159. references acl_object_identity(id),
  160. constraint foreign_fk_5 foreign key(sid) references acl_sid(id));
  161. </programlisting>
  162. </para>
  163. <para>You will have to set the <literal>classIdentityQuery</literal> and
  164. <literal>sidIdentityQuery</literal> properties of
  165. <classname>JdbcMutableAclService</classname> to the following values, respectively: <itemizedlist>
  166. <listitem>
  167. <para><literal>select currval(pg_get_serial_sequence('acl_class',
  168. 'id'))</literal></para>
  169. </listitem>
  170. <listitem>
  171. <para><literal>select currval(pg_get_serial_sequence('acl_sid',
  172. 'id'))</literal></para>
  173. </listitem>
  174. </itemizedlist></para>
  175. </section>
  176. </section>
  177. </section>
  178. </appendix>