x509-auth-provider.xml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <chapter xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="x509">
  2. <info>
  3. <title>X.509 Authentication</title>
  4. </info>
  5. <section xml:id="x509-overview">
  6. <info>
  7. <title>Overview</title>
  8. </info>
  9. <para>The most common use of X.509 certificate authentication is in verifying the identity
  10. of a server when using SSL, most commonly when using HTTPS from a browser. The browser
  11. will automatically check that the certificate presented by a server has been issued (ie
  12. digitally signed) by one of a list of trusted certificate authorities which it
  13. maintains.</para>
  14. <para>You can also use SSL with <quote>mutual authentication</quote>; the server will then
  15. request a valid certificate from the client as part of the SSL handshake. The server
  16. will authenticate the client by checking that its certificate is signed by an acceptable
  17. authority. If a valid certificate has been provided, it can be obtained through the
  18. servlet API in an application. Spring Security X.509 module extracts the certificate
  19. using a filter. It maps the certificate to an application user and loads that user's set
  20. of granted authorities for use with the standard Spring Security infrastructure.</para>
  21. <para>You should be familiar with using certificates and setting up client authentication
  22. for your servlet container before attempting to use it with Spring Security. Most of the
  23. work is in creating and installing suitable certificates and keys. For example, if
  24. you're using Tomcat then read the instructions here <uri
  25. xmlns:xlink="http://www.w3.org/1999/xlink"
  26. xlink:href="http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html"
  27. >http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html</uri>. It's important that you
  28. get this working before trying it out with Spring Security</para>
  29. </section>
  30. <section>
  31. <info>
  32. <title>Adding X.509 Authentication to Your Web Application</title>
  33. </info>
  34. <para> Enabling X.509 client authentication is very straightforward. Just add the
  35. <literal>&lt;x509/&gt;</literal> element to your http security namespace configuration.
  36. <programlisting>
  37. &lt;http&gt;
  38. ...
  39. &lt;x509 subject-principal-regex="CN=(.*?)," user-service-ref="userService"/&gt;
  40. ...
  41. &lt;/http&gt;
  42. </programlisting>
  43. The element has two optional attributes: <itemizedlist>
  44. <listitem>
  45. <para><literal>subject-principal-regex</literal>. The regular expression used to
  46. extract a username from the certificate's subject name. The default value is
  47. shown above. This is the username which will be passed to the
  48. <interfacename>UserDetailsService</interfacename> to load the authorities for
  49. the user.</para>
  50. </listitem>
  51. <listitem>
  52. <para><literal>user-service-ref</literal>. This is the bean Id of the
  53. <interfacename>UserDetailsService</interfacename> to be used with X.509. It
  54. isn't needed if there is only one defined in your application context.</para>
  55. </listitem>
  56. </itemizedlist> The <literal>subject-principal-regex</literal> should contain a single
  57. group. For example the default expression "CN=(.*?)," matches the common name field. So
  58. if the subject name in the certificate is "CN=Jimi Hendrix, OU=...", this will give a
  59. user name of "Jimi Hendrix". The matches are case insensitive. So "emailAddress=(.?),"
  60. will match "EMAILADDRESS=jimi@hendrix.org,CN=..." giving a user name "jimi@hendrix.org".
  61. If the client presents a certificate and a valid username is successfully extracted,
  62. then there should be a valid <classname>Authentication</classname> object in the
  63. security context. If no certificate is found, or no corresponding user could be found
  64. then the security context will remain empty. This means that you can easily use X.509
  65. authentication with other options such as a form-based login. </para>
  66. </section>
  67. <section xml:id="x509-ssl-config">
  68. <info>
  69. <title>Setting up SSL in Tomcat</title>
  70. </info>
  71. <para>There are some pre-generated certificates in the
  72. <filename>samples/certificate</filename> directory in the Spring Security project. You
  73. can use these to enable SSL for testing if you don't want to generate your own. The file
  74. <filename>server.jks</filename> contains the server certificate, private key and the
  75. issuing certificate authority certificate. There are also some client certificate files
  76. for the users from the sample applications. You can install these in your browser to
  77. enable SSL client authentication. </para>
  78. <para> To run tomcat with SSL support, drop the <filename>server.jks</filename> file into
  79. the tomcat <filename>conf</filename> directory and add the following connector to the
  80. <filename>server.xml</filename> file
  81. <programlisting>
  82. &lt;Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" scheme="https" secure="true"
  83. clientAuth="true" sslProtocol="TLS"
  84. keystoreFile="${catalina.home}/conf/server.jks"
  85. keystoreType="JKS" keystorePass="password"
  86. truststoreFile="${catalina.home}/conf/server.jks"
  87. truststoreType="JKS" truststorePass="password"
  88. /&gt;
  89. </programlisting>
  90. <parameter>clientAuth</parameter> can also be set to <parameter>want</parameter> if you
  91. still want SSL connections to succeed even if the client doesn't provide a certificate.
  92. Clients which don't present a certificate won't be able to access any objects secured by
  93. Spring Security unless you use a non-X.509 authentication mechanism, such as form
  94. authentication. </para>
  95. </section>
  96. </chapter>