samples.adoc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. [[springsecuritykerberossamples]]
  2. = Spring Security Kerberos Samples
  3. :figures: servlet/authentication/kerberos
  4. This part of the reference documentation is introducing samples
  5. projects. Samples can be compiled manually by building main
  6. distribution from
  7. https://github.com/spring-projects/spring-security-kerberos.
  8. [IMPORTANT]
  9. ====
  10. If you run sample as is it will not work until a correct configuration
  11. is applied. See notes below for specific samples.
  12. ====
  13. <<samples-sec-server-win-auth>> sample for Windows environment
  14. <<samples-sec-server-client-auth>> sample using server side authenticator
  15. <<samples-sec-server-spnego-form-auth>> sample using ticket validation
  16. with spnego and form
  17. <<samples-sec-client-rest-template>> sample for KerberosRestTemplate
  18. [[samples-sec-server-win-auth]]
  19. == Security Server Windows Auth Sample
  20. Goals of this sample:
  21. - In windows environment, User will be able to logon to application
  22. with Windows Active directory Credential which has been entered
  23. during log on to windows. There should not be any ask for
  24. userid/password credentials.
  25. - In non-windows environment, User will be presented with a screen
  26. to provide Active directory credentials.
  27. [source,yaml,indent=0]
  28. ----
  29. server:
  30. port: 8080
  31. app:
  32. ad-domain: EXAMPLE.ORG
  33. ad-server: ldap://WIN-EKBO0EQ7TS7.example.org/
  34. service-principal: HTTP/neo.example.org@EXAMPLE.ORG
  35. keytab-location: /tmp/tomcat.keytab
  36. ldap-search-base: dc=example,dc=org
  37. ldap-search-filter: "(| (userPrincipalName={0}) (sAMAccountName={0}))"
  38. ----
  39. In above you can see the default configuration for this sample. You
  40. can override these settings using a normal Spring Boot tricks like
  41. using command-line options or custom `application.yml` file.
  42. Run a server.
  43. [source,text,subs="attributes"]
  44. ----
  45. $ java -jar sec-server-win-auth-{spring-security-version}.jar
  46. ----
  47. [IMPORTANT]
  48. ====
  49. You may need to use custom kerberos config with Linux either by using
  50. `-Djava.security.krb5.conf=/path/to/krb5.ini` or
  51. `GlobalSunJaasKerberosConfig` bean.
  52. ====
  53. [NOTE]
  54. ====
  55. See xref:servlet/authentication/kerberos/appendix.adoc#setupwinkerberos[Setup Windows Domain Controller]
  56. for more instructions how to work with windows kerberos environment.
  57. ====
  58. Login to `Windows 8.1` using domain credentials and access sample
  59. image::{figures}/ie1.png[]
  60. image::{figures}/ie2.png[]
  61. Access sample application from a non windows vm and use domain
  62. credentials manually.
  63. image::{figures}/ff1.png[]
  64. image::{figures}/ff2.png[]
  65. image::{figures}/ff3.png[]
  66. [[samples-sec-server-client-auth]]
  67. == Security Server Side Auth Sample
  68. This sample demonstrates how server is able to authenticate user
  69. against kerberos environment using his credentials passed in via a
  70. form login.
  71. Run a server.
  72. [source,text,subs="attributes"]
  73. ----
  74. $ java -jar sec-server-client-auth-{spring-security-version}.jar
  75. ----
  76. [source,yaml,indent=0]
  77. ----
  78. server:
  79. port: 8080
  80. ----
  81. [[samples-sec-server-spnego-form-auth]]
  82. == Security Server Spnego and Form Auth Sample
  83. This sample demonstrates how a server can be configured to accept a
  84. Spnego based negotiation from a browser while still being able to fall
  85. back to a form based authentication.
  86. Using a `user1` principal xref:servlet/authentication/kerberos/appendix.adoc#setupmitkerberos[Setup MIT Kerberos],
  87. do a kerberos login manually using credentials.
  88. [source,text]
  89. ----
  90. $ kinit user1
  91. Password for user1@EXAMPLE.ORG:
  92. $ klist
  93. Ticket cache: FILE:/tmp/krb5cc_1000
  94. Default principal: user1@EXAMPLE.ORG
  95. Valid starting Expires Service principal
  96. 10/03/15 17:18:45 11/03/15 03:18:45 krbtgt/EXAMPLE.ORG@EXAMPLE.ORG
  97. renew until 11/03/15 17:18:40
  98. ----
  99. or using a keytab file.
  100. [source,text]
  101. ----
  102. $ kinit -kt user2.keytab user1
  103. $ klist
  104. Ticket cache: FILE:/tmp/krb5cc_1000
  105. Default principal: user2@EXAMPLE.ORG
  106. Valid starting Expires Service principal
  107. 10/03/15 17:25:03 11/03/15 03:25:03 krbtgt/EXAMPLE.ORG@EXAMPLE.ORG
  108. renew until 11/03/15 17:25:03
  109. ----
  110. Run a server.
  111. [source,text,subs="attributes"]
  112. ----
  113. $ java -jar sec-server-spnego-form-auth-{spring-security-version}.jar
  114. ----
  115. Now you should be able to open your browser and let it do Spnego
  116. authentication with existing ticket.
  117. [NOTE]
  118. ====
  119. See xref:servlet/authentication/kerberos/appendix.adoc#browserspnegoconfig[Configure Browsers for Spnego Negotiation]
  120. for more instructions for configuring browsers to use Spnego.
  121. ====
  122. [source,yaml,indent=0]
  123. ----
  124. server:
  125. port: 8080
  126. app:
  127. service-principal: HTTP/neo.example.org@EXAMPLE.ORG
  128. keytab-location: /tmp/tomcat.keytab
  129. ----
  130. [[samples-sec-client-rest-template]]
  131. == Security Client KerberosRestTemplate Sample
  132. This is a sample using a Spring RestTemplate to access Kerberos
  133. protected resource. You can use this together with
  134. <<samples-sec-server-spnego-form-auth>>.
  135. Default application is configured as shown below.
  136. [source,yaml,indent=0]
  137. ----
  138. app:
  139. user-principal: user2@EXAMPLE.ORG
  140. keytab-location: /tmp/user2.keytab
  141. access-url: http://neo.example.org:8080/hello
  142. ----
  143. Using a `user1` principal xref:servlet/authentication/kerberos/appendix.adoc#setupmitkerberos[Setup MIT Kerberos],
  144. do a kerberos login manually using credentials.
  145. [source,text,subs="attributes"]
  146. ----
  147. $ java -jar sec-client-rest-template-{spring-security-version}.jar --app.user-principal --app.keytab-location
  148. ----
  149. [NOTE]
  150. ====
  151. In above we simply set `app.user-principal` and `app.keytab-location`
  152. to empty values which disables a use of keytab file.
  153. ====
  154. If operation is succesfull you should see below output with `user1@EXAMPLE.ORG`.
  155. [source,text]
  156. ----
  157. <html xmlns="http://www.w3.org/1999/xhtml"
  158. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  159. <head>
  160. <title>Spring Security Kerberos Example</title>
  161. </head>
  162. <body>
  163. <h1>Hello user1@EXAMPLE.ORG!</h1>
  164. </body>
  165. </html>
  166. ----
  167. Or use a `user2` with a keytab file.
  168. [source,text,subs="attributes"]
  169. ----
  170. $ java -jar sec-client-rest-template-{spring-security-version}.jar
  171. ----
  172. If operation is succesfull you should see below output with `user2@EXAMPLE.ORG`.
  173. [source,text]
  174. ----
  175. <html xmlns="http://www.w3.org/1999/xhtml"
  176. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
  177. <head>
  178. <title>Spring Security Kerberos Example</title>
  179. </head>
  180. <body>
  181. <h1>Hello user2@EXAMPLE.ORG!</h1>
  182. </body>
  183. </html>
  184. ----