samples.xml 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <chapter xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
  2. version="5.0" xml:id="sample-apps">
  3. <info>
  4. <title xml:id="samples">Sample Applications</title>
  5. </info>
  6. <para> There are several sample web applications that are available with the project. To avoid
  7. an overly large download, only the "tutorial" and "contacts" samples are included in the
  8. distribution zip file. The others can be built directly from the source which you can obtain
  9. as described in <link xlink:href="#get-source">the introduction</link>. It's easy to build
  10. the project yourself and there's more information on the project web site at <link
  11. xlink:href="http://www.springsource.org/security/">
  12. http://www.springsource.org/security/ </link>. All paths referred to in this chapter are
  13. relative to the project source directory. </para>
  14. <section xml:id="tutorial-sample">
  15. <title>Tutorial Sample</title>
  16. <para> The tutorial sample is a nice basic example to get you started. It uses simple
  17. namespace configuration throughout. The compiled application is included in the
  18. distribution zip file, ready to be deployed into your web container
  19. (<filename>spring-security-samples-tutorial-3.1.x.war</filename>). The <link
  20. xlink:href="#ns-form-and-basic">form-based</link> authentication mechanism is used in
  21. combination with the commonly-used <link xlink:href="#remember-me">remember-me</link>
  22. authentication provider to automatically remember the login using cookies.</para>
  23. <para>We recommend you start with the tutorial sample, as the XML is minimal and easy to
  24. follow. Most importantly, you can easily add this one XML file (and its corresponding
  25. <literal>web.xml</literal> entries) to your existing application. Only when this basic
  26. integration is achieved do we suggest you attempt adding in method authorization or
  27. domain object security.</para>
  28. </section>
  29. <section xml:id="contacts-sample">
  30. <title>Contacts</title>
  31. <para> The Contacts Sample is an advanced example in that it illustrates the more powerful
  32. features of domain object access control lists (ACLs) in addition to basic application
  33. security. The application provides an interface with which the users are able to
  34. administer a simple database of contacts (the domain objects).</para>
  35. <para>To deploy, simply copy the WAR file from Spring Security distribution into your
  36. container’s <literal>webapps</literal> directory. The war should be called
  37. <filename>spring-security-samples-contacts-3.1.x.war</filename> (the appended version
  38. number will vary depending on what release you are using). </para>
  39. <para>After starting your container, check the application can load. Visit
  40. <literal>http://localhost:8080/contacts</literal> (or whichever URL is appropriate for
  41. your web container and the WAR you deployed). </para>
  42. <para>Next, click "Debug". You will be prompted to authenticate, and a series of usernames
  43. and passwords are suggested on that page. Simply authenticate with any of these and view
  44. the resulting page. It should contain a success message similar to the following:
  45. <literallayout>
  46. Security Debug Information
  47. Authentication object is of type:
  48. org.springframework.security.authentication.UsernamePasswordAuthenticationToken
  49. Authentication object as a String:
  50. org.springframework.security.authentication.UsernamePasswordAuthenticationToken@1f127853:
  51. Principal: org.springframework.security.core.userdetails.User@b07ed00: Username: rod; \
  52. Password: [PROTECTED]; Enabled: true; AccountNonExpired: true;
  53. credentialsNonExpired: true; AccountNonLocked: true; \
  54. Granted Authorities: ROLE_SUPERVISOR, ROLE_USER; \
  55. Password: [PROTECTED]; Authenticated: true; \
  56. Details: org.springframework.security.web.authentication.WebAuthenticationDetails@0: \
  57. RemoteIpAddress: 127.0.0.1; SessionId: 8fkp8t83ohar; \
  58. Granted Authorities: ROLE_SUPERVISOR, ROLE_USER
  59. Authentication object holds the following granted authorities:
  60. ROLE_SUPERVISOR (getAuthority(): ROLE_SUPERVISOR)
  61. ROLE_USER (getAuthority(): ROLE_USER)
  62. Success! Your web filters appear to be properly configured!
  63. </literallayout></para>
  64. <para>Once you successfully receive the above message, return to the sample application's
  65. home page and click "Manage". You can then try out the application. Notice that only the
  66. contacts available to the currently logged on user are displayed, and only users with
  67. <literal>ROLE_SUPERVISOR</literal> are granted access to delete their contacts. Behind
  68. the scenes, the <classname>MethodSecurityInterceptor</classname> is securing the
  69. business objects. </para>
  70. <para>The application allows you to modify the access control lists associated with
  71. different contacts. Be sure to give this a try and understand how it works by reviewing
  72. the application context XML files.</para>
  73. <!--
  74. TODO: Reintroduce standalone client example.
  75. <para>The Contacts sample application also includes a
  76. <literal>client</literal> directory. Inside you will find a small
  77. application that queries the backend business objects using several
  78. web services protocols. This demonstrates how to use Spring Security
  79. for authentication with Spring remoting protocols. To try this client,
  80. ensure your servlet container is still running the Contacts sample
  81. application, and then execute <literal>client rod koala</literal>. The
  82. command-line parameters respectively represent the username to use,
  83. and the password to use. Note that you may need to edit
  84. <literal>client.properties</literal> to use a different target
  85. URL.</para>
  86. -->
  87. </section>
  88. <section xml:id="ldap-sample">
  89. <title>LDAP Sample</title>
  90. <para> The LDAP sample application provides a basic configuration and sets up both a
  91. namespace configuration and an equivalent configuration using traditional beans, both in
  92. the same application context file. This means there are actually two identical
  93. authentication providers configured in this application. </para>
  94. </section>
  95. <section xml:id="openid-sample">
  96. <title>OpenID Sample</title>
  97. <para>
  98. The OpenID sample demonstrates how to use the namespace to configure OpenID and how to set up
  99. <link xlink:href="http://openid.net/specs/openid-attribute-exchange-1_0.html">attribute exchange</link>
  100. configurations for Google, Yahoo and MyOpenID identity providers (you can experiment with adding others
  101. if you wish). It uses the JQuery-based <link xlink:href="http://code.google.com/p/openid-selector/">openid-selector</link>
  102. project to provide a user-friendly login page which allows the user to easily select a provider, rather than
  103. typing in the full OpenID identifier.
  104. </para>
  105. <para>
  106. The application differs from normal authentication scenarios in that it allows any user to access the site (provided
  107. their OpenID authentication is successful). The first time you login, you will get a <quote>Welcome [your name]"</quote>
  108. message. If you logout and log back in (with the same OpenID identity) then this should change to <quote>Welcome Back</quote>.
  109. This is achieved by using a custom <interfacename>UserDetailsService</interfacename> which assigns a standard role
  110. to any user and stores the identities internally in a map. Obviously a real application would use a database instead.
  111. Have a look at the source form more information. This class also takes into account the fact that different attributes may be returned
  112. from different providers and builds the name with which it addresses the user accordingly.
  113. </para>
  114. </section>
  115. <section xml:id="cas-sample">
  116. <title>CAS Sample</title>
  117. <para> The CAS sample requires that you run both a CAS server and CAS client. It isn't
  118. included in the distribution so you should check out the project code as described in
  119. <link xlink:href="#get-source">the introduction</link>. You'll find the relevant files
  120. under the <filename>sample/cas</filename> directory. There's also a
  121. <filename>Readme.txt</filename> file in there which explains how to run both the server
  122. and the client directly from the source tree, complete with SSL support.</para>
  123. </section>
  124. <section xml:id="jaas-sample">
  125. <title>JAAS Sample</title>
  126. <para>The JAAS sample is very simple example of how to use a JAAS LoginModule with Spring Security. The provided LoginModule will
  127. successfully authenticate a user if the username equals the password otherwise a LoginException is thrown. The AuthorityGranter
  128. used in this example always grants the role ROLE_USER. The sample application also demonstrates how to run as the JAAS Subject
  129. returned by the LoginModule by setting <link xlink:href="#nsa-http-jaas-api-provision">jaas-api-provision</link> equal to "true".</para>
  130. </section>
  131. <section xml:id="preauth-sample">
  132. <title>Pre-Authentication Sample</title>
  133. <para> This sample application demonstrates how to wire up beans from the <link
  134. xlink:href="#preauth">pre-authentication</link> framework to make use of login
  135. information from a J2EE container. The user name and roles are those setup by the
  136. container. </para>
  137. <para> The code is in <filename>samples/preauth</filename>. </para>
  138. </section>
  139. </chapter>