index.xml 207 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
  4. <!--
  5. * ========================================================================
  6. *
  7. * Copyright 2004 Acegi Technology Pty Limited
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * ========================================================================
  22. -->
  23. <book>
  24. <bookinfo>
  25. <title>Acegi Security System for Spring</title>
  26. <subtitle>Reference Documentation</subtitle>
  27. <releaseinfo>0.7-SNAPSHOT</releaseinfo>
  28. <authorgroup>
  29. <author>
  30. <firstname>Ben</firstname>
  31. <surname>Alex</surname>
  32. </author>
  33. </authorgroup>
  34. </bookinfo>
  35. <toc></toc>
  36. <preface id="preface">
  37. <title>Preface</title>
  38. <para>This document provides a reference guide to the Acegi Security
  39. System for Spring, which is a series of classes that deliver
  40. authentication and authorization services within the Spring
  41. Framework.</para>
  42. <para>I would like to acknowledge this reference was prepared using the
  43. DocBook configuration included with the Spring Framework. The Spring team
  44. in turn acknowledge Chris Bauer (Hibernate) for his assistance with their
  45. DocBook.</para>
  46. </preface>
  47. <chapter id="security">
  48. <title>Security</title>
  49. <sect1 id="security-before-you-begin">
  50. <title>Before You Begin</title>
  51. <para>For your security, each official release JAR of Acegi Security has
  52. been signed by the project leader. This does not in any way alter the
  53. liability disclaimer contained in the License, but it does ensure you
  54. are using a properly reviewed, official build of Acegi Security. Please
  55. refer to the <literal>readme.txt</literal> file in the root of the
  56. release distribution for instructions on how to validate the JARs are
  57. correctly signed, and which certificate has been used to sign
  58. them.</para>
  59. </sect1>
  60. <sect1 id="security-introduction">
  61. <title>Introduction</title>
  62. <para>The Acegi Security System for Spring provides authentication and
  63. authorization capabilities for Spring-powered projects, with full
  64. integration with popular web containers. The security architecture was
  65. designed from the ground up using "The Spring Way" of development, which
  66. includes using bean contexts, interceptors and interface-driven
  67. programming. As a consequence, the Acegi Security System for Spring is
  68. useful out-of-the-box for those seeking to secure their Spring-based
  69. applications, and can be easily adapted to complex customized
  70. requirements.</para>
  71. <para>Security involves two distinct operations, authentication and
  72. authorization. The former relates to resolving whether or not a caller
  73. is who they claim to be. Authorization on the other hand relates to
  74. determining whether or not an authenticated caller is permitted to
  75. perform a given operation.</para>
  76. <para>Throughout the Acegi Security System for Spring, the user, system
  77. or agent that needs to be authenticated is referred to as a "principal".
  78. The security architecture does not have a notion of roles or groups,
  79. which you may be familiar with from other security
  80. implementations.</para>
  81. <sect2 id="security-introduction-status">
  82. <title>Current Status</title>
  83. <para>The Acegi Security System for Spring is widely used by members
  84. of the Spring Community. The APIs are considered stable and only minor
  85. changes are expected. Having said that, like many other projects we
  86. need to strike a balance between backward compatibility and
  87. improvement. Effective version 0.6.1, Acegi Security uses the Apache
  88. Portable Runtime Project versioning guidelines, available from
  89. <literal>http://apr.apache.org/versioning.html</literal>.</para>
  90. <para>Some improvements are currently intended prior to the 1.0.0
  91. release. These are:</para>
  92. <itemizedlist spacing="compact">
  93. <listitem>
  94. <para>Replacing the Ant build with a Maven build. When this
  95. happens the <literal>lib</literal> directory will no longer be
  96. distributed in ZIP releases or hosted in CVS.</para>
  97. </listitem>
  98. <listitem>
  99. <para>"Remember me" functionality. Some discussion on this can be
  100. found at
  101. <literal>http://sourceforge.net/mailarchive/forum.php?thread_id=5177499&amp;forum_id=40659</literal>.</para>
  102. </listitem>
  103. <listitem>
  104. <para>A sample web application which demonstrates the access
  105. control list package.</para>
  106. </listitem>
  107. <listitem>
  108. <para>Implementation of an
  109. <literal>ObjectDefinitionSource</literal> that retrieves its
  110. details from a database.</para>
  111. </listitem>
  112. <listitem>
  113. <para>Deprecation of Acegi Security's various EH-CACHE-based cache
  114. implementations. Instead Acegi Security will provide new cache
  115. implementations which use Spring Framework's new (currently in
  116. CVS) <literal>EhCacheManagerFactoryBean</literal> factory. The
  117. deprecated classes may be removed from the 1.0.0 release.</para>
  118. </listitem>
  119. </itemizedlist>
  120. <para>Whilst this list is subject to change and not in any particular
  121. order, none of the above improvements are likely to result in changes
  122. to the API. The improvements are also relatively minor to implement.
  123. Users of Acegi Security System for Spring should therefore be
  124. comfortable depending on the current version of the project in their
  125. applications.</para>
  126. </sect2>
  127. </sect1>
  128. <sect1 id="security-high-level-design">
  129. <title>High Level Design</title>
  130. <sect2 id="security-high-level-design-key-components">
  131. <title>Key Components</title>
  132. <para>The Acegi Security System for Spring essentially comprises seven
  133. key functional parts:</para>
  134. <itemizedlist spacing="compact">
  135. <listitem>
  136. <para>An <literal>Authentication</literal> object which holds the
  137. principal, credentials and the authorities granted to the
  138. principal. The object can also store additional information
  139. associated with an authentication request, such as the source
  140. TCP/IP address.</para>
  141. </listitem>
  142. <listitem>
  143. <para>A <literal>ContextHolder</literal> which holds the
  144. <literal>Authentication</literal> object in a
  145. <literal>ThreadLocal</literal>-bound object.</para>
  146. </listitem>
  147. <listitem>
  148. <para>An <literal>AuthenticationManager</literal> to authenticate
  149. the <literal>Authentication</literal> object presented via the
  150. <literal>ContextHolder</literal>.</para>
  151. </listitem>
  152. <listitem>
  153. <para>An <literal>AccessDecisionManager</literal> to authorize a
  154. given operation.</para>
  155. </listitem>
  156. <listitem>
  157. <para>A <literal>RunAsManager</literal> to optionally replace the
  158. <literal>Authentication</literal> object whilst a given operation
  159. is being executed.</para>
  160. </listitem>
  161. <listitem>
  162. <para>A "secure object" interceptor, which coordinates the
  163. authentication, authorization, run-as replacement and execution of
  164. a given operation.</para>
  165. </listitem>
  166. <listitem>
  167. <para>An acess control list (ACL) management package, which can be
  168. used to obtain ACLs for domain object instances.</para>
  169. </listitem>
  170. </itemizedlist>
  171. <para>Secure objects refer to any type of object that can have
  172. security applied to it. A secure object must provide some form of
  173. callback, so that the security interceptor can transparently do its
  174. work as required, and callback the object when it is time for it to
  175. proceed with the requested operation. If secure objects cannot provide
  176. a native callback approach, a wrapper needs to be written so this
  177. becomes possible.</para>
  178. <para>Each secure object has its own package under
  179. <literal>net.sf.acegisecurity.intercept</literal>. Every other package
  180. in the security system is secure object independent, in that it can
  181. support any type of secure object presented.</para>
  182. <para>Only developers contemplating an entirely new way of
  183. intercepting and authorizing requests would need to use secure objects
  184. directly. For example, it would be possible to build a new secure
  185. object to secure calls to a messaging system that does not use
  186. <literal>MethodInvocation</literal>s. Most Spring applications will
  187. simply use the three currently supported secure object types
  188. (<literal>MethodInvocation</literal>, <literal>JoinPoint</literal> and
  189. <literal>FilterInterceptor</literal>) with complete
  190. transparency.</para>
  191. <para>Each of the seven key parts is discussed in detail throughout
  192. this document.</para>
  193. </sect2>
  194. <sect2 id="security-high-level-design-supported-secure-objects">
  195. <title>Supported Secure Objects</title>
  196. <para>The Acegi Security System for Spring currently supports three
  197. secure objects.</para>
  198. <para>The first handles an AOP Alliance
  199. <literal>MethodInvocation</literal>. This is the secure object type
  200. used to protect Spring beans. Developers will generally use this
  201. secure object type to secure their business objects. To make a
  202. standard Spring-hosted bean available as a
  203. <literal>MethodInvocation</literal>, the bean is simply published
  204. through a <literal>ProxyFactoryBean</literal> or
  205. <literal>BeanNameAutoProxyCreator</literal> or
  206. <literal>DefaultAdvisorAutoProxyCreator</literal>. Most Spring
  207. developers would already be familiar with these due to their use in
  208. transactions and other areas of Spring.</para>
  209. <para>The second type is an AspectJ <literal>JoinPoint</literal>.
  210. AspectJ has a particular use in securing domain object instances, as
  211. these are most often managed outside the Spring bean container. By
  212. using AspectJ, standard constructs such as <literal>new
  213. Person();</literal> can be used and full security will be applied to
  214. them by Acegi Security. The
  215. <literal>AspectJSecurityInterceptor</literal> is still managed by
  216. Spring, which creates the aspect singleton and wires it with the
  217. appropriate authentication managers, access decision managers and so
  218. on.</para>
  219. <para>The third type is a <literal>FilterInvocation</literal>. This is
  220. an object included with the Acegi Security System for Spring. It is
  221. created by an included filter and simply wraps the HTTP
  222. <literal>ServletRequest</literal>, <literal>ServletResponse</literal>
  223. and <literal>FilterChain</literal>. The
  224. <literal>FilterInvocation</literal> enables HTTP resources to be
  225. secured. Developers do not usually need to understand the mechanics of
  226. how this works, because they just add the filters to their
  227. <literal>web.xml</literal> and let the security system do its
  228. work.</para>
  229. </sect2>
  230. <sect2 id="security-high-level-design-configuration-attributes">
  231. <title>Configuration Attributes</title>
  232. <para>Every secure object can represent an infinite number of
  233. individual requests. For example, a
  234. <literal>MethodInvocation</literal> can represent the invocation of
  235. any method with any arguments, whilst a
  236. <literal>FilterInvocation</literal> can represent any HTTP URL.</para>
  237. <para>The Acegi Security System for Spring needs to record the
  238. configuration that applies to each of these possible requests. The
  239. security configuration of a request to
  240. <literal>BankManager.getBalance(int accountNumber)</literal> needs to
  241. be very different from the security configuration of a request to
  242. <literal>BankManager.approveLoan(int applicationNumber)</literal>.
  243. Similarly, the security configuration of a request to
  244. <literal>http://some.bank.com/index.htm</literal> needs to be very
  245. different from the security configuration of
  246. <literal>http://some.bank.com/manage/timesheet.jsp</literal>.</para>
  247. <para>To store the various security configurations associated with
  248. different requests, a configuration attribute is used. At an
  249. implementation level a configuration attribute is represented by the
  250. <literal>ConfigAttribute</literal> interface. One concrete
  251. implementation of <literal>ConfigAttribute</literal> is provided,
  252. <literal>SecurityConfig</literal>, which simply stores a configuration
  253. attribute as a <literal>String</literal>.</para>
  254. <para>The collection of <literal>ConfigAttribute</literal>s associated
  255. with a particular request is held in a
  256. <literal>ConfigAttributeDefinition</literal>. This concrete class is
  257. simply a holder of <literal>ConfigAttribute</literal>s and does
  258. nothing special.</para>
  259. <para>When a request is received by the security interceptor, it needs
  260. to determine which configuration attributes apply. In other words, it
  261. needs to find the <literal>ConfigAttributeDefinition</literal> which
  262. applies to the request. This decision is handled by the
  263. <literal>ObjectDefinitionSource</literal> interface. The main method
  264. provided by this interface is <literal>public
  265. ConfigAttributeDefinition getAttributes(Object object)</literal>, with
  266. the <literal>Object</literal> being the secure object. Recall the
  267. secure object contains details of the request, so the
  268. <literal>ObjectDefinitionSource</literal> implementation will be able
  269. to extract the details it requires to lookup the relevant
  270. <literal>ConfigAttributeDefinition</literal>.</para>
  271. </sect2>
  272. </sect1>
  273. <sect1 id="security-request-contexts">
  274. <title>Request Contexts</title>
  275. <sect2 id="security-contexts">
  276. <title>Contexts</title>
  277. <para>Many applications require a way of sharing objects between
  278. classes, but without resorting to passing them in method signatures.
  279. This is commonly achieved by using a <literal>ThreadLocal</literal>.
  280. The Acegi Security System for Spring uses
  281. <literal>ThreadLocal</literal> functionality and introduces the
  282. concept of "request contexts".</para>
  283. <para>By placing an object into a request context, that object becomes
  284. available to any other object on the current thread of execution. The
  285. request context is not passed around as a method parameter, but is
  286. held in a <literal>ThreadLocal</literal>. The Acegi Security System
  287. for Spring uses the request context to pass around the authentication
  288. request and response.</para>
  289. <para>A request context is a concrete implementation of the
  290. <literal>Context</literal> interface, which exposes a single
  291. method:</para>
  292. <programlisting>public void validate() throws ContextInvalidException;</programlisting>
  293. <para>This <literal>validate()</literal> method is called to confirm
  294. the <literal>Context</literal> is properly setup. An implementation
  295. will typically use this method to check that the objects it holds are
  296. properly setup.</para>
  297. <para>The <literal>ContextHolder</literal> class makes the
  298. <literal>Context</literal> available to the current thread of
  299. execution using a <literal>ThreadLocal</literal>. A
  300. <literal>ContextInterceptor</literal> is also provided, which is
  301. intended to be chained into the bean context using
  302. <literal>ProxyFactoryBean</literal>. The
  303. <literal>ContextInterceptor</literal> simply calls
  304. <literal>Context.validate()</literal>, which guarantees to business
  305. methods that a valid <literal>Context</literal> is available from the
  306. <literal>ContextHolder</literal>.</para>
  307. </sect2>
  308. <sect2 id="security-contexts-secure-contexts">
  309. <title>Secure Contexts</title>
  310. <para>The Acegi Security System for Spring requires the
  311. <literal>ContextHolder</literal> to contain a request context that
  312. implements the <literal>SecureContext</literal> interface. An
  313. implementation is provided named <literal>SecureContextImpl</literal>.
  314. The <literal>SecureContext</literal> simply extends the
  315. <literal>Context</literal> discussed above and adds a holder and
  316. validation for an <literal>Authentication</literal> object.</para>
  317. </sect2>
  318. <sect2 id="security-contexts-custom-contexts">
  319. <title>Custom Contexts</title>
  320. <para>Developers can create their own request context classes to store
  321. application-specific objects. Such request context classes will need
  322. to implement the <literal>Context</literal> interface. If the Acegi
  323. Security System for Spring is to be used, developers must ensure any
  324. custom request contexts implement the <literal>SecureContext</literal>
  325. interface.</para>
  326. </sect2>
  327. </sect1>
  328. <sect1 id="security-interception">
  329. <title>Security Interception</title>
  330. <sect2 id="security-interception-all-secure-objects">
  331. <title>All Secure Objects</title>
  332. <para>As described in the High Level Design section, each secure
  333. object has its own security interceptor which is responsible for
  334. handling each request. Handling involves a number of
  335. operations:</para>
  336. <orderedlist>
  337. <listitem>
  338. <para>Store the configuration attributes that are associated with
  339. each secure request.</para>
  340. </listitem>
  341. <listitem>
  342. <para>Extract the <literal>ConfigAttributeDefinition</literal>
  343. that applies to the request from the relevant
  344. <literal>ObjectDefinitionSource</literal>.</para>
  345. </listitem>
  346. <listitem>
  347. <para>Obtain the <literal>Authentication</literal> object from the
  348. <literal>SecureContext</literal>, which is held in the
  349. <literal>ContextHolder</literal>.</para>
  350. </listitem>
  351. <listitem>
  352. <para>Pass the <literal>Authentication</literal> object to the
  353. <literal>AuthenticationManager</literal>, update the
  354. <literal>ContextHolder</literal> with the response.</para>
  355. </listitem>
  356. <listitem>
  357. <para>Pass the <literal>Authentication</literal> object, the
  358. <literal>ConfigAttributeDefinition</literal>, and the secure
  359. object to the <literal>AccessDecisionManager</literal>.</para>
  360. </listitem>
  361. <listitem>
  362. <para>Pass the <literal>Authentication</literal> object, the
  363. <literal>ConfigAttributeDefinition</literal>, and the secure
  364. object to the <literal>RunAsManager</literal>.</para>
  365. </listitem>
  366. <listitem>
  367. <para>If the <literal>RunAsManager</literal> returns a new
  368. <literal>Authentication</literal> object, update the
  369. <literal>ContextHolder</literal> with it.</para>
  370. </listitem>
  371. <listitem>
  372. <para>Proceed with the request execution of the secure
  373. object.</para>
  374. </listitem>
  375. <listitem>
  376. <para>If the <literal>RunAsManager</literal> earlier returned a
  377. new <literal>Authentication</literal> object, update the
  378. <literal>ContextHolder</literal> with the
  379. <literal>Authentication</literal> object that was previously
  380. returned by the <literal>AuthenticationManager</literal>.</para>
  381. </listitem>
  382. <listitem>
  383. <para>Return any result received from the secure object
  384. execution.</para>
  385. </listitem>
  386. </orderedlist>
  387. <para>Whilst this may seem quite involved, don't worry. Developers
  388. interact with the security process by simply implementing basic
  389. interfaces (such as <literal>AccessDecisionManager</literal>), which
  390. are fully documented below.</para>
  391. <para>The <literal>AbstractSecurityInterceptor</literal> handles the
  392. majority of the flow listed above. Each secure object has its own
  393. security interceptor which subclasses
  394. <literal>AbstractSecurityInterceptor</literal>. Each of these secure
  395. object-specific security interceptors are discussed below.</para>
  396. </sect2>
  397. <sect2 id="security-interception-aopalliance">
  398. <title>AOP Alliance (MethodInvocation) Security Interceptor</title>
  399. <para>To secure <literal>MethodInvocation</literal>s, developers
  400. simply add a properly configured
  401. <literal>MethodSecurityInterceptor</literal> into the application
  402. context. Next the beans requiring security are chained into the
  403. interceptor. This chaining is accomplished using Spring’s
  404. <literal>ProxyFactoryBean</literal> or
  405. <literal>BeanNameAutoProxyCreator</literal>, as commonly used by many
  406. other parts of Spring (refer to the sample application for examples).
  407. Alternatively, Acegi Security provides a
  408. <literal>MethodDefinitionSourceAdvisor</literal> which may be used
  409. with Spring's <literal>DefaultAdvisorAutoProxyCreator</literal> to
  410. automatically chain the security interceptor in front of any beans
  411. defined against the <literal>MethodSecurityInterceptor</literal>. The
  412. <literal>MethodSecurityInterceptor</literal> itself is configured as
  413. follows:</para>
  414. <para><programlisting>&lt;bean id="bankManagerSecurity" class="net.sf.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"&gt;
  415. &lt;property name="validateConfigAttributes"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
  416. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  417. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  418. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  419. &lt;property name="objectDefinitionSource"&gt;
  420. &lt;value&gt;
  421. net.sf.acegisecurity.context.BankManager.delete*=ROLE_SUPERVISOR,RUN_AS_SERVER
  422. net.sf.acegisecurity.context.BankManager.getBalance=ROLE_TELLER,ROLE_SUPERVISOR,BANKSECURITY_CUSTOMER,RUN_AS_SERVER
  423. &lt;/value&gt;
  424. &lt;/property&gt;
  425. &lt;/bean&gt;</programlisting></para>
  426. <para>As shown above, the <literal>MethodSecurityInterceptor</literal>
  427. is configured with a reference to an
  428. <literal>AuthenticationManager</literal>,
  429. <literal>AccessDecisionManager</literal> and
  430. <literal>RunAsManager</literal>, which are each discussed in separate
  431. sections below. The <literal>MethodSecurityInterceptor</literal> is
  432. also configured with configuration attributes that apply to different
  433. method signatures. A full discussion of configuration attributes is
  434. provided in the High Level Design section of this document.</para>
  435. <para>The <literal>MethodSecurityInterceptor</literal> can be
  436. configured with configuration attributes in three ways. The first is
  437. via a property editor and the application context, which is shown
  438. above. The second is via defining the configuration attributes in your
  439. source code using Jakarta Commons Attributes. The third is via writing
  440. your own <literal>ObjectDefinitionSource</literal>, although this is
  441. beyond the scope of this document. Irrespective of the approach used,
  442. the <literal>ObjectDefinitionSource</literal> is responsible for
  443. returning a <literal>ConfigAttributeDefinition</literal> object that
  444. contains all of the configuration attributes associated with a single
  445. secure method.</para>
  446. <para>It should be noted that the
  447. <literal>MethodSecurityInterceptor.setObjectDefinitionSource()</literal>
  448. method actually expects an instance of
  449. <literal>MethodDefinitionSource</literal>. This is a marker interface
  450. which subclasses <literal>ObjectDefinitionSource</literal>. It simply
  451. denotes the <literal>ObjectDefinitionSource</literal> understands
  452. <literal>MethodInvocation</literal>s. In the interests of simplicity
  453. we'll continue to refer to the
  454. <literal>MethodDefinitionSource</literal> as an
  455. <literal>ObjectDefinitionSource</literal>, as the distinction is of
  456. little relevance to most users of the
  457. <literal>MethodSecurityInterceptor</literal>.</para>
  458. <para>If using the application context property editor approach (as
  459. shown above), commas are used to delimit the different configuration
  460. attributes that apply to a given method pattern. Each configuration
  461. attribute is assigned into its own <literal>SecurityConfig</literal>
  462. object. The <literal>SecurityConfig</literal> object is discussed in
  463. the High Level Design section.</para>
  464. <para>If using the Jakarta Commons Attributes approach, your bean
  465. context will be configured differently:</para>
  466. <para><programlisting>&lt;bean id="attributes" class="org.springframework.metadata.commons.CommonsAttributes"/&gt;
  467. &lt;bean id="objectDefinitionSource" class="net.sf.acegisecurity.intercept.method.MethodDefinitionAttributes"&gt;
  468. &lt;property name="attributes"&gt;&lt;ref local="attributes"/&gt;&lt;/property&gt;
  469. &lt;/bean&gt;
  470. &lt;bean id="bankManagerSecurity" class="net.sf.acegisecurity.intercept.method.MethodSecurityInterceptor"&gt;
  471. &lt;property name="validateConfigAttributes"&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
  472. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  473. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  474. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  475. &lt;property name="objectDefinitionSource"&gt;&lt;ref bean="objectDefinitionSource"/&gt;&lt;/property&gt;
  476. &lt;/bean&gt;</programlisting></para>
  477. <para>In addition, your source code will contain Jakarta Commons
  478. Attributes tags that refer to a concrete implementation of
  479. <literal>ConfigAttribute</literal>. The following example uses the
  480. <literal>SecurityConfig</literal> implementation to represent the
  481. configuration attributes, and results in the same security
  482. configuration as provided by the property editor approach
  483. above:</para>
  484. <para><programlisting>public interface BankManager {
  485. /**
  486. * @@SecurityConfig("ROLE_SUPERVISOR")
  487. * @@SecurityConfig("RUN_AS_SERVER")
  488. */
  489. public void deleteSomething(int id);
  490. /**
  491. * @@SecurityConfig("ROLE_SUPERVISOR")
  492. * @@SecurityConfig("RUN_AS_SERVER")
  493. */
  494. public void deleteAnother(int id);
  495. /**
  496. * @@SecurityConfig("ROLE_TELLER")
  497. * @@SecurityConfig("ROLE_SUPERVISOR")
  498. * @@SecurityConfig("BANKSECURITY_CUSTOMER")
  499. * @@SecurityConfig("RUN_AS_SERVER")
  500. */
  501. public float getBalance(int id);
  502. }</programlisting></para>
  503. <para>You might have noticed the
  504. <literal>validateConfigAttributes</literal> property in the above
  505. <literal>MethodSecurityInterceptor</literal> examples. When set to
  506. <literal>true</literal> (the default), at startup time the
  507. <literal>MethodSecurityInterceptor</literal> will evaluate if the
  508. provided configuration attributes are valid. It does this by checking
  509. each configuration attribute can be processed by either the
  510. <literal>AccessDecisionManager</literal> or the
  511. <literal>RunAsManager</literal>. If neither of these can process a
  512. given configuration attribute, an exception is thrown. If using the
  513. Jakarta Commons Attributes method of configuration, you should set
  514. <literal>validateConfigAttributes</literal> to
  515. <literal>false</literal>.</para>
  516. </sect2>
  517. <sect2 id="security-interception-aspectj">
  518. <title>AspectJ (JoinPoint) Security Interceptor</title>
  519. <para>The AspectJ security interceptor is very similar to the AOP
  520. Alliance security interceptor discussed in the previous section.
  521. Indeed we will only discuss the differences in this section.</para>
  522. <para>The AspectJ interceptor is named
  523. <literal>AspectJSecurityInterceptor</literal>. Unlike the AOP Alliance
  524. security interceptor, which relies on the Spring application context
  525. to weave in the security interceptor via proxying, the
  526. <literal>AspectJSecurityInterceptor</literal> is weaved in via the
  527. AspectJ compiler. It would not be uncommon to use both types of
  528. security interceptors in the same application, with
  529. <literal>AspectJSecurityInterceptor</literal> being used for domain
  530. object instance security and the AOP Alliance
  531. <literal>MethodSecurityInterceptor</literal> being used for services
  532. layer security.</para>
  533. <para>Let's first consider how the
  534. <literal>AspectJSecurityInterceptor</literal> is configured in the
  535. Spring application context:</para>
  536. <para><programlisting>&lt;bean id="bankManagerSecurity" class="net.sf.acegisecurity.intercept.method.aspectj.AspectJSecurityInterceptor"&gt;
  537. &lt;property name="validateConfigAttributes"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
  538. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  539. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  540. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  541. &lt;property name="objectDefinitionSource"&gt;
  542. &lt;value&gt;
  543. net.sf.acegisecurity.context.BankManager.delete*=ROLE_SUPERVISOR,RUN_AS_SERVER
  544. net.sf.acegisecurity.context.BankManager.getBalance=ROLE_TELLER,ROLE_SUPERVISOR,BANKSECURITY_CUSTOMER,RUN_AS_SERVER
  545. &lt;/value&gt;
  546. &lt;/property&gt;
  547. &lt;/bean&gt;</programlisting></para>
  548. <para>As you can see, aside from the class name, the
  549. <literal>AspectJSecurityInterceptor</literal> is exactly the same as
  550. the AOP Alliance security interceptor. Indeed the two interceptors can
  551. share the same <literal>objectDefinitionSource</literal>, as the
  552. <literal>ObjectDefinitionSource</literal> works with
  553. <literal>java.lang.reflect.Method</literal>s rather than an AOP
  554. library-specific class. Of course, your access decisions have access
  555. to the relevant AOP library-specific invocation (ie
  556. <literal>MethodInvocation</literal> or <literal>JoinPoint</literal>)
  557. and as such can consider a range of addition criteria when making
  558. access decisions (such as method arguments).</para>
  559. <para>Next you'll need to define an AspectJ <literal>aspect</literal>.
  560. For example:</para>
  561. <para><programlisting>package net.sf.acegisecurity.samples.aspectj;
  562. import net.sf.acegisecurity.intercept.method.aspectj.AspectJSecurityInterceptor;
  563. import net.sf.acegisecurity.intercept.method.aspectj.AspectJCallback;
  564. import org.springframework.beans.factory.InitializingBean;
  565. public aspect DomainObjectInstanceSecurityAspect implements InitializingBean {
  566. private AspectJSecurityInterceptor securityInterceptor;
  567. pointcut domainObjectInstanceExecution(): target(PersistableEntity)
  568. &amp;&amp; execution(public * *(..)) &amp;&amp; !within(DomainObjectInstanceSecurityAspect);
  569. Object around(): domainObjectInstanceExecution() {
  570. if (this.securityInterceptor != null) {
  571. AspectJCallback callback = new AspectJCallback() {
  572. public Object proceedWithObject() {
  573. return proceed();
  574. }
  575. };
  576. return this.securityInterceptor.invoke(thisJoinPoint, callback);
  577. } else {
  578. return proceed();
  579. }
  580. }
  581. public AspectJSecurityInterceptor getSecurityInterceptor() {
  582. return securityInterceptor;
  583. }
  584. public void setSecurityInterceptor(AspectJSecurityInterceptor securityInterceptor) {
  585. this.securityInterceptor = securityInterceptor;
  586. }
  587. public void afterPropertiesSet() throws Exception {
  588. if (this.securityInterceptor == null)
  589. throw new IllegalArgumentException("securityInterceptor required");
  590. }
  591. }</programlisting></para>
  592. <para>In the above example, the security interceptor will be applied
  593. to every instance of <literal>PersistableEntity</literal>, which is an
  594. abstract class not shown (you can use any other class or
  595. <literal>pointcut</literal> expression you like). For those curious,
  596. <literal>AspectJCallback</literal> is needed because the
  597. <literal>proceed();</literal> statement has special meaning only
  598. within an <literal>around()</literal> body. The
  599. <literal>AspectJSecurityInterceptor</literal> calls this anonymous
  600. <literal>AspectJCallback</literal> class when it wants the target
  601. object to continue.</para>
  602. <para>You will need to configure Spring to load the aspect and wire it
  603. with the <literal>AspectJSecurityInterceptor</literal>. A bean
  604. declaration which achieves this is shown below:</para>
  605. <para><programlisting>&lt;bean id="domainObjectInstanceSecurityAspect"
  606. class="net.sf.acegisecurity.samples.aspectj.DomainObjectInstanceSecurityAspect"
  607. factory-method="aspectOf"&gt;
  608. &lt;property name="securityInterceptor"&gt;&lt;ref bean="aspectJSecurityInterceptor"/&gt;&lt;/property&gt;
  609. &lt;/bean&gt;</programlisting></para>
  610. <para>That's it! Now you can create your beans from anywhere within
  611. your application, using whatever means you think fit (eg <literal>new
  612. Person();</literal>) and they will have the security interceptor
  613. applied.</para>
  614. </sect2>
  615. <sect2 id="security-interception-filterinvocation">
  616. <title>FilterInvocation Security Interceptor</title>
  617. <para>To secure <literal>FilterInvocation</literal>s, developers need
  618. to add a filter to their <literal>web.xml</literal> that delegates to
  619. the <literal>SecurityEnforcementFilter</literal>. A typical
  620. configuration example is provided below: <programlisting>&lt;filter&gt;
  621. &lt;filter-name&gt;Acegi HTTP Request Security Filter&lt;/filter-name&gt;
  622. &lt;filter-class&gt;net.sf.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  623. &lt;init-param&gt;
  624. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  625. &lt;param-value&gt;net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter&lt;/param-value&gt;
  626. &lt;/init-param&gt;
  627. &lt;/filter&gt;
  628. &lt;filter-mapping&gt;
  629. &lt;filter-name&gt;Acegi HTTP Request Security Filter&lt;/filter-name&gt;
  630. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  631. &lt;/filter-mapping&gt;</programlisting></para>
  632. <para>Notice that the filter is actually a
  633. <literal>FilterToBeanProxy</literal>. Most of the filters used by the
  634. Acegi Security System for Spring use this class. Refer to the Filters
  635. section to learn more about this bean.</para>
  636. <para>In the application context you will need to configure three
  637. beans:</para>
  638. <programlisting>&lt;bean id="securityEnforcementFilter" class="net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter"&gt;
  639. &lt;property name="filterSecurityInterceptor"&gt;&lt;ref bean="filterInvocationInterceptor"/&gt;&lt;/property&gt;
  640. &lt;property name="authenticationEntryPoint"&gt;&lt;ref bean="authenticationEntryPoint"/&gt;&lt;/property&gt;
  641. &lt;/bean&gt;
  642. &lt;bean id="authenticationEntryPoint" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint"&gt;
  643. &lt;property name="loginFormUrl"&gt;&lt;value&gt;/acegilogin.jsp&lt;/value&gt;&lt;/property&gt;
  644. &lt;property name="forceHttps"&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
  645. &lt;/bean&gt;
  646. &lt;bean id="filterInvocationInterceptor" class="net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor"&gt;
  647. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  648. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  649. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  650. &lt;property name="objectDefinitionSource"&gt;
  651. &lt;value&gt;
  652. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  653. \A/secure/super/.*\Z=ROLE_WE_DONT_HAVE
  654. \A/secure/.*\Z=ROLE_SUPERVISOR,ROLE_TELLER
  655. &lt;/value&gt;
  656. &lt;/property&gt;
  657. &lt;/bean&gt;</programlisting>
  658. <para>The <literal>AuthenticationEntryPoint</literal> will be called
  659. if the user requests a secure HTTP resource but they are not
  660. authenticated. The class handles presenting the appropriate response
  661. to the user so that authentication can begin. Three concrete
  662. implementations are provided with the Acegi Security System for
  663. Spring: <literal>AuthenticationProcessingFilterEntryPoint</literal>
  664. for commencing a form-based authentication,
  665. <literal>BasicProcessingFilterEntryPoint</literal> for commencing a
  666. Http Basic authentication process, and
  667. <literal>CasProcessingFilterEntryPoint</literal> for commencing a Yale
  668. Central Authentication Service (CAS) login. The
  669. <literal>AuthenticationProcessingFilterEntryPoint</literal> and
  670. <literal>CasProcessingFilterEntryPoint</literal> have optional
  671. properties related to forcing the use of HTTPS, so please refer to the
  672. JavaDocs if you require this.</para>
  673. <para>The <literal>PortMapper</literal> provides information on which
  674. HTTPS ports correspond to which HTTP ports. This is used by the
  675. <literal>AuthenticationProcessingFilterEntryPoint</literal> and
  676. several other beans. The default implementation,
  677. <literal>PortMapperImpl</literal>, knows the common HTTP ports 80 and
  678. 8080 map to HTTPS ports 443 and 8443 respectively. You can customise
  679. this mapping if desired.</para>
  680. <para>The <literal>SecurityEnforcementFilter</literal> primarily
  681. provides session management support and initiates authentication when
  682. required. It delegates actual <literal>FilterInvocation</literal>
  683. security decisions to the configured
  684. <literal>FilterSecurityInterceptor</literal>.</para>
  685. <para>Like any other security interceptor, the
  686. <literal>FilterSecurityInterceptor</literal> requires a reference to
  687. an <literal>AuthenticationManager</literal>,
  688. <literal>AccessDecisionManager</literal> and
  689. <literal>RunAsManager</literal>, which are each discussed in separate
  690. sections below. The <literal>FilterSecurityInterceptor</literal> is
  691. also configured with configuration attributes that apply to different
  692. HTTP URL requests. A full discussion of configuration attributes is
  693. provided in the High Level Design section of this document.</para>
  694. <para>The <literal>FilterSecurityInterceptor</literal> can be
  695. configured with configuration attributes in two ways. The first is via
  696. a property editor and the application context, which is shown above.
  697. The second is via writing your own
  698. <literal>ObjectDefinitionSource</literal>, although this is beyond the
  699. scope of this document. Irrespective of the approach used, the
  700. <literal>ObjectDefinitionSource</literal> is responsible for returning
  701. a <literal>ConfigAttributeDefinition</literal> object that contains
  702. all of the configuration attributes associated with a single secure
  703. HTTP URL.</para>
  704. <para>It should be noted that the
  705. <literal>FilterSecurityInterceptor.setObjectDefinitionSource()</literal>
  706. method actually expects an instance of
  707. <literal>FilterInvocationDefinitionSource</literal>. This is a marker
  708. interface which subclasses <literal>ObjectDefinitionSource</literal>.
  709. It simply denotes the <literal>ObjectDefinitionSource</literal>
  710. understands <literal>FilterInvocation</literal>s. In the interests of
  711. simplicity we'll continue to refer to the
  712. <literal>FilterInvocationDefinitionSource</literal> as an
  713. <literal>ObjectDefinitionSource</literal>, as the distinction is of
  714. little relevance to most users of the
  715. <literal>FilterSecurityInterceptor</literal>.</para>
  716. <para>If using the application context property editor approach (as
  717. shown above), commas are used to delimit the different configuration
  718. attributes that apply to each HTTP URL. Each configuration attribute
  719. is assigned into its own <literal>SecurityConfig</literal> object. The
  720. <literal>SecurityConfig</literal> object is discussed in the High
  721. Level Design section. The <literal>ObjectDefinitionSource</literal>
  722. created by the property editor,
  723. <literal>FilterInvocationDefinitionSource</literal>, matches
  724. configuration attributes against <literal>FilterInvocations</literal>
  725. based on expression evaluation of the request URL. Two standard
  726. expression syntaxes are supported. The default is to treat all
  727. expressions as regular expressions. Alternatively, the presence of a
  728. <literal>PATTERN_TYPE_APACHE_ANT</literal> directive will cause all
  729. expressions to be treated as Apache Ant paths. It is not possible to
  730. mix expression syntaxes within the same definition. For example, the
  731. earlier configuration could be generated using Apache Ant paths as
  732. follows:</para>
  733. <para><programlisting>&lt;bean id="filterInvocationInterceptor" class="net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor"&gt;
  734. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  735. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  736. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  737. &lt;property name="objectDefinitionSource"&gt;
  738. &lt;value&gt;
  739. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  740. PATTERN_TYPE_APACHE_ANT
  741. /secure/super/**=ROLE_WE_DONT_HAVE
  742. /secure/**=ROLE_SUPERVISOR,ROLE_TELLER
  743. &lt;/value&gt;
  744. &lt;/property&gt;
  745. &lt;/bean&gt;</programlisting></para>
  746. <para>Irrespective of the type of expression syntax used, expressions
  747. are always evaluated in the order they are defined. Thus it is
  748. important that more specific expressions are defined higher in the
  749. list than less specific expressions. This is reflected in our example
  750. above, where the more specific <literal>/secure/super/</literal>
  751. pattern appears higher than the less specific
  752. <literal>/secure/</literal> pattern. If they were reversed, the
  753. <literal>/secure/</literal> pattern would always match and the
  754. <literal>/secure/super/</literal> pattern would never be
  755. evaluated.</para>
  756. <para>The special keyword
  757. <literal>CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON</literal> causes
  758. the <literal>FilterInvocationDefinitionSource</literal> to
  759. automatically convert a request URL to lowercase before comparison
  760. against the expressions. Whilst by default the case of the request URL
  761. is not converted, it is generally recommended to use
  762. <literal>CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON</literal> and
  763. write each expression assuming lowercase.</para>
  764. <para>As with other security interceptors, the
  765. <literal>validateConfigAttributes</literal> property is observed. When
  766. set to <literal>true</literal> (the default), at startup time the
  767. <literal>FilterSecurityInterceptor</literal> will evaluate if the
  768. provided configuration attributes are valid. It does this by checking
  769. each configuration attribute can be processed by either the
  770. <literal>AccessDecisionManager</literal> or the
  771. <literal>RunAsManager</literal>. If neither of these can process a
  772. given configuration attribute, an exception is thrown.</para>
  773. </sect2>
  774. </sect1>
  775. <sect1 id="security-authentication">
  776. <title>Authentication</title>
  777. <sect2 id="security-authentication-requests">
  778. <title>Authentication Requests</title>
  779. <para>Authentication requires a way for client code to present its
  780. security identification to the Acegi Security System for Spring. This
  781. is the role of the <literal>Authentication</literal> interface. The
  782. <literal>Authentication</literal> interface holds three important
  783. objects: the principal (the identity of the caller), the credentials
  784. (the proof of the identity of the caller, such as a password), and the
  785. authorities that have been granted to the principal. The principal and
  786. its credentials are populated by the client code, whilst the granted
  787. authorities are populated by the
  788. <literal>AuthenticationManager</literal>. The Acegi Security System
  789. for Spring includes several concrete <literal>Authentication</literal>
  790. implementations:</para>
  791. <itemizedlist spacing="compact">
  792. <listitem>
  793. <para><literal>UsernamePasswordAuthenticationToken</literal>
  794. allows a username and password to be presented as the principal
  795. and credentials respectively. It is also what is created by the
  796. HTTP Session Authentication system.</para>
  797. </listitem>
  798. <listitem>
  799. <para><literal>TestingAuthenticationToken</literal> facilitates
  800. unit testing by automatically being considered an authenticated
  801. object by its associated
  802. <literal>AuthenticationProvider</literal>.</para>
  803. </listitem>
  804. <listitem>
  805. <para><literal>RunAsUserToken</literal> is used by the default
  806. run-as authentication replacement implementation. This is
  807. discussed further in the Run-As Authentication Replacement
  808. section.</para>
  809. </listitem>
  810. <listitem>
  811. <para><literal>CasAuthenticationToken</literal> is used to
  812. represent a successful Yale Central Authentication Service (CAS)
  813. authentication. This is discussed further in the CAS
  814. section.</para>
  815. </listitem>
  816. <listitem>
  817. <para><literal>PrincipalAcegiUserToken</literal> and
  818. <literal>JettyAcegiUserToken</literal> implement
  819. <literal>AuthByAdapter</literal> (a subclass of
  820. <literal>Authentication</literal>) and are used whenever
  821. authentication is completed by Acegi Security System for Spring
  822. container adapters. This is discussed further in the Container
  823. Adapters section.</para>
  824. </listitem>
  825. </itemizedlist>
  826. <para>The authorities granted to a principal are represented by the
  827. <literal>GrantedAuthority</literal> interface. The
  828. <literal>GrantedAuthority</literal> interface is discussed at length
  829. in the Authorization section.</para>
  830. </sect2>
  831. <sect2 id="security-authentication-manager">
  832. <title>Authentication Manager</title>
  833. <para>As discussed in the Security Interception section, the
  834. <literal>AbstractSecurityInterceptor</literal> extracts the
  835. <literal>Authentication</literal> object from the
  836. <literal>SecureContext</literal> in the
  837. <literal>ContextHolder</literal>. This is then passed to an
  838. <literal>AuthenticationManager</literal>. The
  839. <literal>AuthenticationManager</literal> interface is very
  840. simple:</para>
  841. <programlisting>public Authentication authenticate(Authentication authentication) throws AuthenticationException;</programlisting>
  842. <para>Implementations of <literal>AuthenticationManager</literal> are
  843. required to throw an <literal>AuthenticationException</literal> should
  844. authentication fail, or return a fully populated
  845. <literal>Authentication</literal> object. In particular, the returned
  846. <literal>Authentication</literal> object should contain an array of
  847. <literal>GrantedAuthority</literal> objects. The
  848. <literal>SecurityInterceptor</literal> places the populated
  849. <literal>Authentication</literal> object back in the
  850. <literal>SecureContext</literal> in the
  851. <literal>ContextHolder</literal>, overwriting the original
  852. <literal>Authentication</literal> object.</para>
  853. <para>The <literal>AuthenticationException</literal> has a number of
  854. subclasses. The most important are
  855. <literal>BadCredentialsException</literal> (an incorrect principal or
  856. credentials), <literal>DisabledException</literal> and
  857. <literal>LockedException</literal>. The latter two exceptions indicate
  858. the principal was found, but the credentials were not checked and
  859. authentication is denied. An
  860. <literal>AuthenticationServiceException</literal> is also provided,
  861. which indicates the authentication system could not process the
  862. request (eg a database was unavailable).</para>
  863. </sect2>
  864. <sect2 id="security-authentication-provider">
  865. <title>Provider-Based Authentication</title>
  866. <para>Whilst the basic <literal>Authentication</literal> and
  867. <literal>AuthenticationManager</literal> interfaces enable users to
  868. develop their own authentication systems, users should consider using
  869. the provider-based authentication packages provided by the Acegi
  870. Security System for Spring. The key class,
  871. <literal>ProviderManager</literal>, is configured via the bean context
  872. with a list of <literal>AuthenticationProvider</literal>s:</para>
  873. <para><programlisting>&lt;bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager"&gt;
  874. &lt;property name="providers"&gt;
  875. &lt;list&gt;
  876. &lt;ref bean="daoAuthenticationProvider"/&gt;
  877. &lt;ref bean="someOtherAuthenticationProvider"/&gt;
  878. &lt;/list&gt;
  879. &lt;/property&gt;
  880. &lt;/bean&gt;</programlisting></para>
  881. <para><literal>ProviderManager</literal> calls a series of registered
  882. <literal>AuthenticationProvider</literal> implementations, until one
  883. is found that indicates it is able to authenticate a given
  884. <literal>Authentication</literal> class. When the first compatible
  885. <literal>AuthenticationProvider</literal> is located, it is passed the
  886. authentication request. The <literal>AuthenticationProvider</literal>
  887. will then either throw an <literal>AuthenticationException</literal>
  888. or return a fully populated <literal>Authentication</literal>
  889. object.</para>
  890. <para>Note the <literal>ProviderManager</literal> may throw a
  891. <literal>ProviderNotFoundException</literal> (a subclass of
  892. <literal>AuthenticationException</literal>) if it none of the
  893. registered <literal>AuthenticationProviders</literal> can validate the
  894. <literal>Authentication</literal> object.</para>
  895. <para>Several <literal>AuthenticationProvider</literal>
  896. implementations are provided with the Acegi Security System for
  897. Spring:</para>
  898. <para><itemizedlist spacing="compact">
  899. <listitem>
  900. <para><literal>TestingAuthenticationProvider</literal> is able
  901. to authenticate a <literal>TestingAuthenticationToken</literal>.
  902. The limit of its authentication is simply to treat whatever is
  903. contained in the <literal>TestingAuthenticationToken</literal>
  904. as valid. This makes it ideal for use during unit testing, as
  905. you can create an <literal>Authentication</literal> object with
  906. precisely the <literal>GrantedAuthority</literal> objects
  907. required for calling a given method. You definitely would not
  908. register this <literal>AuthenticationProvider</literal> on a
  909. production system.</para>
  910. </listitem>
  911. <listitem>
  912. <para><literal>DaoAuthenticationProvider</literal> is able to
  913. authenticate a
  914. <literal>UsernamePasswordAuthenticationToken</literal> by
  915. accessing an authentication respository via a data access
  916. object. This is discussed further below, as it is the main way
  917. authentication is initially handled.</para>
  918. </listitem>
  919. <listitem>
  920. <para><literal>RunAsImplAuthenticationToken</literal> is able to
  921. authenticate a <literal>RunAsUserToken</literal>. This is
  922. discussed further in the Run-As Authentication Replacement
  923. section. You would not register this
  924. <literal>AuthenticationProvider</literal> if you were not using
  925. run-as replacement.</para>
  926. </listitem>
  927. <listitem>
  928. <para><literal>AuthByAdapterProvider</literal> is able to
  929. authenticate any <literal>AuthByAdapter</literal> (a subclass of
  930. <literal>Authentication</literal> used with container adapters).
  931. This is discussed further in the Container Adapters section. You
  932. would not register this
  933. <literal>AuthenticationProvider</literal> if you were not using
  934. container adapters.</para>
  935. </listitem>
  936. <listitem>
  937. <para><literal>CasAuthenticationProvider</literal> is able to
  938. authenticate Yale Central Authentication Service (CAS) tickets.
  939. This is discussed further in the CAS Single Sign On
  940. section.</para>
  941. </listitem>
  942. <listitem>
  943. <para><literal>JaasAuthenticationProvider</literal> is able to
  944. delegate authentication requests to a JAAS
  945. <literal>LoginModule</literal>. This is discussed further
  946. below.</para>
  947. </listitem>
  948. </itemizedlist></para>
  949. </sect2>
  950. <sect2 id="security-authentication-provider-dao">
  951. <title>Data Access Object Authentication Provider</title>
  952. <para>The Acegi Security System for Spring includes a
  953. production-quality <literal>AuthenticationProvider</literal>
  954. implementation called <literal>DaoAuthenticationProvider</literal>.
  955. This authentication provider is able to authenticate a
  956. <literal>UsernamePasswordAuthenticationToken</literal> by obtaining
  957. authentication details from a data access object configured at bean
  958. creation time:</para>
  959. <para><programlisting>&lt;bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;
  960. &lt;property name="authenticationDao"&gt;&lt;ref bean="inMemoryDaoImpl"/&gt;&lt;/property&gt;
  961. &lt;property name="saltSource"&gt;&lt;ref bean="saltSource"/&gt;&lt;/property&gt;
  962. &lt;property name="passwordEncoder"&gt;&lt;ref bean="passwordEncoder"/&gt;&lt;/property&gt;
  963. &lt;/bean&gt;</programlisting></para>
  964. <para>The <literal>PasswordEncoder</literal> and
  965. <literal>SaltSource</literal> are optional. A
  966. <literal>PasswordEncoder</literal> provides encoding and decoding of
  967. passwords obtained from the authentication repository. A
  968. <literal>SaltSource</literal> enables the passwords to be populated
  969. with a "salt", which enhances the security of the passwords in the
  970. authentication repository. <literal>PasswordEncoder</literal>
  971. implementations are provided with the Acegi Security System for Spring
  972. covering MD5, SHA and cleartext encodings. Two
  973. <literal>SaltSource</literal> implementations are also provided:
  974. <literal>SystemWideSaltSource</literal> which encodes all passwords
  975. with the same salt, and <literal>ReflectionSaltSource</literal>, which
  976. inspects a given property of the returned
  977. <literal>UserDetails</literal> object to obtain the salt. Please refer
  978. to the JavaDocs for further details on these optional features.</para>
  979. <para>In addition to the properties above, the
  980. <literal>DaoAuthenticationProvider</literal> supports optional caching
  981. of <literal>UserDetails</literal> objects. The
  982. <literal>UserCache</literal> interface enables the
  983. <literal>DaoAuthenticationProvider</literal> to place a
  984. <literal>UserDetails</literal> object into the cache, and retrieve it
  985. from the cache upon subsequent authentication attempts for the same
  986. username. By default the <literal>DaoAuthenticationProvider</literal>
  987. uses the <literal>NullUserCache</literal>, which performs no caching.
  988. A usable caching implementation is also provided,
  989. <literal>EhCacheBasedUserCache</literal>, which is configured as
  990. follows:</para>
  991. <para><programlisting>&lt;bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;
  992. &lt;property name="authenticationDao"&gt;&lt;ref bean="authenticationDao"/&gt;&lt;/property&gt;
  993. &lt;property name="userCache"&gt;&lt;ref bean="userCache"/&gt;&lt;/property&gt;
  994. &lt;/bean&gt;
  995. &lt;bean id="userCache" class="net.sf.acegisecurity.providers.dao.cache.EhCacheBasedUserCache"&gt;
  996. &lt;property name="minutesToIdle"&gt;&lt;value&gt;5&lt;/value&gt;&lt;/property&gt;
  997. &lt;/bean&gt;</programlisting></para>
  998. <para>For a class to be able to provide the
  999. <literal>DaoAuthenticationProvider</literal> with access to an
  1000. authentication repository, it must implement the
  1001. <literal>AuthenticationDao</literal> interface:</para>
  1002. <para><programlisting>public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException;</programlisting></para>
  1003. <para>The <literal>UserDetails</literal> is an interface that provides
  1004. getters that guarantee non-null provision of basic authentication
  1005. information such as the username, password, granted authorities and
  1006. whether the user is enabled or disabled. A concrete implementation,
  1007. <literal>User</literal>, is also provided. Acegi Security users will
  1008. need to decide when writing their <literal>AuthenticationDao</literal>
  1009. what type of <literal>UserDetails</literal> to return. In most cases
  1010. <literal>User</literal> will be used directly or subclassed, although
  1011. special circumstances (such as object relational mappers) may require
  1012. users to write their own <literal>UserDetails</literal> implementation
  1013. from scratch.</para>
  1014. <para>Given <literal>AuthenticationDao</literal> is so simple to
  1015. implement, it should be easy for users to retrieve authentication
  1016. information using a persistence strategy of their choice.</para>
  1017. <para>A design decision was made not to support account locking in the
  1018. <literal>DaoAuthenticationProvider</literal>, as doing so would have
  1019. increased the complexity of the <literal>AuthenticationDao</literal>
  1020. interface. For instance, a method would be required to increase the
  1021. count of unsuccessful authentication attempts. Such functionality
  1022. could be easily provided by leveraging the application event
  1023. publishing features discussed below.</para>
  1024. <para><literal>DaoAuthenticationProvider</literal> returns an
  1025. <literal>Authentication</literal> object which in turn has its
  1026. <literal>principal</literal> property set. The principal will be
  1027. either a <literal>String</literal> (which is essentially the username)
  1028. or a <literal>UserDetails</literal> object (which was looked up from
  1029. the <literal>AuthenticationDao</literal>). By default the
  1030. <literal>UserDetails</literal> is returned, as this enables
  1031. applications to add extra properties potentially of use in
  1032. applications, such as the user's full name, email address etc. If
  1033. using container adapters, or if your applications were written to
  1034. operate with <literal>String</literal>s (as was the case for releases
  1035. prior to Acegi Security 0.6), you should set the
  1036. <literal>DaoAuthenticationProvider.forcePrincipalAsString</literal>
  1037. property to <literal>true</literal> in your application
  1038. context.</para>
  1039. </sect2>
  1040. <sect2 id="security-authentication-provider-events">
  1041. <title>Event Publishing</title>
  1042. <para>The <literal>DaoAuthenticationProvider</literal> automatically
  1043. obtains the <literal>ApplicationContext</literal> it is running in at
  1044. startup time. This allows the provider to publish events through the
  1045. standard Spring event framework. Three types of event messages are
  1046. published:</para>
  1047. <itemizedlist spacing="compact">
  1048. <listitem>
  1049. <para><literal>AuthenticationSuccessEvent</literal> is published
  1050. when an authentication request is successful.</para>
  1051. </listitem>
  1052. <listitem>
  1053. <para><literal>AuthenticationFailureDisabledEvent</literal> is
  1054. published when an authentication request is unsuccessful because
  1055. the returned <literal>UserDetails</literal> is disabled. This is
  1056. normally the case when an account is locked.</para>
  1057. </listitem>
  1058. <listitem>
  1059. <para><literal>AuthenticationFailureUsernameNotFoundEvent</literal>
  1060. is published when an authentication request is unsuccessful
  1061. because the <literal>AuthenticationDao</literal> could not locate
  1062. the <literal>UserDetails</literal>.</para>
  1063. </listitem>
  1064. <listitem>
  1065. <para><literal>AuthenticationFailurePasswordEvent</literal> is
  1066. published when an authentication request is unsuccessful because
  1067. the presented password did not match that in the
  1068. <literal>UserDetails</literal>.</para>
  1069. </listitem>
  1070. </itemizedlist>
  1071. <para>Each event contains two objects: the
  1072. <literal>Authentication</literal> object that represented the
  1073. authentication request, and the <literal>UserDetails</literal> object
  1074. that was found in response to the authentication request (clearly the
  1075. latter will be a dummy object in the case of
  1076. <literal>AuthenticationFailureUsernameNotFoundEvent</literal>). The
  1077. <literal>Authentication</literal> interface provides a
  1078. <literal>getDetails()</literal> method which often includes
  1079. information that event consumers may find useful (eg the TCP/IP
  1080. address that the authentication request originated from).</para>
  1081. <para>As per standard Spring event handling, you can receive these
  1082. events by adding a bean to the application context which implements
  1083. the <literal>ApplicationListener</literal> interface. Included with
  1084. Acegi Security is a <literal>LoggerListener</literal> class which
  1085. receives these events and publishes their details to Commons Logging.
  1086. Refer to the JavaDocs for <literal>LoggerListener</literal> for
  1087. details on the logging priorities used for different message
  1088. types.</para>
  1089. <para>This event publishing system enables you to implement account
  1090. locking and record authentication event history. This might be of
  1091. interest to application users, who can be advised of the times and
  1092. source IP address of all unsuccessful password attempts (and account
  1093. lockouts) since their last successful login. Such capabilities are
  1094. simple to implement and greatly improve the security of your
  1095. application.</para>
  1096. </sect2>
  1097. <sect2 id="security-authentication-provider-in-memory">
  1098. <title>In-Memory Authentication</title>
  1099. <para>Whilst it is easy to use the
  1100. <literal>DaoAuthenticationProvider</literal> and create a custom
  1101. <literal>AuthenticationDao</literal> implementation that extracts
  1102. information from a persistence engine of choice, many applications do
  1103. not require such complexity. One alternative is to configure an
  1104. authentication repository in the application context itself using the
  1105. <literal>InMemoryDaoImpl</literal>:</para>
  1106. <para><programlisting>&lt;bean id="inMemoryDaoImpl" class="net.sf.acegisecurity.providers.dao.memory.InMemoryDaoImpl"&gt;
  1107. &lt;property name="userMap"&gt;
  1108. &lt;value&gt;
  1109. marissa=koala,ROLE_TELLER,ROLE_SUPERVISOR
  1110. dianne=emu,ROLE_TELLER
  1111. scott=wombat,ROLE_TELLER
  1112. peter=opal,disabled,ROLE_TELLER
  1113. &lt;/value&gt;
  1114. &lt;/property&gt;
  1115. &lt;/bean&gt;</programlisting></para>
  1116. <para>The <literal>userMap</literal> property contains each of the
  1117. usernames, passwords, a list of granted authorities and an optional
  1118. enabled/disabled keyword. Commas delimit each token. The username must
  1119. appear to the left of the equals sign, and the password must be the
  1120. first token to the right of the equals sign. The
  1121. <literal>enabled</literal> and <literal>disabled</literal> keywords
  1122. (case insensitive) may appear in the second or any subsequent token.
  1123. Any remaining tokens are treated as granted authorities, which are
  1124. created as <literal>GrantedAuthorityImpl</literal> objects (refer to
  1125. the Authorization section for further discussion on granted
  1126. authorities). Note that if a user has no password and/or no granted
  1127. authorities, the user will not be created in the in-memory
  1128. authentication repository.</para>
  1129. </sect2>
  1130. <sect2 id="security-authentication-provider-jdbc">
  1131. <title>JDBC Authentication</title>
  1132. <para>The Acegi Security System for Spring also includes an
  1133. authentication provider that can obtain authentication information
  1134. from a JDBC data source. The typical configuration for the
  1135. <literal>JdbcDaoImpl</literal> is shown below:</para>
  1136. <para><programlisting>&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  1137. &lt;property name="driverClassName"&gt;&lt;value&gt;org.hsqldb.jdbcDriver&lt;/value&gt;&lt;/property&gt;
  1138. &lt;property name="url"&gt;&lt;value&gt;jdbc:hsqldb:hsql://localhost:9001&lt;/value&gt;&lt;/property&gt;
  1139. &lt;property name="username"&gt;&lt;value&gt;sa&lt;/value&gt;&lt;/property&gt;
  1140. &lt;property name="password"&gt;&lt;value&gt;&lt;/value&gt;&lt;/property&gt;
  1141. &lt;/bean&gt;
  1142. &lt;bean id="jdbcDaoImpl" class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl"&gt;
  1143. &lt;property name="dataSource"&gt;&lt;ref bean="dataSource"/&gt;&lt;/property&gt;
  1144. &lt;/bean&gt;</programlisting></para>
  1145. <para>You can use different relational database management systems by
  1146. modifying the <literal>DriverManagerDataSource</literal> shown above.
  1147. Irrespective of the database used, a standard schema must be used as
  1148. indicated in <literal>dbinit.txt</literal>.</para>
  1149. <para>If you default schema is unsuitable for your needs,
  1150. <literal>JdbcDaoImpl</literal> provides two properties that allow
  1151. customisation of the SQL statements. You may also subclass the
  1152. <literal>JdbcDaoImpl</literal> if further customisation is necessary.
  1153. Please refer to the JavaDocs for details.</para>
  1154. <para>The Acegi Security System for Spring ships with a Hypersonic SQL
  1155. instance that has the required authentication information and sample
  1156. data already populated. To use this server, simply execute the
  1157. <literal>server.bat</literal> or <literal>server.sh</literal> script
  1158. included in the distribution. This will load a new database server
  1159. instance that will service requests made to the URL indicated in the
  1160. bean context configuration shown above.</para>
  1161. </sect2>
  1162. <sect2 id="security-authentication-provider-jaas">
  1163. <title>JAAS Authentication</title>
  1164. <para>Acegi Security provides a package able to delegate
  1165. authentication requests to the Java Authentication and Authorization
  1166. Service (JAAS). This package is discussed in detail below.</para>
  1167. <para>Central to JAAS operation are login configuration files. To
  1168. learn more about JAAS login configuration files, consult the JAAS
  1169. reference documentation available from Sun Microsystems. We expect you
  1170. to have a basic understanding of JAAS and its login configuration file
  1171. syntax in order to understand this section.</para>
  1172. <sect3>
  1173. <title>JaasAuthenticationProvider</title>
  1174. <para>The <literal>JaasAuthenticationProvider</literal> attempts to
  1175. authenticate a user’s principal and credentials through JAAS.</para>
  1176. <para>Let’s assume we have a JAAS login configuration file,
  1177. <literal>/WEB-INF/login.conf</literal>, with the following
  1178. contents:</para>
  1179. <para><programlisting>JAASTest {
  1180. sample.SampleLoginModule required;
  1181. };</programlisting></para>
  1182. <para>Like all Acegi Security beans, the
  1183. <literal>JaasAuthenticationProvider</literal> is configured via the
  1184. application context. The following definitions would correspond to
  1185. the above JAAS login configuration file:</para>
  1186. <para><programlisting>&lt;bean id="jaasAuthenticationProvider" class="net.sf.acegisecurity.providers.jaas.JaasAuthenticationProvider"&gt;
  1187. &lt;property name="loginConfig"&gt;
  1188. &lt;value&gt;/WEB-INF/login.conf&lt;/value&gt;
  1189. &lt;/property&gt;
  1190. &lt;property name="loginContextName"&gt;
  1191. &lt;value&gt;JAASTest&lt;/value&gt;
  1192. &lt;/property&gt;
  1193. &lt;property name="callbackHandlers"&gt;
  1194. &lt;list&gt;
  1195. &lt;bean class="net.sf.acegisecurity.providers.jaas.JaasNameCallbackHandler"/&gt;
  1196. &lt;bean class="net.sf.acegisecurity.providers.jaas.JaasPasswordCallbackHandler"/&gt;
  1197. &lt;/list&gt;
  1198. &lt;/property&gt;
  1199. &lt;property name="authorityGranters"&gt;
  1200. &lt;list&gt;
  1201. &lt;bean class="net.sf.acegisecurity.providers.jaas.TestAuthorityGranter"/&gt;
  1202. &lt;/list&gt;
  1203. &lt;/property&gt;
  1204. &lt;/bean&gt;</programlisting></para>
  1205. <para>The <literal>CallbackHandler</literal>s and
  1206. <literal>AuthorityGranter</literal>s are discussed below.</para>
  1207. </sect3>
  1208. <sect3>
  1209. <title>Callbacks</title>
  1210. <para>Most JAAS <literal>LoginModule</literal>s require a callback
  1211. of some sort. These callbacks are usually used to obtain the
  1212. username and password from the user. In an Acegi Security
  1213. deployment, Acegi Security is responsible for this user interaction
  1214. (typically via a reference to a
  1215. <literal>ContextHolder</literal>-managed
  1216. <literal>Authentication</literal> object). The JAAS package for
  1217. Acegi Security provides two default callback handlers,
  1218. <literal>JaasNameCallbackHandler</literal> and
  1219. <literal>JaasPasswordCallbackHandler</literal>. Each of these
  1220. callback handlers implement
  1221. <literal>JaasAuthenticationCallbackHandler</literal>. In most cases
  1222. these callback handlers can simply be used without understanding the
  1223. internal mechanics. For those needing full control over the callback
  1224. behavior, internally <literal>JaasAutheticationProvider</literal>
  1225. wraps these <literal>JaasAuthenticationCallbackHandler</literal>s
  1226. with an <literal>InternalCallbackHandler</literal>. The
  1227. <literal>InternalCallbackHandler</literal> is the class that
  1228. actually implements JAAS’ normal <literal>CallbackHandler</literal>
  1229. interface. Any time that the JAAS <literal>LoginModule</literal> is
  1230. used, it is passed a list of application context configured
  1231. <literal>InternalCallbackHandler</literal>s. If the
  1232. <literal>LoginModule</literal> requests a callback against the
  1233. <literal>InternalCallbackHandler</literal>s, the callback is in-turn
  1234. passed to the <literal>JaasAuthenticationCallbackHandler</literal>s
  1235. being wrapped.</para>
  1236. </sect3>
  1237. <sect3>
  1238. <title>AuthorityGranters</title>
  1239. <para>JAAS works with principals. Even “roles” are represented as
  1240. principals in JAAS. Acegi Security, on the other hand, works with
  1241. <literal>Authentication</literal> objects. Each
  1242. <literal>Authentication</literal> object contains a single
  1243. principal, and multiple <literal>GrantedAuthority</literal>[]s. To
  1244. facilitate mapping between these different concepts, the Acegi
  1245. Security JAAS package includes an
  1246. <literal>AuthorityGranter</literal> interface. An
  1247. <literal>AuthorityGranter</literal> is responsible for inspecting a
  1248. JAAS principal and returning a <literal>String</literal>. The
  1249. <literal>JaasAuthenticationProvider</literal> then creates a
  1250. <literal>JaasGrantedAuthority</literal> (which implements Acegi
  1251. Security’s <literal>GrantedAuthority</literal> interface) containing
  1252. both the <literal>AuthorityGranter</literal>-returned
  1253. <literal>String</literal> and the JAAS principal that the
  1254. <literal>AuthorityGranter</literal> was passed. The
  1255. <literal>JaasAuthenticationProvider</literal> obtains the JAAS
  1256. principals by firstly successfully authenticating the user’s
  1257. credentials using the JAAS <literal>LoginModule</literal>, and then
  1258. accessing the <literal>LoginContext</literal> it returns. A call to
  1259. <literal>LoginContext.getSubject().getPrincipals()</literal> is
  1260. made, with each resulting principal passed to each
  1261. <literal>AuthorityGranter</literal> defined against the
  1262. <literal>JaasAuthenticationProvider.setAuthorityGranters(List)</literal>
  1263. property. Acegi Security does not include any production
  1264. <literal>AuthorityGranter</literal>s given every JAAS principal has
  1265. an implementation-specific meaning. However, there is a
  1266. <literal>TestAuthorityGranter</literal> in the unit tests that
  1267. demonstrates a simple <literal>AuthorityGranter</literal>
  1268. implementation.</para>
  1269. </sect3>
  1270. </sect2>
  1271. <sect2 id="security-authentication-recommendations">
  1272. <title>Authentication Recommendations</title>
  1273. <para>With the heavy use of interfaces throughout the authentication
  1274. system (<literal>Authentication</literal>,
  1275. <literal>AuthenticationManager</literal>,
  1276. <literal>AuthenticationProvider</literal> and
  1277. <literal>AuthenticationDao</literal>) it might be confusing to a new
  1278. user to know which part of the authentication system to customize. In
  1279. general, the following is recommended:</para>
  1280. <itemizedlist>
  1281. <listitem>
  1282. <para>Use the
  1283. <literal>UsernamePasswordAuthenticationToken</literal>
  1284. implementation where possible.</para>
  1285. </listitem>
  1286. <listitem>
  1287. <para>If you simply need to implement a new authentication
  1288. repository (eg to obtain user details from your application’s
  1289. existing database), use the
  1290. <literal>DaoAuthenticationProvider</literal> along with the
  1291. <literal>AuthenticationDao</literal>. It is the fastest and safest
  1292. way to integrate an external database.</para>
  1293. </listitem>
  1294. <listitem>
  1295. <para>If you're using Container Adapters or a
  1296. <literal>RunAsManager</literal> that replaces the
  1297. <literal>Authentication</literal> object, ensure you have
  1298. registered the <literal>AuthByAdapterProvider</literal> and
  1299. <literal>RunAsManagerImplProvider</literal> respectively with your
  1300. <literal>ProviderManager</literal>.</para>
  1301. </listitem>
  1302. <listitem>
  1303. <para>Never enable the
  1304. <literal>TestingAuthenticationProvider</literal> on a production
  1305. system. Doing so will allow any client to simply present a
  1306. <literal>TestingAuthenticationToken</literal> and obtain whatever
  1307. access they request.</para>
  1308. </listitem>
  1309. <listitem>
  1310. <para>Adding a new <literal>AuthenticationProvider</literal> is
  1311. sufficient to support most custom authentication requirements.
  1312. Only unusual requirements would require the
  1313. <literal>ProviderManager</literal> to be replaced with a different
  1314. <literal>AuthenticationManager</literal>.</para>
  1315. </listitem>
  1316. </itemizedlist>
  1317. </sect2>
  1318. </sect1>
  1319. <sect1 id="security-authorization">
  1320. <title>Authorization</title>
  1321. <sect2 id="security-authorization-granted-authorities">
  1322. <title>Granted Authorities</title>
  1323. <para>As briefly mentioned in the Authentication section, all
  1324. <literal>Authentication</literal> implementations are required to
  1325. store an array of <literal>GrantedAuthority</literal> objects. These
  1326. represent the authorities that have been granted to the principal. The
  1327. <literal>GrantedAuthority</literal> objects are inserted into the
  1328. <literal>Authentication</literal> object by the
  1329. <literal>AuthenticationManager</literal> and are later read by
  1330. <literal>AccessDecisionManager</literal>s when making authorization
  1331. decisions.</para>
  1332. <para><literal>GrantedAuthority</literal> is an interface with only
  1333. one method:</para>
  1334. <para><programlisting>public String getAuthority();</programlisting></para>
  1335. <para>This method allows <literal>AccessDecisionManager</literal>s to
  1336. obtain a precise <literal>String</literal> representation of the
  1337. <literal>GrantedAuthority</literal>. By returning a representation as
  1338. a <literal>String</literal>, a <literal>GrantedAuthority</literal> can
  1339. be easily "read" by most <literal>AccessDecisionManager</literal>s. If
  1340. a <literal>GrantedAuthority</literal> cannot be precisely represented
  1341. as a <literal>String</literal>, the
  1342. <literal>GrantedAuthority</literal> is considered "complex" and
  1343. <literal>getAuthority()</literal> must return
  1344. <literal>null</literal>.</para>
  1345. <para>An example of a "complex" <literal>GrantedAuthority</literal>
  1346. would be an implementation that stores a list of operations and
  1347. authority thresholds that apply to different customer account numbers.
  1348. Representing this complex <literal>GrantedAuthority</literal> as a
  1349. <literal>String</literal> would be quite complex, and as a result the
  1350. <literal>getAuthority()</literal> method should return
  1351. <literal>null</literal>. This will indicate to any
  1352. <literal>AccessDecisionManager</literal> that it will need to
  1353. specifically support the <literal>GrantedAuthority</literal>
  1354. implementation in order to understand its contents.</para>
  1355. <para>The Acegi Security System for Spring includes one concrete
  1356. <literal>GrantedAuthority</literal> implementation,
  1357. <literal>GrantedAuthorityImpl</literal>. This allows any
  1358. user-specified <literal>String</literal> to be converted into a
  1359. <literal>GrantedAuthority</literal>. All
  1360. <literal>AuthenticationProvider</literal>s included with the security
  1361. architecture use <literal>GrantedAuthorityImpl</literal> to populate
  1362. the <literal>Authentication</literal> object.</para>
  1363. </sect2>
  1364. <sect2 id="security-authorization-access-decision-managers">
  1365. <title>Access Decision Managers</title>
  1366. <para>The <literal>AccessDecisionManager</literal> is called by the
  1367. <literal>AbstractSecurityInterceptor</literal> and is responsible for
  1368. making final access control decisions. The
  1369. <literal>AccessDecisionManager</literal> interface contains three
  1370. methods:</para>
  1371. <para><programlisting>public void decide(Authentication authentication, Object object, ConfigAttributeDefinition config) throws AccessDeniedException;
  1372. public boolean supports(ConfigAttribute attribute);
  1373. public boolean supports(Class clazz);</programlisting></para>
  1374. <para>As can be seen from the first method, the
  1375. <literal>AccessDecisionManager</literal> is passed via method
  1376. parameters all information that is likely to be of value in assessing
  1377. an authorization decision. In particular, passing the secure
  1378. <literal>Object</literal> enables those arguments contained in the
  1379. actual secure object invocation to be inspected. For example, let's
  1380. assume the secure object was a <literal>MethodInvocation</literal>. It
  1381. would be easy to query the <literal>MethodInvocation</literal> for any
  1382. <literal>Customer</literal> argument, and then implement some sort of
  1383. security logic in the <literal>AccessDecisionManager</literal> to
  1384. ensure the principal is permitted to operate on that customer.
  1385. Implementations are expected to throw an
  1386. <literal>AccessDeniedException</literal> if access is denied.</para>
  1387. <para>The <literal>supports(ConfigAttribute)</literal> method is
  1388. called by the <literal>AbstractSecurityInterceptor</literal> at
  1389. startup time to determine if the
  1390. <literal>AccessDecisionManager</literal> can process the passed
  1391. <literal>ConfigAttribute</literal>. The
  1392. <literal>supports(Class)</literal> method is called by a security
  1393. interceptor implementation to ensure the configured
  1394. <literal>AccessDecisionManager</literal> supports the type of secure
  1395. object that the security interceptor will present.</para>
  1396. </sect2>
  1397. <sect2 id="security-authorization-voting-decision-manager">
  1398. <title>Voting Decision Manager</title>
  1399. <para>Whilst users can implement their own
  1400. <literal>AccessDecisionManager</literal> to control all aspects of
  1401. authorization, the Acegi Security System for Spring includes several
  1402. <literal>AccessDecisionManager</literal> implementations that are
  1403. based on voting. Using this approach, a series of
  1404. <literal>AccessDecisionVoter</literal> implementations are polled on
  1405. an authorization decision. The
  1406. <literal>AccessDecisionManager</literal> then decides whether or not
  1407. to throw an <literal>AccessDeniedException</literal> based on its
  1408. assessment of the votes.</para>
  1409. <para>The <literal>AccessDecisionVoter</literal> interface has three
  1410. methods:</para>
  1411. <para><programlisting>public int vote(Authentication authentication, Object object, ConfigAttributeDefinition config);
  1412. public boolean supports(ConfigAttribute attribute);
  1413. public boolean supports(Class clazz);</programlisting></para>
  1414. <para>Concrete implementations return an <literal>int</literal>, with
  1415. possible values being reflected in the
  1416. <literal>AccessDecisionVoter</literal> static fields
  1417. <literal>ACCESS_ABSTAIN</literal>, <literal>ACCESS_DENIED</literal>
  1418. and <literal>ACCESS_GRANTED</literal>. A voting implementation will
  1419. return <literal>ACCESS_ABSTAIN</literal> if it has no opinion on an
  1420. authorization decision. If it does have an opinion, it must return
  1421. either <literal>ACCESS_DENIED</literal> or
  1422. <literal>ACCESS_GRANTED</literal>.</para>
  1423. <para>There are three concrete
  1424. <literal>AccessDecisionManager</literal>s provided with the Acegi
  1425. Security System for Spring that tally the votes. The
  1426. <literal>ConsensusBased</literal> implementation will grant or deny
  1427. access based on the consensus of non-abstain votes. Properties are
  1428. provided to control behavior in the event of an equality of votes or
  1429. if all votes are abstain. The <literal>AffirmativeBased</literal>
  1430. implementation will grant access if one or more
  1431. <literal>ACCESS_GRANTED</literal> votes were received (ie a deny vote
  1432. will be ignored, provided there was at least one grant vote). Like the
  1433. <literal>ConsensusBased</literal> implementation, there is a parameter
  1434. that controls the behavior if all voters abstain. The
  1435. <literal>UnanimousBased</literal> provider expects unanimous
  1436. <literal>ACCESS_GRANTED</literal> votes in order to grant access,
  1437. ignoring abstains. It will deny access if there is any
  1438. <literal>ACCESS_DENIED</literal> vote. Like the other implementations,
  1439. there is a parameter that controls the behaviour if all voters
  1440. abstain.</para>
  1441. <para>It is possible to implement a custom
  1442. <literal>AccessDecisionManager</literal> that tallies votes
  1443. differently. For example, votes from a particular
  1444. <literal>AccessDecisionVoter</literal> might receive additional
  1445. weighting, whilst a deny vote from a particular voter may have a veto
  1446. effect.</para>
  1447. <para>There is one concrete <literal>AccessDecisionVoter</literal>
  1448. implementation provided with the Acegi Security System for Spring. The
  1449. <literal>RoleVoter</literal> class will vote if any ConfigAttribute
  1450. begins with <literal>ROLE_</literal>. It will vote to grant access if
  1451. there is a <literal>GrantedAuthority</literal> which returns a
  1452. <literal>String</literal> representation (via the
  1453. <literal>getAuthority()</literal> method) exactly equal to one or more
  1454. <literal>ConfigAttributes</literal> starting with
  1455. <literal>ROLE_</literal>. If there is no exact match of any
  1456. <literal>ConfigAttribute</literal> starting with
  1457. <literal>ROLE_</literal>, the <literal>RoleVoter</literal> will vote
  1458. to deny access. If no <literal>ConfigAttribute</literal> begins with
  1459. <literal>ROLE_</literal>, the voter will abstain.
  1460. <literal>RoleVoter</literal> is case sensitive on comparisons as well
  1461. as the <literal>ROLE_</literal> prefix.</para>
  1462. <para>It is possible to implement a custom
  1463. <literal>AccessDecisionVoter</literal>. Several examples are provided
  1464. in the Acegi Security System for Spring unit tests, including
  1465. <literal>ContactSecurityVoter</literal> and
  1466. <literal>DenyVoter</literal>. The
  1467. <literal>ContactSecurityVoter</literal> abstains from voting decisions
  1468. where a <literal>CONTACT_OWNED_BY_CURRENT_USER</literal>
  1469. <literal>ConfigAttribute</literal> is not found. If voting, it queries
  1470. the <literal>MethodInvocation</literal> to extract the owner of the
  1471. <literal>Contact</literal> object that is subject of the method call.
  1472. It votes to grant access if the <literal>Contact</literal> owner
  1473. matches the principal presented in the
  1474. <literal>Authentication</literal> object. It could have just as easily
  1475. compared the <literal>Contact</literal> owner with some
  1476. <literal>GrantedAuthority</literal> the
  1477. <literal>Authentication</literal> object presented. All of this is
  1478. achieved with relatively few lines of code and demonstrates the
  1479. flexibility of the authorization model.</para>
  1480. </sect2>
  1481. <sect2 id="security-authorization-taglib">
  1482. <title>Authorization Tag Library</title>
  1483. <para>The Acegi Security System for Spring comes bundled with a JSP
  1484. tag library that eases JSP writing. The tag library is known as
  1485. <literal>authz</literal>.</para>
  1486. <para>This library allows you to easy develop JSP pages which
  1487. reference the security environment. For example,
  1488. <literal>authz</literal> allows you to determine if a principal holds
  1489. a particular granted authority, holds a group of granted authorities,
  1490. or does not hold a given granted authority.</para>
  1491. <sect3>
  1492. <title>Usage</title>
  1493. <para>The following JSP fragment illustrates how to use the
  1494. <literal>authz</literal> taglib:</para>
  1495. <para><programlisting>&lt;authz:authorize ifAllGranted="ROLE_SUPERVISOR"&gt;
  1496. &lt;td&gt;
  1497. &lt;A HREF="del.htm?id=&lt;c:out value="${contact.id}"/&gt;"&gt;Del&lt;/A&gt;
  1498. &lt;/td&gt;
  1499. &lt;/authz:authorize&gt;</programlisting></para>
  1500. <para>This code was copied from the Contacts sample
  1501. application.</para>
  1502. <para>What this code says is: if the principal has been granted
  1503. ROLE_SUPERVISOR, allow the tag's body to be output.</para>
  1504. </sect3>
  1505. <sect3>
  1506. <title>Installation</title>
  1507. <para>Installation is a simple matter. Simply copy the
  1508. <literal>acegi-security-taglib.jar</literal> file into your
  1509. application's <literal>WEB-INF/lib</literal> folder. The tag library
  1510. includes it's TLD, which makes it easier to work with JSP 1.2+
  1511. containers.</para>
  1512. <para>If you are using a JSP 1.1 container, you will need to declare
  1513. the JSP tag library in your application's <literal>web.xml</literal>
  1514. file, with code such as this:</para>
  1515. <para><programlisting>&lt;taglib&gt;
  1516. &lt;taglib-uri&gt;http://acegisecurity.sf.net/authz&lt;/taglib-uri&gt;
  1517. &lt;taglib-location&gt;/WEB-INF/authz.tld&lt;/taglib-location&gt;
  1518. &lt;/taglib&gt;</programlisting></para>
  1519. <para>For JSP 1.1 containers you will also need to extract the
  1520. <literal>authz.tld</literal> file from the
  1521. <literal>acegi-security-taglib.jar</literal> file and put it into
  1522. your application's <literal>WEB-INF/lib</literal> folder. Use a
  1523. regular Zip tool, or Java's JAR utility.</para>
  1524. </sect3>
  1525. <sect3>
  1526. <title>Reference</title>
  1527. <para>The <literal>authz:authorize</literal> tag declares the
  1528. following attributes:</para>
  1529. <para><itemizedlist spacing="compact">
  1530. <listitem>
  1531. <para><literal>ifAllGranted</literal>: All the listed roles
  1532. must be granted for the tag to output its body.</para>
  1533. </listitem>
  1534. <listitem>
  1535. <para><literal>ifAnyGranted</literal>: Any of the listed roles
  1536. must be granted for the tag to output its body.</para>
  1537. </listitem>
  1538. <listitem>
  1539. <para><literal>ifNotGranted</literal>: None of the listed
  1540. roles must be granted for the tag to output its body.</para>
  1541. </listitem>
  1542. </itemizedlist></para>
  1543. <para>You'll note that in each attribute you can list multiple
  1544. roles. Simply separate the roles using a comma. The
  1545. <literal>authorize</literal> tag ignores whitespace in
  1546. attributes.</para>
  1547. <para>The tag library logically ANDs all of it's parameters
  1548. together. This means that if you combine two or more attributes, all
  1549. attributes must be true for the tag to output it's body. Don't add
  1550. an <literal>ifAllGranted="ROLE_SUPERVISOR"</literal>, followed by an
  1551. <literal>ifNotGranted="ROLE_SUPERVISOR"</literal>, or you'll be
  1552. surprised to never see the tag's body.</para>
  1553. <para>By requiring all attributes to return true, the authorize tag
  1554. allows you to create more complex authorization scenarios. For
  1555. example, you could declare an
  1556. <literal>ifAllGranted="ROLE_SUPERVISOR"</literal> and an
  1557. <literal>ifNotGranted="ROLE_NEWBIE_SUPERVISOR"</literal> in the same
  1558. tag, in order to prevent new supervisors from seeing the tag body.
  1559. However it would no doubt be simpler to use
  1560. <literal>ifAllGranted="ROLE_EXPERIENCED_SUPERVISOR"</literal> rather
  1561. than inserting NOT conditions into your design.</para>
  1562. <para>One last item: the tag verifies the authorizations in a
  1563. specific order: first <literal>ifNotGranted</literal>, then
  1564. <literal>ifAllGranted</literal>, and finally,
  1565. <literal>ifAnyGranted</literal>.</para>
  1566. </sect3>
  1567. </sect2>
  1568. <sect2 id="security-authorization-recommendations">
  1569. <title>Authorization Recommendations</title>
  1570. <para>Given there are several ways to achieve similar authorization
  1571. outcomes in the Acegi Security System for Spring, the following
  1572. general recommendations are made:</para>
  1573. <itemizedlist>
  1574. <listitem>
  1575. <para>Grant authorities using
  1576. <literal>GrantedAuthorityImpl</literal> where possible. Because it
  1577. is already supported by the Acegi Security System for Spring, you
  1578. avoid the need to create custom
  1579. <literal>AuthenticationManager</literal> or
  1580. <literal>AuthenticationProvider</literal> implementations simply
  1581. to populate the <literal>Authentication</literal> object with a
  1582. custom <literal>GrantedAuthority</literal>.</para>
  1583. </listitem>
  1584. <listitem>
  1585. <para>Writing an <literal>AccessDecisionVoter</literal>
  1586. implementation and using either <literal>ConsensusBased</literal>,
  1587. <literal>AffirmativeBased</literal> or
  1588. <literal>UnanimousBased</literal> as the
  1589. <literal>AccessDecisionManager</literal> may be the best approach
  1590. to implementing your custom access decision rules.</para>
  1591. </listitem>
  1592. </itemizedlist>
  1593. </sect2>
  1594. </sect1>
  1595. <sect1 id="security-run-as">
  1596. <title>Run-As Authentication Replacement</title>
  1597. <sect2 id="security-run-as-purpose">
  1598. <title>Purpose</title>
  1599. <para>The <literal>AbstractSecurityInterceptor</literal> is able to
  1600. temporarily replace the <literal>Authentication</literal> object in
  1601. the <literal>SecureContext</literal> and
  1602. <literal>ContextHolder</literal> during the
  1603. <literal>SecurityInterceptorCallback</literal>. This only occurs if
  1604. the original <literal>Authentication</literal> object was successfully
  1605. processed by the <literal>AuthenticationManager</literal> and
  1606. <literal>AccessDecisionManager</literal>. The
  1607. <literal>RunAsManager</literal> will indicate the replacement
  1608. <literal>Authentication</literal> object, if any, that should be used
  1609. during the <literal>SecurityInterceptorCallback</literal>.</para>
  1610. <para>By temporarily replacing the <literal>Authentication</literal>
  1611. object during a <literal>SecurityInterceptorCallback</literal>, the
  1612. secured invocation will be able to call other objects which require
  1613. different authentication and authorization credentials. It will also
  1614. be able to perform any internal security checks for specific
  1615. <literal>GrantedAuthority</literal> objects.</para>
  1616. </sect2>
  1617. <sect2 id="security-run-as-usage">
  1618. <title>Usage</title>
  1619. <para>A <literal>RunAsManager</literal> interface is provided by the
  1620. Acegi Security System for Spring:</para>
  1621. <para><programlisting>public Authentication buildRunAs(Authentication authentication, Object object, ConfigAttributeDefinition config);
  1622. public boolean supports(ConfigAttribute attribute);
  1623. public boolean supports(Class clazz);</programlisting></para>
  1624. <para>The first method returns the <literal>Authentication</literal>
  1625. object that should replace the existing
  1626. <literal>Authentication</literal> object for the duration of the
  1627. method invocation. If the method returns <literal>null</literal>, it
  1628. indicates no replacement should be made. The second method is used by
  1629. the <literal>AbstractSecurityInterceptor</literal> as part of its
  1630. startup validation of configuration attributes. The
  1631. <literal>supports(Class)</literal> method is called by a security
  1632. interceptor implementation to ensure the configured
  1633. <literal>RunAsManager</literal> supports the type of secure object
  1634. that the security interceptor will present.</para>
  1635. <para>One concrete implementation of a <literal>RunAsManager</literal>
  1636. is provided with the Acegi Security System for Spring. The
  1637. <literal>RunAsManagerImpl</literal> class returns a replacement
  1638. <literal>RunAsUserToken</literal> if any
  1639. <literal>ConfigAttribute</literal> starts with
  1640. <literal>RUN_AS_</literal>. If any such
  1641. <literal>ConfigAttribute</literal> is found, the replacement
  1642. <literal>RunAsUserToken</literal> will contain the same principal,
  1643. credentials and granted authorities as the original
  1644. <literal>Authentication</literal> object, along with a new
  1645. <literal>GrantedAuthorityImpl</literal> for each
  1646. <literal>RUN_AS_</literal> <literal>ConfigAttribute</literal>. Each
  1647. new <literal>GrantedAuthorityImpl</literal> will be prefixed with
  1648. <literal>ROLE_</literal>, followed by the <literal>RUN_AS</literal>
  1649. <literal>ConfigAttribute</literal>. For example, a
  1650. <literal>RUN_AS_SERVER</literal> will result in the replacement
  1651. <literal>RunAsUserToken</literal> containing a
  1652. <literal>ROLE_RUN_AS_SERVER</literal> granted authority.</para>
  1653. <para>The replacement <literal>RunAsUserToken</literal> is just like
  1654. any other <literal>Authentication</literal> object. It needs to be
  1655. authenticated by the <literal>AuthenticationManager</literal>,
  1656. probably via delegation to a suitable
  1657. <literal>AuthenticationProvider</literal>. The
  1658. <literal>RunAsImplAuthenticationProvider</literal> performs such
  1659. authentication. It simply accepts as valid any
  1660. <literal>RunAsUserToken</literal> presented.</para>
  1661. <para>To ensure malicious code does not create a
  1662. <literal>RunAsUserToken</literal> and present it for guaranteed
  1663. acceptance by the <literal>RunAsImplAuthenticationProvider</literal>,
  1664. the hash of a key is stored in all generated tokens. The
  1665. <literal>RunAsManagerImpl</literal> and
  1666. <literal>RunAsImplAuthenticationProvider</literal> is created in the
  1667. bean context with the same key:</para>
  1668. <para><programlisting>&lt;bean id="runAsManager" class="net.sf.acegisecurity.runas.RunAsManagerImpl"&gt;
  1669. &lt;property name="key"&gt;&lt;value&gt;my_run_as_password&lt;/value&gt;&lt;/property&gt;
  1670. &lt;/bean&gt;</programlisting><programlisting>&lt;bean id="runAsAuthenticationProvider" class="net.sf.acegisecurity.runas.RunAsImplAuthenticationProvider"&gt;
  1671. &lt;property name="key"&gt;&lt;value&gt;my_run_as_password&lt;/value&gt;&lt;/property&gt;
  1672. &lt;/bean&gt;</programlisting></para>
  1673. <para>By using the same key, each <literal>RunAsUserToken</literal>
  1674. can be validated it was created by an approved
  1675. <literal>RunAsManagerImpl</literal>. The
  1676. <literal>RunAsUserToken</literal> is immutable after creation for
  1677. security reasons.</para>
  1678. </sect2>
  1679. </sect1>
  1680. <sect1 id="security-ui">
  1681. <title>User Interfacing with the ContextHolder</title>
  1682. <sect2 id="security-ui-purpose">
  1683. <title>Purpose</title>
  1684. <para>Everything presented so far assumes one thing: the
  1685. <literal>ContextHolder</literal> is populated with a valid
  1686. <literal>SecureContext</literal>, which in turn contains a valid
  1687. <literal>Authentication</literal> object. Developers are free to do
  1688. this in whichever way they like, such as directly calling the relevant
  1689. objects at runtime. However, several classes have been provided to
  1690. make this process transparent in many situations.</para>
  1691. <para>The <literal>net.sf.acegisecurity.ui</literal> package is
  1692. designed to make interfacing web application user interfaces with the
  1693. <literal>ContextHolder</literal> as simple as possible. There are two
  1694. major steps in doing this:</para>
  1695. <para><itemizedlist spacing="compact">
  1696. <listitem>
  1697. <para>Actually authenticate the user and place the resulting
  1698. <literal>Authentication</literal> object in a "well-known
  1699. location".</para>
  1700. </listitem>
  1701. <listitem>
  1702. <para>Extract the <literal>Authentication</literal> object from
  1703. the "well-known location" and place in into the
  1704. <literal>ContextHolder</literal> for the duration of the secure
  1705. object invocation.</para>
  1706. </listitem>
  1707. </itemizedlist></para>
  1708. <para>There are several alternatives are available for the first step,
  1709. which will be briefly discussed in this chapter. The most popular
  1710. approach is HTTP Session Authentication, which uses the
  1711. <literal>HttpSession</literal> object and filters to authenticate the
  1712. user. Another approach is HTTP Basic Authentication, which allows
  1713. clients to use HTTP headers to present authentication information to
  1714. the Acegi Security System for Spring. Alternatively, you can also use
  1715. Yale Central Authentication Service (CAS) for enterprise-wide single
  1716. sign on. The final approach is via Container Adapters, which allow
  1717. supported web containers to perform the authentication themselves.
  1718. HTTP Session and Basic Authentication is discussed below, whilst CAS
  1719. and Container Adapters are discussed in separate sections of this
  1720. document.</para>
  1721. </sect2>
  1722. <sect2 id="security-ui-http-session">
  1723. <title>HTTP Session Authentication</title>
  1724. <para>HTTP Session Authentication involves using the
  1725. <literal>AuthenticationProcessingFilter</literal> to process a login
  1726. form. The login form simply contains <literal>j_username</literal> and
  1727. <literal>j_password</literal> input fields, and posts to a URL that is
  1728. monitored by the filter (by default
  1729. <literal>j_acegi_security_check</literal>). The filter is defined in
  1730. <literal>web.xml</literal> behind a
  1731. <literal>FilterToBeanProxy</literal> as follows:</para>
  1732. <para><programlisting>&lt;filter&gt;
  1733. &lt;filter-name&gt;Acegi Authentication Processing Filter&lt;/filter-name&gt;
  1734. &lt;filter-class&gt;net.sf.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  1735. &lt;init-param&gt;
  1736. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  1737. &lt;param-value&gt;net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter&lt;/param-value&gt;
  1738. &lt;/init-param&gt;
  1739. &lt;/filter&gt;
  1740. &lt;filter-mapping&gt;
  1741. &lt;filter-name&gt;Acegi Authentication Processing Filter&lt;/filter-name&gt;
  1742. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  1743. &lt;/filter-mapping&gt;</programlisting></para>
  1744. <para>For a discussion of <literal>FilterToBeanProxy</literal>, please
  1745. refer to the Filters section. The application context will need to
  1746. define the <literal>AuthenticationProcessingFilter</literal>:</para>
  1747. <para><programlisting>&lt;bean id="authenticationProcessingFilter" class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter"&gt;
  1748. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  1749. &lt;property name="authenticationFailureUrl"&gt;&lt;value&gt;/acegilogin.jsp?login_error=1&lt;/value&gt;&lt;/property&gt;
  1750. &lt;property name="defaultTargetUrl"&gt;&lt;value&gt;/&lt;/value&gt;&lt;/property&gt;
  1751. &lt;property name="filterProcessesUrl"&gt;&lt;value&gt;/j_acegi_security_check&lt;/value&gt;&lt;/property&gt;
  1752. &lt;/bean&gt;</programlisting></para>
  1753. <para>The configured <literal>AuthenticationManager</literal>
  1754. processes each authentication request. If authentication fails, the
  1755. browser will be redirected to the
  1756. <literal>authenticationFailureUrl</literal>. The
  1757. <literal>AuthenticationException</literal> will be placed into the
  1758. <literal>HttpSession</literal> attribute indicated by
  1759. <literal>AbstractProcessingFilter.ACEGI_SECURITY_LAST_EXCEPTION_KEY</literal>,
  1760. enabling a reason to be provided to the user on the error page.</para>
  1761. <para>If authentication is successful, the resulting
  1762. <literal>Authentication</literal> object will be placed into the
  1763. <literal>HttpSession</literal> attribute indicated by
  1764. <literal>HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY</literal>.
  1765. This becomes the "well-known location" from which the
  1766. <literal>Authentication</literal> object is later extracted.</para>
  1767. <para>Once the <literal>HttpSession</literal> has been updated, the
  1768. browser will need to be redirected to the target URL. The target URL
  1769. is usually indicated by the <literal>HttpSession</literal> attribute
  1770. specified by
  1771. <literal>AbstractProcessingFilter.ACEGI_SECURITY_TARGET_URL_KEY</literal>.
  1772. This attribute is automatically set by the
  1773. <literal>SecurityEnforcementFilter</literal> when an
  1774. <literal>AuthenticationException</literal> occurs, so that after login
  1775. is completed the user can return to what they were trying to access.
  1776. If for some reason the <literal>HttpSession</literal> does not
  1777. indicate the target URL, the browser will be redirected to the
  1778. <literal>defaultTargetUrl</literal> property.</para>
  1779. <para>Because this authentication approach is fully contained within a
  1780. single web application, HTTP Session Authentication is recommended to
  1781. be used instead of Container Adapters.</para>
  1782. </sect2>
  1783. <sect2 id="security-ui-http-basic">
  1784. <title>HTTP Basic Authentication</title>
  1785. <para>The Acegi Security System for Spring provides a
  1786. <literal>BasicProcessingFilter</literal> which is capable of
  1787. processing authentication credentials presented in HTTP headers. This
  1788. can be used for authenticating calls made by Spring remoting protocols
  1789. (such as Hessian and Burlap), as well as normal user agents (such as
  1790. Internet Explorer and Navigator). The standard governing HTTP Basic
  1791. Authentication is defined by RFC 1945, Section 11, and the
  1792. <literal>BasicProcessingFilter</literal> conforms with this
  1793. RFC.</para>
  1794. <para>To implement HTTP Basic Authentication, it is necessary to add
  1795. the following filter to <literal>web.xml</literal>:</para>
  1796. <para><programlisting>&lt;filter&gt;
  1797. &lt;filter-name&gt;Acegi HTTP BASIC Authorization Filter&lt;/filter-name&gt;
  1798. &lt;filter-class&gt;net.sf.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  1799. &lt;init-param&gt;
  1800. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  1801. &lt;param-value&gt;net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter&lt;/param-value&gt;
  1802. &lt;/init-param&gt;
  1803. &lt;/filter&gt;
  1804. &lt;filter-mapping&gt;
  1805. &lt;filter-name&gt;Acegi HTTP BASIC Authorization Filter&lt;/filter-name&gt;
  1806. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  1807. &lt;/filter-mapping&gt;</programlisting></para>
  1808. <para>For a discussion of <literal>FilterToBeanProxy</literal>, please
  1809. refer to the Filters section. The application context will need to
  1810. define the <literal>BasicProcessingFilter</literal> and its required
  1811. collaborator:</para>
  1812. <para><programlisting>&lt;bean id="basicProcessingFilter" class="net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter"&gt;
  1813. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  1814. &lt;property name="authenticationEntryPoint"&gt;&lt;ref bean="authenticationEntryPoint"/&gt;&lt;/property&gt;
  1815. &lt;/bean&gt;
  1816. &lt;bean id="authenticationEntryPoint" class="net.sf.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint"&gt;
  1817. &lt;property name="realmName"&gt;&lt;value&gt;Name Of Your Realm&lt;/value&gt;&lt;/property&gt;
  1818. &lt;/bean&gt;</programlisting></para>
  1819. <para>The configured <literal>AuthenticationManager</literal>
  1820. processes each authentication request. If authentication fails, the
  1821. configured <literal>AuthenticationEntryPoint</literal> will be used to
  1822. retry the authentication process. Usually you will use the
  1823. <literal>BasicProcessingFilterEntryPoint</literal>, which returns a
  1824. 401 response with a suitable header to retry HTTP Basic
  1825. authentication. If authentication is successful, the resulting
  1826. <literal>Authentication</literal> object will be placed into the
  1827. <literal>HttpSession</literal> attribute indicated by
  1828. <literal>HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY</literal>.
  1829. This becomes the "well-known location" from which the
  1830. <literal>Authentication</literal> object is later extracted.</para>
  1831. <para>If the authentication event was successful, or authentication
  1832. was not attempted because the HTTP header did not contain a supported
  1833. authentication request, the filter chain will continue as normal. The
  1834. only time the filter chain will be interrupted is if authentication
  1835. fails and the <literal>AuthenticationEntryPoint</literal> is called,
  1836. as discussed in the previous paragraph.</para>
  1837. <para>HTTP Basic Authentication is recommended to be used instead of
  1838. Container Adapters. It can be used in conjunction with HTTP Session
  1839. Authentication, as demonstrated in the Contacts sample application.
  1840. You can also use it instead of HTTP Session Authentication if you
  1841. wish.</para>
  1842. </sect2>
  1843. <sect2 id="security-ui-well-known">
  1844. <title>Well-Known Location Integration</title>
  1845. <para>Once a web application has used either HTTP Session
  1846. Authentication, HTTP Basic Authentication, or a Container Adapter, an
  1847. <literal>Authentication</literal> object will exist in a well-known
  1848. location. The final step in automatically integrating the user
  1849. interface with the backend security interceptor is to extract this
  1850. <literal>Authentication</literal> object from the well-known location
  1851. and place it into a <literal>SecureContext</literal> in the
  1852. <literal>ContextHolder</literal>.</para>
  1853. <para>The <literal>AbstractIntegrationFilter</literal> and its
  1854. subclasses provide this well-known location integration. These classes
  1855. are standard filters, and at the start of each request they will
  1856. attempt to extract the <literal>Authentication</literal> object from a
  1857. well-known location. The <literal>Authentication</literal> object will
  1858. then be added to a <literal>SecureContext</literal>, the
  1859. <literal>SecureContext</literal> associated with the
  1860. <literal>ContextHolder</literal> for the duration of the request, and
  1861. the <literal>ContextHolder</literal> be cleared when the request is
  1862. finished. Four concrete subclasses of
  1863. <literal>AbstractIntegrationFilter</literal> are provided with the
  1864. Acegi Security System for Spring:</para>
  1865. <para><itemizedlist>
  1866. <listitem>
  1867. <para><literal>HttpSessionIntegrationFilter</literal> is used
  1868. with HTTP Session Authentication, HTTP Basic Authentication, or
  1869. any other approach that populates the
  1870. <literal>HttpSession</literal> accordingly. It extracts the
  1871. <literal>Authentication</literal> object from the
  1872. <literal>HttpSession</literal> attribute indicated by
  1873. <literal>HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY</literal>.</para>
  1874. </listitem>
  1875. <listitem>
  1876. <para><literal>HttpRequestIntegrationFilter</literal> is used
  1877. with Catalina, Jetty and Resin Container Adapters. It extracts
  1878. the authentication information from
  1879. <literal>HttpServletRequest.getUserPrincipal()</literal>.</para>
  1880. </listitem>
  1881. <listitem>
  1882. <para><literal>JbossIntegrationFilter</literal> is used with the
  1883. JBoss Container Adapter. It extracts the authentication from
  1884. <literal>java:comp/env/security/subject</literal>.</para>
  1885. </listitem>
  1886. <listitem>
  1887. <para><literal>AutoIntegrationFilter</literal> automatically
  1888. determines which filter to use. This makes a web application WAR
  1889. file more portable, as the <literal>web.xml</literal> is not
  1890. hard-coded to a specific
  1891. <literal>AbstractIntegrationFilter</literal>.</para>
  1892. </listitem>
  1893. </itemizedlist></para>
  1894. <para>To define the <literal>AutoIntegrationFilter</literal>
  1895. (recommended), simply add the following to your web.xml:</para>
  1896. <para><programlisting>&lt;filter&gt;
  1897. &lt;filter-name&gt;Acegi Security System for Spring Auto Integration Filter&lt;/filter-name&gt;
  1898. &lt;filter-class&gt;net.sf.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  1899. &lt;init-param&gt;
  1900. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  1901. &lt;param-value&gt;net.sf.acegisecurity.ui.AutoIntegrationFilter&lt;/param-value&gt;
  1902. &lt;/init-param&gt;
  1903. &lt;/filter&gt;
  1904. &lt;filter-mapping&gt;
  1905. &lt;filter-name&gt;Acegi Security System for Spring Auto Integration Filter&lt;/filter-name&gt;
  1906. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  1907. &lt;/filter-mapping&gt;</programlisting></para>
  1908. <para>You will also need to add the following line to your application
  1909. context:</para>
  1910. <para><programlisting>&lt;bean id="autoIntegrationFilter" class="net.sf.acegisecurity.ui.AutoIntegrationFilter" /&gt;</programlisting></para>
  1911. <para>Once in the <literal>ContextHolder</literal>, the standard Acegi
  1912. Security System for Spring classes can be used. Because
  1913. <literal>ContextHolder</literal> is a standard object which is
  1914. populated using a filter at the container level, JSPs and Servlets do
  1915. not need to use Spring's MVC packages. This enables those applications
  1916. that use other MVC frameworks to still leverage Spring's other
  1917. capabilities, with full authentication and authorization support. The
  1918. <literal>debug.jsp</literal> page provided with the sample application
  1919. demonstrates accessing the <literal>ContextHolder</literal>
  1920. independent of Spring's MVC packages.</para>
  1921. </sect2>
  1922. </sect1>
  1923. <sect1 id="security-container-adapters">
  1924. <title>Container Adapters</title>
  1925. <sect2 id="security-container-adapters-overview">
  1926. <title>Overview</title>
  1927. <para>Early versions of the Acegi Security System for Spring
  1928. exclusively used Container Adapters for interfacing authentication
  1929. with end users. Whilst this worked well, it required considerable time
  1930. to support multiple container versions and the configuration itself
  1931. was relatively time-consuming for developers. For this reason the HTTP
  1932. Session Authentication and HTTP Basic Authentication approaches were
  1933. developed, and are today recommended for most applications.</para>
  1934. <para>Container Adapters enable the Acegi Security System for Spring
  1935. to integrate directly with the containers used to host end user
  1936. applications. This integration means that applications can continue to
  1937. leverage the authentication and authorization capabilities built into
  1938. containers (such as <literal>isUserInRole()</literal> and form-based
  1939. or basic authentication), whilst benefiting from the enhanced security
  1940. interception capabilities provided by the Acegi Security System for
  1941. Spring.</para>
  1942. <para>The integration between a container and the Acegi Security
  1943. System for Spring is achieved through an adapter. The adapter provides
  1944. a container-compatible user authentication provider, and needs to
  1945. return a container-compatible user object.</para>
  1946. <para>The adapter is instantiated by the container and is defined in a
  1947. container-specific configuration file. The adapter then loads a Spring
  1948. application context which defines the normal authentication manager
  1949. settings, such as the authentication providers that can be used to
  1950. authenticate the request. The application context is usually named
  1951. <literal>acegisecurity.xml</literal> and is placed in a
  1952. container-specific location.</para>
  1953. <para>The Acegi Security System for Spring currently supports Jetty,
  1954. Catalina (Tomcat), JBoss and Resin. Additional container adapters can
  1955. easily be written.</para>
  1956. </sect2>
  1957. <sect2 id="security-container-adapters-adapter-provider">
  1958. <title>Adapter Authentication Provider</title>
  1959. <para>As is always the case, the container adapter generated
  1960. <literal>Authentication</literal> object still needs to be
  1961. authenticated by an <literal>AuthenticationManager</literal> when
  1962. requested to do so by the
  1963. <literal>AbstractSecurityInterceptor</literal>. The
  1964. <literal>AuthenticationManager</literal> needs to be certain the
  1965. adapter-provided <literal>Authentication</literal> object is valid and
  1966. was actually authenticated by a trusted adapter.</para>
  1967. <para>Adapters create <literal>Authentication</literal> objects which
  1968. are immutable and implement the <literal>AuthByAdapter</literal>
  1969. interface. These objects store the hash of a key that is defined by
  1970. the adapter. This allows the <literal>Authentication</literal> object
  1971. to be validated by the <literal>AuthByAdapterProvider</literal>. This
  1972. authentication provider is defined as follows:</para>
  1973. <para><programlisting>&lt;bean id="authByAdapterProvider" class="net.sf.acegisecurity.adapters.AuthByAdapterProvider"&gt;
  1974. &lt;property name="key"&gt;&lt;value&gt;my_password&lt;/value&gt;&lt;/property&gt;
  1975. &lt;/bean&gt;</programlisting></para>
  1976. <para>The key must match the key that is defined in the
  1977. container-specific configuration file that starts the adapter. The
  1978. <literal>AuthByAdapterProvider</literal> automatically accepts as
  1979. valid any <literal>AuthByAdapter</literal> implementation that returns
  1980. the expected hash of the key.</para>
  1981. <para>To reiterate, this means the adapter will perform the initial
  1982. authentication using providers such as
  1983. <literal>DaoAuthenticationProvider</literal>, returning an
  1984. <literal>AuthByAdapter</literal> instance that contains a hash code of
  1985. the key. Later, when an application calls a security interceptor
  1986. managed resource, the <literal>AuthByAdapter</literal> instance in the
  1987. <literal>SecureContext</literal> in the
  1988. <literal>ContextHolder</literal> will be tested by the application's
  1989. <literal>AuthByAdapterProvider</literal>. There is no requirement for
  1990. additional authentication providers such as
  1991. <literal>DaoAuthenticationProvider</literal> within the
  1992. application-specific application context, as the only type of
  1993. <literal>Authentication</literal> instance that will be presented by
  1994. the application is from the container adapter.</para>
  1995. <para>Classloader issues are frequent with containers and the use of
  1996. container adapters illustrates this further. Each container requires a
  1997. very specific configuration. The installation instructions are
  1998. provided below. Once installed, please take the time to try the sample
  1999. application to ensure your container adapter is properly
  2000. configured.</para>
  2001. <para>When using container adapters with the
  2002. <literal>DaoAuthenticationProvider</literal>, ensure you set its
  2003. <literal>forcePrincipalAsString</literal> property to
  2004. <literal>true</literal>.</para>
  2005. </sect2>
  2006. <sect2 id="security-container-adapters-catalina">
  2007. <title>Catalina (Tomcat) Installation</title>
  2008. <para>The following was tested with Jakarta Tomcat 4.1.30 and 5.0.19.
  2009. We automatically test the following directions using our container
  2010. integration test system and these versions of Catalina
  2011. (Tomcat).</para>
  2012. <para><literal>$CATALINA_HOME</literal> refers to the root of your
  2013. Catalina (Tomcat) installation.</para>
  2014. <para>Edit your <literal>$CATALINA_HOME/conf/server.xml</literal> file
  2015. so the <literal>&lt;Engine&gt;</literal> section contains only one
  2016. active <literal>&lt;Realm&gt;</literal> entry. An example realm
  2017. entry:</para>
  2018. <para><programlisting> &lt;Realm className="net.sf.acegisecurity.adapters.catalina.CatalinaAcegiUserRealm"
  2019. appContextLocation="conf/acegisecurity.xml"
  2020. key="my_password" /&gt;</programlisting></para>
  2021. <para>Be sure to remove any other <literal>&lt;Realm&gt;</literal>
  2022. entry from your <literal>&lt;Engine&gt;</literal> section.</para>
  2023. <para>Copy <literal>acegisecurity.xml</literal> into
  2024. <literal>$CATALINA_HOME/conf</literal>.</para>
  2025. <para>Copy <literal>acegi-security-catalina-server.jar</literal> into
  2026. <literal>$CATALINA_HOME/server/lib</literal>.</para>
  2027. <para>Copy the following files into
  2028. <literal>$CATALINA_HOME/common/lib</literal>:</para>
  2029. <itemizedlist>
  2030. <listitem>
  2031. <para><literal>aopalliance.jar</literal></para>
  2032. </listitem>
  2033. <listitem>
  2034. <para><literal>spring.jar</literal></para>
  2035. </listitem>
  2036. <listitem>
  2037. <para><literal>acegi-security-catalina-common.jar</literal></para>
  2038. </listitem>
  2039. <listitem>
  2040. <para><literal>commons-codec.jar</literal></para>
  2041. </listitem>
  2042. <listitem>
  2043. <para><literal>burlap.jar</literal></para>
  2044. </listitem>
  2045. <listitem>
  2046. <para><literal>hessian.jar</literal></para>
  2047. </listitem>
  2048. </itemizedlist>
  2049. <para>None of the above JAR files (or
  2050. <literal>acegi-security.jar</literal>) should be in your application's
  2051. <literal>WEB-INF/lib</literal>. The realm name indicated in your
  2052. <literal>web.xml</literal> does not matter with Catalina.</para>
  2053. <para>We have received reports of problems using this Container
  2054. Adapter with Mac OS X. A work-around is to use a script such as
  2055. follows:</para>
  2056. <para><programlisting>#!/bin/sh
  2057. export CATALINA_HOME="/Library/Tomcat"
  2058. export JAVA_HOME="/Library/Java/Home"
  2059. cd /
  2060. $CATALINA_HOME/bin/startup.sh</programlisting></para>
  2061. </sect2>
  2062. <sect2 id="security-container-adapters-jetty">
  2063. <title>Jetty Installation</title>
  2064. <para>The following was tested with Jetty 4.2.18. We automatically
  2065. test the following directions using our container integration test
  2066. system and this version of Jetty.</para>
  2067. <para><literal>$JETTY_HOME</literal> refers to the root of your Jetty
  2068. installation.</para>
  2069. <para>Edit your <literal>$JETTY_HOME/etc/jetty.xml</literal> file so
  2070. the <literal>&lt;Configure class&gt;</literal> section has a new
  2071. addRealm call:</para>
  2072. <para><programlisting> &lt;Call name="addRealm"&gt;
  2073. &lt;Arg&gt;
  2074. &lt;New class="net.sf.acegisecurity.adapters.jetty.JettyAcegiUserRealm"&gt;
  2075. &lt;Arg&gt;Spring Powered Realm&lt;/Arg&gt;
  2076. &lt;Arg&gt;my_password&lt;/Arg&gt;
  2077. &lt;Arg&gt;etc/acegisecurity.xml&lt;/Arg&gt;
  2078. &lt;/New&gt;
  2079. &lt;/Arg&gt;
  2080. &lt;/Call&gt;</programlisting></para>
  2081. <para>Copy <literal>acegisecurity.xml</literal> into
  2082. <literal>$JETTY_HOME/etc</literal>.</para>
  2083. <para>Copy the following files into
  2084. <literal>$JETTY_HOME/ext</literal>:<itemizedlist>
  2085. <listitem>
  2086. <para><literal>aopalliance.jar</literal></para>
  2087. </listitem>
  2088. <listitem>
  2089. <para><literal>commons-logging.jar</literal></para>
  2090. </listitem>
  2091. <listitem>
  2092. <para><literal>spring.jar</literal></para>
  2093. </listitem>
  2094. <listitem>
  2095. <para><literal>acegi-security-jetty-ext.jar</literal></para>
  2096. </listitem>
  2097. <listitem>
  2098. <para><literal>commons-codec.jar</literal></para>
  2099. </listitem>
  2100. <listitem>
  2101. <para><literal>burlap.jar</literal></para>
  2102. </listitem>
  2103. <listitem>
  2104. <para><literal>hessian.jar</literal></para>
  2105. </listitem>
  2106. </itemizedlist></para>
  2107. <para>None of the above JAR files (or
  2108. <literal>acegi-security.jar</literal>) should be in your application's
  2109. <literal>WEB-INF/lib</literal>. The realm name indicated in your
  2110. <literal>web.xml</literal> does matter with Jetty. The
  2111. <literal>web.xml</literal> must express the same
  2112. <literal>&lt;realm-name&gt;</literal> as your
  2113. <literal>jetty.xml</literal> (in the example above, "Spring Powered
  2114. Realm").</para>
  2115. </sect2>
  2116. <sect2 id="security-container-adapters-joss">
  2117. <title>JBoss Installation</title>
  2118. <para>The following was tested with JBoss 3.2.6. We automatically test
  2119. the following directions using our container integration test system
  2120. and this version of JBoss.</para>
  2121. <para><literal>$JBOSS_HOME</literal> refers to the root of your JBoss
  2122. installation.</para>
  2123. <para>There are two different ways of making spring context available
  2124. to the Jboss integration classes. </para>
  2125. <para>The first approach is by editing your
  2126. <literal>$JBOSS_HOME/server/your_config/conf/login-config.xml</literal>
  2127. file so that it contains a new entry under the
  2128. <literal>&lt;Policy&gt;</literal> section:</para>
  2129. <para><programlisting> &lt;application-policy name = "SpringPoweredRealm"&gt;
  2130. &lt;authentication&gt;
  2131. &lt;login-module code = "net.sf.acegisecurity.adapters.jboss.JbossSpringLoginModule"
  2132. flag = "required"&gt;
  2133. &lt;module-option name = "appContextLocation"&gt;acegisecurity.xml&lt;/module-option&gt;
  2134. &lt;module-option name = "key"&gt;my_password&lt;/module-option&gt;
  2135. &lt;/login-module&gt;
  2136. &lt;/authentication&gt;
  2137. &lt;/application-policy&gt;</programlisting></para>
  2138. <para>Copy <literal>acegisecurity.xml</literal> into
  2139. <literal>$JBOSS_HOME/server/your_config/conf</literal>.</para>
  2140. <para>In this configuration <literal>acegisecurity.xml</literal>
  2141. contains the spring context definition including all the
  2142. authentication manager beans. You have to bear in mind though, that
  2143. <literal>SecurityContext</literal> is created and destroyed on each
  2144. login request, so the login operation might become costly.
  2145. Alternatively, the second approach is to use Spring singleton
  2146. capabilities through
  2147. <literal>org.springframework.beans.factory.access.SingletonBeanFactoryLocator</literal>.
  2148. The required configuration for this approach is:</para>
  2149. <para><programlisting> &lt;application-policy name = "SpringPoweredRealm"&gt;
  2150. &lt;authentication&gt;
  2151. &lt;login-module code = "net.sf.acegisecurity.adapters.jboss.JbossSpringLoginModule"
  2152. flag = "required"&gt;
  2153. &lt;module-option name = "singletonId"&gt;springRealm&lt;/module-option&gt;
  2154. &lt;module-option name = "key"&gt;my_password&lt;/module-option&gt;
  2155. &lt;module-option name = "authenticationManager"&gt;authenticationManager&lt;/module-option&gt;
  2156. &lt;/login-module&gt;
  2157. &lt;/authentication&gt;
  2158. &lt;/application-policy&gt;</programlisting></para>
  2159. <para>In the above code fragment,
  2160. <literal>authenticationManager</literal> is a helper property that
  2161. defines the expected name of the
  2162. <literal>AuthenticationManager</literal> in case you have several
  2163. defined in the IoC container. The <literal>singletonId</literal>
  2164. property references a bean defined in a
  2165. <literal>beanRefFactory.xml</literal> file. This file needs to be
  2166. available from anywhere on the JBoss classpath, including
  2167. <literal>$JBOSS_HOME/server/your_config/conf</literal>. The
  2168. <literal>beanRefFactory.xml</literal> contains the following
  2169. declaration:</para>
  2170. <para><programlisting>&lt;beans&gt;
  2171. &lt;bean id="springRealm" singleton="true" lazy-init="true" class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt;
  2172. &lt;constructor-arg&gt;
  2173. &lt;list&gt;
  2174. &lt;value&gt;acegisecurity.xml&lt;/value&gt;
  2175. &lt;/list&gt;
  2176. &lt;/constructor-arg&gt;
  2177. &lt;/bean&gt;
  2178. &lt;/beans&gt;</programlisting></para>
  2179. <para>Finally, irrespective of the configuration approach you need to
  2180. copy the following files into
  2181. <literal>$JBOSS_HOME/server/your_config/lib</literal>:<itemizedlist>
  2182. <listitem>
  2183. <para><literal>aopalliance.jar</literal></para>
  2184. </listitem>
  2185. <listitem>
  2186. <para><literal>spring.jar</literal></para>
  2187. </listitem>
  2188. <listitem>
  2189. <para><literal>acegi-security-jboss-lib.jar</literal></para>
  2190. </listitem>
  2191. <listitem>
  2192. <para><literal>commons-codec.jar</literal></para>
  2193. </listitem>
  2194. <listitem>
  2195. <para><literal>burlap.jar</literal></para>
  2196. </listitem>
  2197. <listitem>
  2198. <para><literal>hessian.jar</literal></para>
  2199. </listitem>
  2200. </itemizedlist></para>
  2201. <para>None of the above JAR files (or
  2202. <literal>acegi-security.jar</literal>) should be in your application's
  2203. <literal>WEB-INF/lib</literal>. The realm name indicated in your
  2204. <literal>web.xml</literal> does not matter with JBoss. However, your
  2205. web application's <literal>WEB-INF/jboss-web.xml</literal> must
  2206. express the same <literal>&lt;security-domain&gt;</literal> as your
  2207. <literal>login-config.xml</literal>. For example, to match the above
  2208. example, your <literal>jboss-web.xml</literal> would look like
  2209. this:</para>
  2210. <para><programlisting>&lt;jboss-web&gt;
  2211. &lt;security-domain&gt;java:/jaas/SpringPoweredRealm&lt;/security-domain&gt;
  2212. &lt;/jboss-web&gt;</programlisting></para>
  2213. </sect2>
  2214. <sect2 id="security-container-adapters-resin">
  2215. <title>Resin Installation</title>
  2216. <para>The following was tested with Resin 3.0.6.</para>
  2217. <para><literal>$RESIN_HOME</literal> refers to the root of your Resin
  2218. installation.</para>
  2219. <para>Resin provides several ways to support the container adapter. In
  2220. the instructions below we have elected to maximise consistency with
  2221. other container adapter configurations. This will allow Resin users to
  2222. simply deploy the sample application and confirm correct
  2223. configuration. Developers comfortable with Resin are naturally able to
  2224. use its capabilities to package the JARs with the web application
  2225. itself, and/or support single sign-on.</para>
  2226. <para>Copy the following files into
  2227. <literal>$RESIN_HOME/lib</literal>:<itemizedlist>
  2228. <listitem>
  2229. <para><literal>aopalliance.jar</literal></para>
  2230. </listitem>
  2231. <listitem>
  2232. <para><literal>commons-logging.jar</literal></para>
  2233. </listitem>
  2234. <listitem>
  2235. <para><literal>spring.jar</literal></para>
  2236. </listitem>
  2237. <listitem>
  2238. <para><literal>acegi-security-resin-lib.jar</literal></para>
  2239. </listitem>
  2240. <listitem>
  2241. <para><literal>commons-codec.jar</literal></para>
  2242. </listitem>
  2243. <listitem>
  2244. <para><literal>burlap.jar</literal></para>
  2245. </listitem>
  2246. <listitem>
  2247. <para><literal>hessian.jar</literal></para>
  2248. </listitem>
  2249. </itemizedlist></para>
  2250. <para>Unlike the container-wide <literal>acegisecurity.xml</literal>
  2251. files used by other container adapters, each Resin web application
  2252. will contain its own
  2253. <literal>WEB-INF/resin-acegisecurity.xml</literal> file. Each web
  2254. application will also contain a <literal>resin-web.xml</literal> file
  2255. which Resin uses to start the container adapter:</para>
  2256. <para><programlisting>&lt;web-app&gt;
  2257. &lt;authenticator&gt;
  2258. &lt;type&gt;net.sf.acegisecurity.adapters.resin.ResinAcegiAuthenticator&lt;/type&gt;
  2259. &lt;init&gt;
  2260. &lt;app-context-location&gt;WEB-INF/resin-acegisecurity.xml&lt;/app-context-location&gt;
  2261. &lt;key&gt;my_password&lt;/key&gt;
  2262. &lt;/init&gt;
  2263. &lt;/authenticator&gt;
  2264. &lt;/web-app&gt;</programlisting></para>
  2265. <para>With the basic configuration provided above, none of the JAR
  2266. files listed (or <literal>acegi-security.jar</literal>) should be in
  2267. your application's <literal>WEB-INF/lib</literal>. The realm name
  2268. indicated in your <literal>web.xml</literal> does not matter with
  2269. Resin, as the relevant authentication class is indicated by the
  2270. <literal>&lt;authenticator&gt;</literal> setting.</para>
  2271. </sect2>
  2272. </sect1>
  2273. <sect1 id="security-cas">
  2274. <title>Yale Central Authentication Service (CAS) Single Sign On</title>
  2275. <sect2 id="security-cas-overview">
  2276. <title>Overview</title>
  2277. <para>Yale University produces an enterprise-wide single sign on
  2278. system known as CAS. Unlike other initiatives, Yale's Central
  2279. Authentication Service is open source, widely used, simple to
  2280. understand, platform independent, and supports proxy capabilities. The
  2281. Acegi Security System for Spring fully supports CAS, and provides an
  2282. easy migration path from single-application deployments of Acegi
  2283. Security through to multiple-application deployments secured by an
  2284. enterprise-wide CAS server.</para>
  2285. <para>You can learn more about CAS at
  2286. <literal>http://www.yale.edu/tp/auth/</literal>. You will need to
  2287. visit this URL to download the CAS Server files. Whilst the Acegi
  2288. Security System for Spring includes two CAS libraries in the
  2289. "-with-dependencies" ZIP file, you will still need the CAS Java Server
  2290. Pages and <literal>web.xml</literal> to customise and deploy your CAS
  2291. server.</para>
  2292. </sect2>
  2293. <sect2 id="security-cas-how-cas-works">
  2294. <title>How CAS Works</title>
  2295. <para>Whilst the CAS web site above contains two documents that detail
  2296. the architecture of CAS, we present the general overview again here
  2297. within the context of the Acegi Security System for Spring. The
  2298. following refers to CAS 2.0, being the version of CAS that Acegi
  2299. Security System for Spring supports.</para>
  2300. <para>Somewhere in your enterprise you will need to setup a CAS
  2301. server. The CAS server is simply a standard WAR file, so there isn't
  2302. anything difficult about setting up your server. Inside the WAR file
  2303. you will customise the login and other single sign on pages displayed
  2304. to users. You will also need to specify in the web.xml a
  2305. <literal>PasswordHandler</literal>. The
  2306. <literal>PasswordHandler</literal> has a simple method that returns a
  2307. boolean as to whether a given username and password is valid. Your
  2308. <literal>PasswordHandler</literal> implementation will need to link
  2309. into some type of backend authentication repository, such as an LDAP
  2310. server or database.</para>
  2311. <para>If you are already running an existing CAS server instance, you
  2312. will have already established a <literal>PasswordHandler</literal>. If
  2313. you do not already have a <literal>PasswordHandler</literal>, you
  2314. might prefer to use the Acegi Security System for Spring
  2315. <literal>CasPasswordHandler</literal> class. This class delegates
  2316. through to the standard Acegi Security
  2317. <literal>AuthenticationManager</literal>, enabling you to use a
  2318. security configuration you might already have in place. You do not
  2319. need to use the <literal>CasPasswordHandler</literal> class on your
  2320. CAS server if you do not wish. The Acegi Security System for Spring
  2321. will function as a CAS client successfully irrespective of the
  2322. <literal>PasswordHandler</literal> you've chosen for your CAS
  2323. server.</para>
  2324. <para>Apart from the CAS server itself, the other key player is of
  2325. course the secure web applications deployed throughout your
  2326. enterprise. These web applications are known as "services". There are
  2327. two types of services: standard services and proxy services. A proxy
  2328. service is able to request resources from other services on behalf of
  2329. the user. This will be explained more fully later.</para>
  2330. <para>Services can be developed in a large variety of languages, due
  2331. to CAS 2.0's very light XML-based protocol. The Yale CAS home page
  2332. contains a clients archive which demonstrates CAS clients in Java,
  2333. Active Server Pages, Perl, Python and others. Naturally, Java support
  2334. is very strong given the CAS server is written in Java. You do not
  2335. need to use any of CAS' client classes in applications secured by the
  2336. Acegi Security System for Spring. This is handled transparently for
  2337. you.</para>
  2338. <para>The basic interaction between a web browser, CAS server and an
  2339. Acegi Security for System Spring secured service is as follows:</para>
  2340. <orderedlist>
  2341. <listitem>
  2342. <para>The web user is browsing the service's public pages. CAS or
  2343. Acegi Security is not involved.</para>
  2344. </listitem>
  2345. <listitem>
  2346. <para>The user eventually requests a page that is either secure or
  2347. one of the beans it uses is secure. Acegi Security's
  2348. <literal>SecurityEnforcementFilter</literal> will detect the
  2349. <literal>AuthenticationException</literal>.</para>
  2350. </listitem>
  2351. <listitem>
  2352. <para>Because the user's <literal>Authentication</literal> object
  2353. (or lack thereof) caused an
  2354. <literal>AuthenticationException</literal>, the
  2355. <literal>SecurityEnforcementFilter</literal> will call the
  2356. configured <literal>AuthenticationEntryPoint</literal>. If using
  2357. CAS, this will be the
  2358. <literal>CasProcessingFilterEntryPoint</literal> class.</para>
  2359. </listitem>
  2360. <listitem>
  2361. <para>The <literal>CasProcessingFilterEntry</literal> point will
  2362. redirect the user's browser to the CAS server. It will also
  2363. indicate a <literal>service</literal> parameter, which is the
  2364. callback URL for the Acegi Security service. For example, the URL
  2365. to which the browser is redirected might be
  2366. <literal>https://my.company.com/cas/login?service=https%3A%2F%2Fserver3.company.com%2Fwebapp%2Fj_acegi_cas_security_check</literal>.</para>
  2367. </listitem>
  2368. <listitem>
  2369. <para>After the user's browser redirects to CAS, they will be
  2370. prompted for their username and password. If the user presents a
  2371. session cookie which indicates they've previously logged on, they
  2372. will not be prompted to login again (there is an exception to this
  2373. procedure, which we'll cover later). CAS will use the
  2374. <literal>PasswordHandler</literal> discussed above to decide
  2375. whether the username and password is valid.</para>
  2376. </listitem>
  2377. <listitem>
  2378. <para>Upon successful login, CAS will redirect the user's browser
  2379. back to the original service. It will also include a
  2380. <literal>ticket</literal> parameter, which is an opaque string
  2381. representing the "service ticket". Continuing our earlier example,
  2382. the URL the browser is redirected to might be
  2383. <literal>https://server3.company.com/webapp/j_acegi_cas_security_check?ticket=ST-0-ER94xMJmn6pha35CQRoZ</literal>.</para>
  2384. </listitem>
  2385. <listitem>
  2386. <para>Back in the service web application, the
  2387. <literal>CasProcessingFilter</literal> is always listening for
  2388. requests to <literal>/j_acegi_cas_security_check</literal> (this
  2389. is configurable, but we'll use the defaults in this introduction).
  2390. The processing filter will construct a
  2391. <literal>UsernamePasswordAuthenticationToken</literal>
  2392. representing the service ticket. The principal will be equal to
  2393. <literal>CasProcessingFilter.CAS_STATEFUL_IDENTIFIER</literal>,
  2394. whilst the credentials will be the service ticket opaque value.
  2395. This authentication request will then be handed to the configured
  2396. <literal>AuthenticationManager</literal>.</para>
  2397. </listitem>
  2398. <listitem>
  2399. <para>The <literal>AuthenticationManager</literal> implementation
  2400. will be the <literal>ProviderManager</literal>, which is in turn
  2401. configured with the <literal>CasAuthenticationProvider</literal>.
  2402. The <literal>CasAuthenticationProvider</literal> only responds to
  2403. <literal>UsernamePasswordAuthenticationToken</literal>s containing
  2404. the CAS-specific principal (such as
  2405. <literal>CasProcessingFilter.CAS_STATEFUL_IDENTIFIER</literal>)
  2406. and <literal>CasAuthenticationToken</literal>s (discussed
  2407. later).</para>
  2408. </listitem>
  2409. <listitem>
  2410. <para><literal>CasAuthenticationProvider</literal> will validate
  2411. the service ticket using a <literal>TicketValidator</literal>
  2412. implementation. Acegi Security includes one implementation, the
  2413. <literal>CasProxyTicketValidator</literal>. This implementation a
  2414. ticket validation class included in the CAS client library. The
  2415. <literal>CasProxyTicketValidator</literal> makes a HTTPS request
  2416. to the CAS server in order to validate the service ticket. The
  2417. <literal>CasProxyTicketValidator</literal> may also include a
  2418. proxy callback URL, which is included in this example:
  2419. <literal>https://my.company.com/cas/proxyValidate?service=https%3A%2F%2Fserver3.company.com%2Fwebapp%2Fj_acegi_cas_security_check&amp;ticket=ST-0-ER94xMJmn6pha35CQRoZ&amp;pgtUrl=https://server3.company.com/webapp/casProxy/receptor</literal>.</para>
  2420. </listitem>
  2421. <listitem>
  2422. <para>Back on the CAS server, the proxy validation request will be
  2423. received. If the presented service ticket matches the service URL
  2424. the ticket was issued to, CAS will provide an affirmative response
  2425. in XML indicating the username. If any proxy was involved in the
  2426. authentication (discussed below), the list of proxies is also
  2427. included in the XML response.</para>
  2428. </listitem>
  2429. <listitem>
  2430. <para>[OPTIONAL] If the request to the CAS validation service
  2431. included the proxy callback URL (in the <literal>pgtUrl</literal>
  2432. parameter), CAS will include a <literal>pgtIou</literal> string in
  2433. the XML response. This <literal>pgtIou</literal> represents a
  2434. proxy-granting ticket IOU. The CAS server will then create its own
  2435. HTTPS connection back to the <literal>pgtUrl</literal>. This is to
  2436. mutually authenticate the CAS server and the claimed service URL.
  2437. The HTTPS connection will be used to send a proxy granting ticket
  2438. to the original web application. For example,
  2439. <literal>https://server3.company.com/webapp/casProxy/receptor?pgtIou=PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt&amp;pgtId=PGT-1-si9YkkHLrtACBo64rmsi3v2nf7cpCResXg5MpESZFArbaZiOKH</literal>.
  2440. We suggest you use CAS' <literal>ProxyTicketReceptor</literal>
  2441. servlet to receive these proxy-granting tickets, if they are
  2442. required.</para>
  2443. </listitem>
  2444. <listitem>
  2445. <para>The <literal>CasProxyTicketValidator</literal> will parse
  2446. the XML received from the CAS server. It will return to the
  2447. <literal>CasAuthenticationProvider</literal> a
  2448. <literal>TicketResponse</literal>, which includes the username
  2449. (mandatory), proxy list (if any were involved), and proxy-granting
  2450. ticket IOU (if the proxy callback was requested).</para>
  2451. </listitem>
  2452. <listitem>
  2453. <para>Next <literal>CasAuthenticationProvider</literal> will call
  2454. a configured <literal>CasProxyDecider</literal>. The
  2455. <literal>CasProxyDecider</literal> indicates whether the proxy
  2456. list in the <literal>TicketResponse</literal> is acceptable to the
  2457. service. Several implementations are provided with the Acegi
  2458. Security System: <literal>RejectProxyTickets</literal>,
  2459. <literal>AcceptAnyCasProxy</literal> and
  2460. <literal>NamedCasProxyDecider</literal>. These names are largely
  2461. self-explanatory, except <literal>NamedCasProxyDecider</literal>
  2462. which allows a <literal>List</literal> of trusted proxies to be
  2463. provided.</para>
  2464. </listitem>
  2465. <listitem>
  2466. <para><literal>CasAuthenticationProvider</literal> will next
  2467. request a <literal>CasAuthoritiesPopulator</literal> to advise the
  2468. <literal>GrantedAuthority</literal> objects that apply to the user
  2469. contained in the <literal>TicketResponse</literal>. Acegi Security
  2470. includes a <literal>DaoCasAuthoritiesPopulator</literal> which
  2471. simply uses the <literal>AuthenticationDao</literal>
  2472. infrastructure to find the <literal>UserDetails</literal> and
  2473. their associated <literal>GrantedAuthority</literal>s. Note that
  2474. the password and enabled/disabled status of
  2475. <literal>UserDetails</literal> returned by the
  2476. <literal>AuthenticationDao</literal> are ignored, as the CAS
  2477. server is responsible for authentication decisions.
  2478. <literal>DaoCasAuthoritiesPopulator</literal> is only concerned
  2479. with retrieving the <literal>GrantedAuthority</literal>s.</para>
  2480. </listitem>
  2481. <listitem>
  2482. <para>If there were no problems,
  2483. <literal>CasAuthenticationProvider</literal> constructs a
  2484. <literal>CasAuthenticationToken</literal> including the details
  2485. contained in the <literal>TicketResponse</literal> and the
  2486. <literal>GrantedAuthority</literal>s. The
  2487. <literal>CasAuthenticationToken</literal> contains the hash of a
  2488. key, so that the <literal>CasAuthenticationProvider</literal>
  2489. knows it created it.</para>
  2490. </listitem>
  2491. <listitem>
  2492. <para>Control then returns to
  2493. <literal>CasProcessingFilter</literal>, which places the created
  2494. <literal>CasAuthenticationToken</literal> into the
  2495. <literal>HttpSession</literal> attribute named
  2496. <literal>HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY</literal>.</para>
  2497. </listitem>
  2498. <listitem>
  2499. <para>The user's browser is redirected to the original page that
  2500. caused the <literal>AuthenticationException</literal>.</para>
  2501. </listitem>
  2502. <listitem>
  2503. <para>As the <literal>Authentication</literal> object is now in
  2504. the well-known location, it is handled like any other
  2505. authentication approach. Usually the
  2506. <literal>AutoIntegrationFilter</literal> will be used to associate
  2507. the <literal>Authentication</literal> object with the
  2508. <literal>ContextHolder</literal> for the duration of each
  2509. request.</para>
  2510. </listitem>
  2511. </orderedlist>
  2512. <para>It's good that you're still here! It might sound involved, but
  2513. you can relax as the Acegi Security System for Spring classes hide
  2514. much of the complexity. Let's now look at how this is
  2515. configured.</para>
  2516. </sect2>
  2517. <sect2 id="security-cas-install-server">
  2518. <title>CAS Server Installation (Optional)</title>
  2519. <para>As mentioned above, the Acegi Security System for Spring
  2520. includes a <literal>PasswordHandler</literal> that bridges your
  2521. existing <literal>AuthenticationManager</literal> into CAS. You do not
  2522. need to use this <literal>PasswordHandler</literal> to use Acegi
  2523. Security on the client side (any CAS
  2524. <literal>PasswordHandler</literal> will do).</para>
  2525. <para>To install, you will need to download and extract the CAS server
  2526. archive. We used version 2.0.12. There will be a
  2527. <literal>/web</literal> directory in the root of the deployment. Copy
  2528. an <literal>applicationContext.xml</literal> containing your
  2529. <literal>AuthenticationManager</literal> as well as the
  2530. <literal>CasPasswordHandler</literal> into the
  2531. <literal>/web/WEB-INF</literal> directory. A sample
  2532. <literal>applicationContext.xml</literal> is included below:</para>
  2533. <programlisting>&lt;bean id="inMemoryDaoImpl" class="net.sf.acegisecurity.providers.dao.memory.InMemoryDaoImpl"&gt;
  2534. &lt;property name="userMap"&gt;
  2535. &lt;value&gt;
  2536. marissa=koala,ROLES_IGNORED_BY_CAS
  2537. dianne=emu,ROLES_IGNORED_BY_CAS
  2538. scott=wombat,ROLES_IGNORED_BY_CAS
  2539. peter=opal,disabled,ROLES_IGNORED_BY_CAS
  2540. &lt;/value&gt;
  2541. &lt;/property&gt;
  2542. &lt;/bean&gt;
  2543. &lt;bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;
  2544. &lt;property name="authenticationDao"&gt;&lt;ref bean="inMemoryDaoImpl"/&gt;&lt;/property&gt;
  2545. &lt;/bean&gt;
  2546. &lt;bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager"&gt;
  2547. &lt;property name="providers"&gt;
  2548. &lt;list&gt;
  2549. &lt;ref bean="daoAuthenticationProvider"/&gt;
  2550. &lt;/list&gt;
  2551. &lt;/property&gt;
  2552. &lt;/bean&gt;
  2553. &lt;bean id="casPasswordHandler" class="net.sf.acegisecurity.adapters.cas.CasPasswordHandler"&gt;
  2554. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  2555. &lt;/bean&gt;</programlisting>
  2556. <para>Note the granted authorities are ignored by CAS because it has
  2557. no way of communicating the granted authorities to calling
  2558. applications. CAS is only concerned with username and passwords (and
  2559. the enabled/disabled status).</para>
  2560. <para>Next you will need to edit the existing
  2561. <literal>/web/WEB-INF/web.xml</literal> file. Add (or edit in the case
  2562. of the <literal>authHandler</literal> property) the following
  2563. lines:</para>
  2564. <para><programlisting>&lt;context-param&gt;
  2565. &lt;param-name&gt;edu.yale.its.tp.cas.authHandler&lt;/param-name&gt;
  2566. &lt;param-value&gt;net.sf.acegisecurity.adapters.cas.CasPasswordHandlerProxy&lt;/param-value&gt;
  2567. &lt;/context-param&gt;
  2568. &lt;context-param&gt;
  2569. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
  2570. &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
  2571. &lt;/context-param&gt;
  2572. &lt;listener&gt;
  2573. &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  2574. &lt;/listener&gt;</programlisting></para>
  2575. <para>Copy the <literal>spring.jar</literal> and
  2576. <literal>acegi-security.jar</literal> files into
  2577. <literal>/web/WEB-INF/lib</literal>. Now use the <literal>ant
  2578. dist</literal> task in the <literal>build.xml</literal> in the root of
  2579. the directory structure. This will create
  2580. <literal>/lib/cas.war</literal>, which is ready for deployment to your
  2581. servlet container.</para>
  2582. <para>Note CAS heavily relies on HTTPS. You can't even test the system
  2583. without a HTTPS certificate. Whilst you should refer to your web
  2584. container's documentation on setting up HTTPS, if you need some
  2585. additional help or a test certificate you might like to check the
  2586. <literal>samples/contacts/etc/ssl</literal> directory.</para>
  2587. </sect2>
  2588. <sect2 id="security-cas-install-client">
  2589. <title>CAS Acegi Security System Client Installation</title>
  2590. <para>The web application side of CAS is made easy due to the Acegi
  2591. Security System for Spring. It is assumed you already know the basics
  2592. of using the Acegi Security System for Spring, so these are not
  2593. covered again below. Only the CAS-specific beans are mentioned.</para>
  2594. <para>You will need to add a <literal>ServiceProperties</literal> bean
  2595. to your application context. This represents your service:</para>
  2596. <para><programlisting>&lt;bean id="serviceProperties" class="net.sf.acegisecurity.ui.cas.ServiceProperties"&gt;
  2597. &lt;property name="service"&gt;&lt;value&gt;https://localhost:8443/contacts-cas/j_acegi_cas_security_check&lt;/value&gt;&lt;/property&gt;
  2598. &lt;property name="sendRenew"&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
  2599. &lt;/bean&gt;</programlisting></para>
  2600. <para>The <literal>service</literal> must equal a URL that will be
  2601. monitored by the <literal>CasProcessingFilter</literal>. The
  2602. <literal>sendRenew</literal> defaults to false, but should be set to
  2603. true if your application is particularly sensitive. What this
  2604. parameter does is tell the CAS login service that a single sign on
  2605. login is unacceptable. Instead, the user will need to re-enter their
  2606. username and password in order to gain access to the service.</para>
  2607. <para>The following beans should be configured to commence the CAS
  2608. authentication process:</para>
  2609. <para><programlisting>&lt;bean id="casProcessingFilter" class="net.sf.acegisecurity.ui.cas.CasProcessingFilter"&gt;
  2610. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  2611. &lt;property name="authenticationFailureUrl"&gt;&lt;value&gt;/casfailed.jsp&lt;/value&gt;&lt;/property&gt;
  2612. &lt;property name="defaultTargetUrl"&gt;&lt;value&gt;/&lt;/value&gt;&lt;/property&gt;
  2613. &lt;property name="filterProcessesUrl"&gt;&lt;value&gt;/j_acegi_cas_security_check&lt;/value&gt;&lt;/property&gt;
  2614. &lt;/bean&gt;
  2615. &lt;bean id="securityEnforcementFilter" class="net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter"&gt;
  2616. &lt;property name="filterSecurityInterceptor"&gt;&lt;ref bean="filterInvocationInterceptor"/&gt;&lt;/property&gt;
  2617. &lt;property name="authenticationEntryPoint"&gt;&lt;ref bean="casProcessingFilterEntryPoint"/&gt;&lt;/property&gt;
  2618. &lt;/bean&gt;
  2619. &lt;bean id="casProcessingFilterEntryPoint" class="net.sf.acegisecurity.ui.cas.CasProcessingFilterEntryPoint"&gt;
  2620. &lt;property name="loginUrl"&gt;&lt;value&gt;https://localhost:8443/cas/login&lt;/value&gt;&lt;/property&gt;
  2621. &lt;property name="serviceProperties"&gt;&lt;ref bean="serviceProperties"/&gt;&lt;/property&gt;
  2622. &lt;/bean&gt;</programlisting></para>
  2623. <para>You will also need to add the
  2624. <literal>CasProcessingFilter</literal> to web.xml:</para>
  2625. <para><programlisting>&lt;filter&gt;
  2626. &lt;filter-name&gt;Acegi CAS Processing Filter&lt;/filter-name&gt;
  2627. &lt;filter-class&gt;net.sf.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  2628. &lt;init-param&gt;
  2629. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  2630. &lt;param-value&gt;net.sf.acegisecurity.ui.cas.CasProcessingFilter&lt;/param-value&gt;
  2631. &lt;/init-param&gt;
  2632. &lt;/filter&gt;
  2633. &lt;filter-mapping&gt;
  2634. &lt;filter-name&gt;Acegi CAS Processing Filter&lt;/filter-name&gt;
  2635. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  2636. &lt;/filter-mapping&gt;</programlisting></para>
  2637. <para>The <literal>CasProcessingFilter</literal> has very similar
  2638. properties to the <literal>AuthenticationProcessingFilter</literal>
  2639. (used for form-based logins). Each property is
  2640. self-explanatory.</para>
  2641. <para>For CAS to operate, the
  2642. <literal>SecurityEnforcementFilter</literal> must have its
  2643. <literal>authenticationEntryPoint</literal> property set to the
  2644. <literal>CasProcessingFilterEntryPoint</literal> bean.</para>
  2645. <para>The <literal>CasProcessingFilterEntryPoint</literal> must refer
  2646. to the <literal>ServiceProperties</literal> bean (discussed above),
  2647. which provides the URL to the enterprise's CAS login server. This is
  2648. where the user's browser will be redirected.</para>
  2649. <para>Next you need to add an <literal>AuthenticationManager</literal>
  2650. that uses <literal>CasAuthenticationProvider</literal> and its
  2651. collaborators:</para>
  2652. <para><programlisting>&lt;bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager"&gt;
  2653. &lt;property name="providers"&gt;
  2654. &lt;list&gt;
  2655. &lt;ref bean="casAuthenticationProvider"/&gt;
  2656. &lt;/list&gt;
  2657. &lt;/property&gt;
  2658. &lt;/bean&gt;
  2659. &lt;bean id="casAuthenticationProvider" class="net.sf.acegisecurity.providers.cas.CasAuthenticationProvider"&gt;
  2660. &lt;property name="casAuthoritiesPopulator"&gt;&lt;ref bean="casAuthoritiesPopulator"/&gt;&lt;/property&gt;
  2661. &lt;property name="casProxyDecider"&gt;&lt;ref bean="casProxyDecider"/&gt;&lt;/property&gt;
  2662. &lt;property name="ticketValidator"&gt;&lt;ref bean="casProxyTicketValidator"/&gt;&lt;/property&gt;
  2663. &lt;property name="statelessTicketCache"&gt;&lt;ref bean="statelessTicketCache"/&gt;&lt;/property&gt;
  2664. &lt;property name="key"&gt;&lt;value&gt;my_password_for_this_auth_provider_only&lt;/value&gt;&lt;/property&gt;
  2665. &lt;/bean&gt;
  2666. &lt;bean id="casProxyTicketValidator" class="net.sf.acegisecurity.providers.cas.ticketvalidator.CasProxyTicketValidator"&gt;
  2667. &lt;property name="casValidate"&gt;&lt;value&gt;https://localhost:8443/cas/proxyValidate&lt;/value&gt;&lt;/property&gt;
  2668. &lt;property name="proxyCallbackUrl"&gt;&lt;value&gt;https://localhost:8443/contacts-cas/casProxy/receptor&lt;/value&gt;&lt;/property&gt;
  2669. &lt;property name="serviceProperties"&gt;&lt;ref bean="serviceProperties"/&gt;&lt;/property&gt;
  2670. &lt;!-- &lt;property name="trustStore"&gt;&lt;value&gt;/some/path/to/your/lib/security/cacerts&lt;/value&gt;&lt;/property&gt; --&gt;
  2671. &lt;/bean&gt;
  2672. &lt;bean id="statelessTicketCache" class="net.sf.acegisecurity.providers.cas.cache.EhCacheBasedTicketCache"&gt;
  2673. &lt;property name="minutesToIdle"&gt;&lt;value&gt;20&lt;/value&gt;&lt;/property&gt;
  2674. &lt;/bean&gt;
  2675. &lt;bean id="casAuthoritiesPopulator" class="net.sf.acegisecurity.providers.cas.populator.DaoCasAuthoritiesPopulator"&gt;
  2676. &lt;property name="authenticationDao"&gt;&lt;ref bean="inMemoryDaoImpl"/&gt;&lt;/property&gt;
  2677. &lt;/bean&gt;
  2678. &lt;bean id="casProxyDecider" class="net.sf.acegisecurity.providers.cas.proxy.RejectProxyTickets"/&gt;</programlisting></para>
  2679. <para>The beans are all reasonable self-explanatory if you refer back
  2680. to the "How CAS Works" section. Careful readers might notice one
  2681. surprise: the <literal>statelessTicketCache</literal> property of the
  2682. <literal>CasAuthenticationProvider</literal>. This is discussed in
  2683. detail in the "Advanced CAS Usage" section.</para>
  2684. <para>Note the <literal>CasProxyTicketValidator</literal> has a
  2685. remarked out <literal>trustStore</literal> property. This property
  2686. might be helpful if you experience HTTPS certificate issues. Also note
  2687. the <literal>proxyCallbackUrl</literal> is set so the service can
  2688. receive a proxy-granting ticket. As mentioned above, this is optional
  2689. and unnecessary if you do not require proxy-granting tickets. If you
  2690. do use this feature, you will need to configure a suitable servlet to
  2691. receive the proxy-granting tickets. We suggest you use CAS'
  2692. <literal>ProxyTicketReceptor</literal> by adding the following to your
  2693. web application's <literal>web.xml</literal>:</para>
  2694. <para><programlisting>&lt;servlet&gt;
  2695. &lt;servlet-name&gt;casproxy&lt;/servlet-name&gt;
  2696. &lt;servlet-class&gt;edu.yale.its.tp.cas.proxy.ProxyTicketReceptor&lt;/servlet-class&gt;
  2697. &lt;/servlet&gt;
  2698. &lt;servlet-mapping&gt;
  2699. &lt;servlet-name&gt;casproxy&lt;/servlet-name&gt;
  2700. &lt;url-pattern&gt;/casProxy/*&lt;/url-pattern&gt;
  2701. &lt;/servlet-mapping&gt;</programlisting></para>
  2702. <para>This completes the configuration of CAS. If you haven't made any
  2703. mistakes, your web application should happily work within the
  2704. framework of CAS single sign on. No other parts of the Acegi Security
  2705. System for Spring need to be concerned about the fact CAS handled
  2706. authentication.</para>
  2707. <para>There is also a <literal>contacts-cas.war</literal> file in the
  2708. sample applications directory. This sample application uses the above
  2709. settings and can be deployed to see CAS in operation.</para>
  2710. </sect2>
  2711. <sect2 id="security-cas-advanced-usage">
  2712. <title>Advanced CAS Usage</title>
  2713. <para>The <literal>CasAuthenticationProvider</literal> distinguishes
  2714. between stateful and stateless clients. A stateful client is
  2715. considered any that originates via the
  2716. <literal>CasProcessingFilter</literal>. A stateless client is any that
  2717. presents an authentication request via the
  2718. <literal>UsernamePasswordAuthenticationToken</literal> with a
  2719. principal equal to
  2720. <literal>CasProcessingFilter.CAS_STATELESS_IDENTIFIER</literal>.</para>
  2721. <para>Stateless clients are likely to be via remoting protocols such
  2722. as Hessian and Burlap. The <literal>BasicProcessingFilter</literal> is
  2723. still used in this case, but the remoting protocol client is expected
  2724. to present a username equal to the static string above, and a password
  2725. equal to a CAS service ticket. Clients should acquire a CAS service
  2726. ticket directly from the CAS server.</para>
  2727. <para>Because remoting protocols have no way of presenting themselves
  2728. within the context of a <literal>HttpSession</literal>, it isn't
  2729. possible to rely on the <literal>HttpSession</literal>'s
  2730. <literal>HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY</literal>
  2731. attribute to locate the <literal>CasAuthenticationToken</literal>.
  2732. Furthermore, because the CAS server invalidates a service ticket after
  2733. it has been validated by the <literal>TicketValidator</literal>,
  2734. presenting the same service ticket on subsequent requests will not
  2735. work. It is similarly very difficult to obtain a proxy-granting ticket
  2736. for a remoting protocol client, as they are often deployed on client
  2737. machines which rarely have HTTPS URLs that would be accessible to the
  2738. CAS server.</para>
  2739. <para>One obvious option is to not use CAS at all for remoting
  2740. protocol clients. However, this would eliminate many of the desirable
  2741. features of CAS.</para>
  2742. <para>As a middle-ground, the
  2743. <literal>CasAuthenticationProvider</literal> uses a
  2744. <literal>StatelessTicketCache</literal>. This is used solely for
  2745. requests with a principal equal to
  2746. <literal>CasProcessingFilter.CAS_STATELESS_IDENTIFIER</literal>. What
  2747. happens is the <literal>CasAuthenticationProvider</literal> will store
  2748. the resulting <literal>CasAuthenticationToken</literal> in the
  2749. <literal>StatelessTicketCache</literal>, keyed on the service ticket.
  2750. Accordingly, remoting protocol clients can present the same service
  2751. ticket and the <literal>CasAuthenticationProvider</literal> will not
  2752. need to contact the CAS server for validation (aside from the first
  2753. request).</para>
  2754. <para>The other aspect of advanced CAS usage involves creating proxy
  2755. tickets from the proxy-granting ticket. As indicated above, we
  2756. recommend you use CAS' <literal>ProxyTicketReceptor</literal> to
  2757. receive these tickets. The <literal>ProxyTicketReceptor</literal>
  2758. provides a static method that enables you to obtain a proxy ticket by
  2759. presenting the proxy-granting IOU ticket. You can obtain the
  2760. proxy-granting IOU ticket by calling
  2761. <literal>CasAuthenticationToken.getProxyGrantingTicketIou()</literal>.</para>
  2762. <para>It is hoped you find CAS integration easy and useful with the
  2763. Acegi Security System for Spring classes. Welcome to enterprise-wide
  2764. single sign on!</para>
  2765. </sect2>
  2766. </sect1>
  2767. <sect1 id="security-channels">
  2768. <title>Channel Security</title>
  2769. <sect2 id="security-channels-overview">
  2770. <title>Overview</title>
  2771. <para>In addition to coordinating the authentication and authorization
  2772. requirements of your application, the Acegi Security System for Spring
  2773. is also able to ensure unauthenticated web requests have certain
  2774. properties. These properties may include being of a particular
  2775. transport type, having a particular <literal>HttpSession</literal>
  2776. attribute set and so on. The most common requirement is for your web
  2777. requests to be received using a particular transport protocol, such as
  2778. HTTPS.</para>
  2779. <para>An important issue in considering transport security is that of
  2780. session hijacking. Your web container manages a
  2781. <literal>HttpSession</literal> by reference to a
  2782. <literal>jsessionid</literal> that is sent to user agents either via a
  2783. cookie or URL rewriting. If the <literal>jsessionid</literal> is ever
  2784. sent over HTTP, there is a possibility that session identifier can be
  2785. intercepted and used to impersonate the user after they complete the
  2786. authentication process. This is because most web containers maintain
  2787. the same session identifier for a given user, even after they switch
  2788. from HTTP to HTTPS pages.</para>
  2789. <para>If session hijacking is considered too significant a risk for
  2790. your particular application, the only option is to use HTTPS for every
  2791. request. This means the <literal>jsessionid</literal> is never sent
  2792. across an insecure channel. You will need to ensure your
  2793. <literal>web.xml</literal>-defined
  2794. <literal>&lt;welcome-file&gt;</literal> points to a HTTPS location,
  2795. and the application never directs the user to a HTTP location. The
  2796. Acegi Security System for Spring provides a solution to assist with
  2797. the latter.</para>
  2798. </sect2>
  2799. <sect2 id="security-channels-installation">
  2800. <title>Configuration</title>
  2801. <para>To utilise Acegi Security's channel security services, add the
  2802. following lines to <literal>web.xml</literal>:</para>
  2803. <para><programlisting>&lt;filter&gt;
  2804. &lt;filter-name&gt;Acegi Channel Processing Filter&lt;/filter-name&gt;
  2805. &lt;filter-class&gt;net.sf.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  2806. &lt;init-param&gt;
  2807. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  2808. &lt;param-value&gt;net.sf.acegisecurity.securechannel.ChannelProcessingFilter&lt;/param-value&gt;
  2809. &lt;/init-param&gt;
  2810. &lt;/filter&gt;
  2811. &lt;filter-mapping&gt;
  2812. &lt;filter-name&gt;Acegi Channel Processing Filter&lt;/filter-name&gt;
  2813. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  2814. &lt;/filter-mapping&gt;</programlisting></para>
  2815. <para>As usual when running <literal>FilterToBeanProxy</literal>, you
  2816. will also need to configure the filter in your application
  2817. context:</para>
  2818. <para><programlisting>&lt;bean id="channelProcessingFilter" class="net.sf.acegisecurity.securechannel.ChannelProcessingFilter"&gt;
  2819. &lt;property name="channelDecisionManager"&gt;&lt;ref bean="channelDecisionManager"/&gt;&lt;/property&gt;
  2820. &lt;property name="filterInvocationDefinitionSource"&gt;
  2821. &lt;value&gt;
  2822. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  2823. \A/secure/.*\Z=REQUIRES_SECURE_CHANNEL
  2824. \A/acegilogin.jsp.*\Z=REQUIRES_SECURE_CHANNEL
  2825. \A/j_acegi_security_check.*\Z=REQUIRES_SECURE_CHANNEL
  2826. \A.*\Z=REQUIRES_INSECURE_CHANNEL
  2827. &lt;/value&gt;
  2828. &lt;/property&gt;
  2829. &lt;/bean&gt;
  2830. &lt;bean id="channelDecisionManager" class="net.sf.acegisecurity.securechannel.ChannelDecisionManagerImpl"&gt;
  2831. &lt;property name="channelProcessors"&gt;
  2832. &lt;list&gt;
  2833. &lt;ref bean="secureChannelProcessor"/&gt;
  2834. &lt;ref bean="insecureChannelProcessor"/&gt;
  2835. &lt;/list&gt;
  2836. &lt;/property&gt;
  2837. &lt;/bean&gt;
  2838. &lt;bean id="secureChannelProcessor" class="net.sf.acegisecurity.securechannel.SecureChannelProcessor"/&gt;
  2839. &lt;bean id="insecureChannelProcessor" class="net.sf.acegisecurity.securechannel.InsecureChannelProcessor"/&gt;</programlisting></para>
  2840. <para>Like <literal>FilterSecurityInterceptor</literal>, Apache Ant
  2841. style paths are also supported by the
  2842. <literal>ChannelProcessingFilter</literal>.</para>
  2843. <para>The <literal>ChannelProcessingFilter</literal> operates by
  2844. filtering all web requests and determining the configuration
  2845. attributes that apply. It then delegates to the
  2846. <literal>ChannelDecisionManager</literal>. The default implementation,
  2847. <literal>ChannelDecisionManagerImpl</literal>, should suffice in most
  2848. cases. It simply delegates through the list of configured
  2849. <literal>ChannelProcessor</literal> instances. A
  2850. <literal>ChannelProcessor</literal> will review the request, and if it
  2851. is unhappy with the request (eg it was received across the incorrect
  2852. transport protocol), it will perform a redirect, throw an exception or
  2853. take whatever other action is appropriate.</para>
  2854. <para>Included with the Acegi Security System for Spring are two
  2855. concrete <literal>ChannelProcessor</literal> implementations:
  2856. <literal>SecureChannelProcessor</literal> ensures requests with a
  2857. configuration attribute of <literal>REQUIRES_SECURE_CHANNEL</literal>
  2858. are received over HTTPS, whilst
  2859. <literal>InsecureChannelProcessor</literal> ensures requests with a
  2860. configuration attribute of
  2861. <literal>REQUIRES_INSECURE_CHANNEL</literal> are received over HTTP.
  2862. Both implementations delegate to a
  2863. <literal>ChannelEntryPoint</literal> if the required transport
  2864. protocol is not used. The two <literal>ChannelEntryPoint</literal>
  2865. implementations included with Acegi Security simply redirect the
  2866. request to HTTP and HTTPS as appropriate. Appropriate defaults are
  2867. assigned to the <literal>ChannelProcessor</literal> implementations
  2868. for the configuration attribute keywords they respond to and the
  2869. <literal>ChannelEntryPoint</literal> they delegate to, although you
  2870. have the ability to override these using the application
  2871. context.</para>
  2872. <para>Note that the redirections are absolute (eg
  2873. http://www.company.com:8080/app/page), not relative (eg /app/page).
  2874. During testing it was discovered that Internet Explorer 6 Service Pack
  2875. 1 has a bug whereby it does not respond correctly to a redirection
  2876. instruction which also changes the port to use. Accordingly, absolute
  2877. URLs are used in conjunction with bug detection logic in the
  2878. <literal>PortResolverImpl</literal> that is wired up by default to
  2879. many Acegi Security beans. Please refer to the JavaDocs for
  2880. <literal>PortResolverImpl</literal> for further details.</para>
  2881. </sect2>
  2882. <sect2 id="security-channels-usage">
  2883. <title>Usage</title>
  2884. <para>Once configured, using the channel security filter is very easy.
  2885. Simply request pages without regard to the protocol (ie HTTP or HTTPS)
  2886. or port (eg 80, 8080, 443, 8443 etc). Obviously you'll still need a
  2887. way of making the initial request (probably via the
  2888. <literal>web.xml</literal> <literal>&lt;welcome-file&gt;</literal> or
  2889. a well-known home page URL), but once this is done the filter will
  2890. perform redirects as defined by your application context.</para>
  2891. <para>You can also add your own <literal>ChannelProcessor</literal>
  2892. implementations to the <literal>ChannelDecisionManagerImpl</literal>.
  2893. For example, you might set a <literal>HttpSession</literal> attribute
  2894. when a human user is detected via a "enter the contents of this
  2895. graphic" procedure. Your <literal>ChannelProcessor</literal> would
  2896. respond to say <literal>REQUIRES_HUMAN_USER</literal> configuration
  2897. attributes and redirect to an appropriate entry point to start the
  2898. human user validation process if the <literal>HttpSession</literal>
  2899. attribute is not currently set.</para>
  2900. <para>To decide whether a security check belongs in a
  2901. <literal>ChannelProcessor</literal> or an
  2902. <literal>AccessDecisionVoter</literal>, remember that the former is
  2903. designed to handle unauthenticated requests, whilst the latter is
  2904. designed to handle authenticated requests. The latter therefore has
  2905. access to the granted authorities of the authenticated principal. In
  2906. addition, problems detected by a <literal>ChannelProcessor</literal>
  2907. will generally cause a HTTP/HTTPS redirection so its requirements can
  2908. be met, whilst problems detected by an
  2909. <literal>AccessDecisionVoter</literal> will ultimately result in an
  2910. <literal>AccessDeniedException</literal> (depending on the governing
  2911. <literal>AccessDecisionManager</literal>).</para>
  2912. </sect2>
  2913. </sect1>
  2914. <sect1 id="acls">
  2915. <title>Instance-Based Access Control List (ACL) Security</title>
  2916. <sect2 id="acls-overview">
  2917. <title>Overview</title>
  2918. <para>THIS FEATURE WAS ADDED IN VERSION 0.6. WE WELCOME YOUR COMMENTS
  2919. AND IMPROVEMENTS.</para>
  2920. <para>Complex applications often will find the need to define access
  2921. permissions not simply at a web request or method invocation level.
  2922. Instead, security decisions need to comprise both who
  2923. (<literal>Authentication</literal>), where
  2924. (<literal>MethodInvocation</literal>) and what
  2925. (<literal>SomeDomainObject</literal>). In other words, authorization
  2926. decisions also need to consider the actual domain object instance
  2927. subject of a method invocation.</para>
  2928. <para>Imagine you're designing an application for a pet clinic. There
  2929. will be two main groups of users of your Spring-based application:
  2930. staff of the pet clinic, as well as the pet clinic's customers. The
  2931. staff will have access to all of the data, whilst your customers will
  2932. only be able to see their own customer records. To make it a little
  2933. more interesting, your customers can allow other users to see their
  2934. customer records, such as their "puppy preschool "mentor or president
  2935. of their local "Pony Club". Using Acegi Security System for Spring as
  2936. the foundation, you have several approaches that can be
  2937. used:<orderedlist>
  2938. <listitem>
  2939. <para>Write your business methods to enforce the security. You
  2940. could consult a collection within the
  2941. <literal>Customer</literal> domain object instance to determine
  2942. which users have access. By using the
  2943. <literal>ContextHolder.getContext()</literal> and casting it to
  2944. <literal>SecureContext</literal>, you'll be able to access the
  2945. <literal>Authentication</literal> object.</para>
  2946. </listitem>
  2947. <listitem>
  2948. <para>Write an <literal>AccessDecisionVoter</literal> to enforce
  2949. the security from the <literal>GrantedAuthority[]</literal>s
  2950. stored in the <literal>Authentication</literal> object. This
  2951. would mean your <literal>AuthenticationManager</literal> would
  2952. need to populate the <literal>Authentication</literal> with
  2953. custom <literal>GrantedAuthority</literal>[]s representing each
  2954. of the <literal>Customer</literal> domain object instances the
  2955. principal has access to.</para>
  2956. </listitem>
  2957. <listitem>
  2958. <para>Write an <literal>AccessDecisionVoter</literal> to enforce
  2959. the security and open the target <literal>Customer</literal>
  2960. domain object directly. This would mean your voter needs access
  2961. to a DAO that allows it to retrieve the
  2962. <literal>Customer</literal> object. It would then access the
  2963. <literal>Customer</literal> object's collection of approved
  2964. users and make the appropriate decision.</para>
  2965. </listitem>
  2966. </orderedlist></para>
  2967. <para>Each one of these approaches is perfectly legitimate. However,
  2968. the first couples your authorization checking to your business code.
  2969. The main problems with this include the enhanced difficulty of unit
  2970. testing and the fact it would be more difficult to reuse the
  2971. <literal>Customer</literal> authorization logic elsewhere. Obtaining
  2972. the <literal>GrantedAuthority[]</literal>s from the
  2973. <literal>Authentication</literal> object is also fine, but will not
  2974. scale to large numbers of <literal>Customer</literal>s. If a user
  2975. might be able to access 5,000 <literal>Customer</literal>s (unlikely
  2976. in this case, but imagine if it were a popular vet for a large Pony
  2977. Club!) the amount of memory consumed and time required to construct
  2978. the <literal>Authentication</literal> object would be undesirable. The
  2979. final method, opening the <literal>Customer</literal> directly from
  2980. external code, is probably the best of the three. It achieves
  2981. separation of concerns, and doesn't misuse memory or CPU cycles, but
  2982. it is still inefficient in that both the
  2983. <literal>AccessDecisionVoter</literal> and the eventual business
  2984. method itself will perform a call to the DAO responsible for
  2985. retrieving the <literal>Customer</literal> object. Two accesses per
  2986. method invocation is clearly undesirable. In addition, with every
  2987. approach listed you'll need to write your own access control list
  2988. (ACL) persistence and business logic from scratch.</para>
  2989. <para>Fortunately, there is another alternative, which we'll talk
  2990. about below.</para>
  2991. </sect2>
  2992. <sect2 id="acls-acl-package">
  2993. <title>The net.sf.acegisecurity.acl Package</title>
  2994. <para>The <literal>net.sf.acegisecurity.acl</literal> package is very
  2995. simple, comprising only a handful of interfaces and a single class. It
  2996. provides the basic foundation for access control list (ACL) lookups.
  2997. The central interface is <literal>AclManager</literal>, which is
  2998. defined by two methods:</para>
  2999. <para><programlisting>public AclEntry[] getAcls(java.lang.Object domainInstance);
  3000. public AclEntry[] getAcls(java.lang.Object domainInstance, Authentication authentication);</programlisting></para>
  3001. <para><literal>AclManager</literal> is intended to be used as a
  3002. collaborator against your business objects, or, more desirably,
  3003. <literal>AccessDecisionVoter</literal>s. This means you use Spring's
  3004. normal <literal>ApplicationContext</literal> features to wire up your
  3005. <literal>AccessDecisionVoter</literal> (or business method) with an
  3006. <literal>AclManager</literal>. Consideration was given to placing the
  3007. ACL information in the <literal>ContextHolder</literal>, but it was
  3008. felt this would be inefficient both in terms of memory usage as well
  3009. as the time spent loading potentially unused ACL information. The
  3010. trade-off of needing to wire up a collaborator for those objects
  3011. requiring ACL information is rather minor, particularly in a
  3012. Spring-managed application.</para>
  3013. <para>The first method of the <literal>AclManager</literal> will
  3014. return all ACLs applying to the domain object instance passed to it.
  3015. The second method does the same, but only returns those ACLs which
  3016. apply to the passed <literal>Authentication</literal> object.</para>
  3017. <para>The <literal>AclEntry</literal> interface returned by
  3018. <literal>AclManager</literal> is merely a marker interface. You will
  3019. need to provide an implementation that reflects that ACL permissions
  3020. for your application.</para>
  3021. <para>Rounding out the <literal>net.sf.acegisecurity.acl</literal>
  3022. package is an <literal>AclProviderManager</literal> class, with a
  3023. corresponding <literal>AclProvider</literal> interface.
  3024. <literal>AclProviderManager</literal> is a concrete implementation of
  3025. <literal>AclManager</literal>, which iterates through registered
  3026. <literal>AclProvider</literal>s. The first
  3027. <literal>AclProvider</literal> that indicates it can authoritatively
  3028. provide ACL information for the presented domain object instance will
  3029. be used. This is very similar to the
  3030. <literal>AuthenticationProvider</literal> interface used for
  3031. authentication.</para>
  3032. <para>With this background, let's now look at a usable ACL
  3033. implementation.</para>
  3034. </sect2>
  3035. <sect2 id="acls-masking">
  3036. <title>Integer Masked ACLs</title>
  3037. <para>Acegi Security System for Spring includes a production-quality
  3038. ACL provider implementation. The implementation is based on integer
  3039. masking, which is commonly used for ACL permissions given its
  3040. flexibility and speed. Anyone who has used Unix's
  3041. <literal>chmod</literal> command will know all about this type of
  3042. permission masking (eg <literal>chmod 777</literal>). You'll find the
  3043. classes and interfaces for the integer masking ACL package under
  3044. <literal>net.sf.acegisecurity.acl.basic</literal>.</para>
  3045. <para>Extending the <literal>AclEntry</literal> interface is a
  3046. <literal>BasicAclEntry</literal> interface, with the main methods
  3047. shown below:</para>
  3048. <para><programlisting>public AclObjectIdentity getAclObjectIdentity();
  3049. public AclObjectIdentity getAclObjectParentIdentity();
  3050. public int getMask();
  3051. public java.lang.Object getRecipient();</programlisting></para>
  3052. <para>As shown, each <literal>BasicAclEntry</literal> has four main
  3053. properties. The <literal>mask</literal> is the integer that represents
  3054. the permissions granted to the <literal>recipient</literal>. The
  3055. <literal>aclObjectIdentity</literal> is able to identify the domain
  3056. object instance for which the ACL applies, and the
  3057. <literal>aclObjectParentIdentity</literal> optionally specifies the
  3058. parent of the domain object instance. Multiple
  3059. <literal>BasicAclEntry</literal>s usually exist against a single
  3060. domain object instance, and as suggested by the parent identity
  3061. property, permissions granted higher in the object hierarchy will
  3062. trickle down and be inherited (unless blocked by integer zero).</para>
  3063. <para><literal>BasicAclEntry</literal> implementations typically
  3064. provide convenience methods, such as
  3065. <literal>isReadAllowed()</literal>, to avoid application classes
  3066. needing to perform bit masking themselves. The
  3067. <literal>SimpleAclEntry</literal> and
  3068. <literal>AbstractBasicAclEntry</literal> demonstrate and provide much
  3069. of this bit masking logic.</para>
  3070. <para>The <literal>AclObjectIdentity</literal> itself is merely a
  3071. marker interface, so you need to provide implementations for your
  3072. domain objects. However, the package does include a
  3073. <literal>NamedEntityObjectIdentity</literal> implementation which will
  3074. suit many needs. The <literal>NamedEntityObjectIdentity</literal>
  3075. identifies a given domain object instance by the classname of the
  3076. instance and the identity of the instance. A
  3077. <literal>NamedEntityObjectIdentity</literal> can be constructed
  3078. manually (by calling the constructor and providing the classname and
  3079. identity <literal>String</literal>s), or by passing in any domain
  3080. object that contains a <literal>getId()</literal> method.</para>
  3081. <para>The actual <literal>AclProvider</literal> implementation is
  3082. named <literal>BasicAclProvider</literal>. It has adopted a similar
  3083. design to that used by the authentication-related
  3084. <literal>DaoAuthenticationProvder</literal>. Specifically, you define
  3085. a <literal>BasicAclDao</literal> against the provider, so different
  3086. ACL repository types can be accessed in a pluggable manner. The
  3087. <literal>BasicAclProvider</literal> also supports pluggable cache
  3088. providers (with Acegi Security System for Spring including an
  3089. implementation that fronts EH-CACHE).</para>
  3090. <para>The <literal>BasicAclDao</literal> interface is very simple to
  3091. implement:</para>
  3092. <para><programlisting>public BasicAclEntry[] getAcls(AclObjectIdentity aclObjectIdentity);</programlisting></para>
  3093. <para>A <literal>BasicAclDao</literal> implementation needs to
  3094. understand the presented <literal>AclObjectIdentity</literal> and how
  3095. it maps to a storage repository, find the relevant records, and create
  3096. appropriate <literal>BasicAclEntry</literal> objects and return
  3097. them.</para>
  3098. <para>Acegi Security includes a single <literal>BasicAclDao</literal>
  3099. implementation called <literal>JdbcDaoImpl</literal>. As implied by
  3100. the name, it accesses ACL information from a JDBC database. The
  3101. default database schema and some sample data will aid in understanding
  3102. its function:</para>
  3103. <para><programlisting>CREATE TABLE acl_object_identity (
  3104. id IDENTITY NOT NULL,
  3105. object_identity VARCHAR_IGNORECASE(250) NOT NULL,
  3106. parent_object INTEGER,
  3107. acl_class VARCHAR_IGNORECASE(250) NOT NULL,
  3108. CONSTRAINT unique_object_identity UNIQUE(object_identity),
  3109. FOREIGN KEY (parent_object) REFERENCES acl_object_identity(id)
  3110. );
  3111. CREATE TABLE acl_permission (
  3112. id IDENTITY NOT NULL,
  3113. acl_object_identity INTEGER NOT NULL,
  3114. recipient VARCHAR_IGNORECASE(100) NOT NULL,
  3115. mask INTEGER NOT NULL,
  3116. CONSTRAINT unique_recipient UNIQUE(acl_object_identity, recipient),
  3117. FOREIGN KEY (acl_object_identity) REFERENCES acl_object_identity(id)
  3118. );
  3119. INSERT INTO acl_object_identity VALUES (1, 'corp.DomainObject:1', null, 'net.sf.acegisecurity.acl.basic.SimpleAclEntry');
  3120. INSERT INTO acl_object_identity VALUES (2, 'corp.DomainObject:2', 1, 'net.sf.acegisecurity.acl.basic.SimpleAclEntry');
  3121. INSERT INTO acl_object_identity VALUES (3, 'corp.DomainObject:3', 1, 'net.sf.acegisecurity.acl.basic.SimpleAclEntry');
  3122. INSERT INTO acl_object_identity VALUES (4, 'corp.DomainObject:4', 1, 'net.sf.acegisecurity.acl.basic.SimpleAclEntry');
  3123. INSERT INTO acl_object_identity VALUES (5, 'corp.DomainObject:5', 3, 'net.sf.acegisecurity.acl.basic.SimpleAclEntry');
  3124. INSERT INTO acl_object_identity VALUES (6, 'corp.DomainObject:6', 3, 'net.sf.acegisecurity.acl.basic.SimpleAclEntry');
  3125. INSERT INTO acl_permission VALUES (null, 1, 'ROLE_SUPERVISOR', 1);
  3126. INSERT INTO acl_permission VALUES (null, 2, 'ROLE_SUPERVISOR', 0);
  3127. INSERT INTO acl_permission VALUES (null, 2, 'marissa', 2);
  3128. INSERT INTO acl_permission VALUES (null, 3, 'scott', 14);
  3129. INSERT INTO acl_permission VALUES (null, 6, 'scott', 1);</programlisting></para>
  3130. <para>As can be seen, database-specific constraints are used
  3131. extensively to ensure the integrity of the ACL information. If you
  3132. need to use a different database (Hypersonic SQL statements are shown
  3133. above), you should try to implement equivalent constraints.</para>
  3134. <para>The <literal>JdbcDaoImpl</literal> will only respond to requests
  3135. for <literal>NamedEntityObjectIdentity</literal>s. It converts such
  3136. identities into a single <literal>String</literal>, comprising
  3137. the<literal> NamedEntityObjectIdentity.getClassname()</literal> +
  3138. <literal>":"</literal> +
  3139. <literal>NamedEntityObjectIdentity.getId()</literal>. This yields the
  3140. type of <literal>object_identity</literal> values shown above. As
  3141. indicated by the sample data, each database row corresponds to a
  3142. single <literal>BasicAclEntry</literal>. As stated earlier and
  3143. demonstrated by <literal>corp.DomainObject:2</literal> in the above
  3144. sample data, each domain object instance will often have multiple
  3145. <literal>BasicAclEntry</literal>[]s.</para>
  3146. <para>As <literal>JdbcDaoImpl</literal> is required to return concrete
  3147. <literal>BasicAclEntry</literal> classes, it needs to know which
  3148. <literal>BasicAclEntry</literal> implementation it is to create and
  3149. populate. This is the role of the <literal>acl_class</literal> column.
  3150. <literal>JdbcDaoImpl</literal> will create the indicated class and set
  3151. its <literal>mask</literal>, <literal>recipient</literal>,
  3152. <literal>aclObjectIdentity</literal> and
  3153. <literal>aclObjectParentIdentity</literal> properties.</para>
  3154. <para>As you can probably tell from the sample data, the
  3155. <literal>parent_object_identity</literal> value can either be null or
  3156. in the same format as the <literal>object_identity</literal>. If
  3157. non-null, <literal>JdbcDaoImpl</literal> will create a
  3158. <literal>NamedEntityObjectIdentity</literal> to place inside the
  3159. returned <literal>BasicAclEntry</literal> class.</para>
  3160. <para>Returning to the <literal>BasicAclProvider</literal>, before it
  3161. can poll the <literal>BasicAclDao</literal> implementation it needs to
  3162. convert the domain object instance it was passed into an
  3163. <literal>AclObjectIdentity</literal>.
  3164. <literal>BasicAclProvider</literal> has a <literal>protected
  3165. AclObjectIdentity obtainIdentity(Object domainInstance)</literal>
  3166. method that is responsible for this. As a protected method, it enables
  3167. subclasses to easily override. The normal implementation checks
  3168. whether the passed domain object instance implements the
  3169. <literal>AclObjectIdentityAware</literal> interface, which is merely a
  3170. getter for an <literal>AclObjectIdentity</literal>. If the domain
  3171. object does implement this interface, that is the identity returned.
  3172. If the domain object does not implement this interface, the method
  3173. will attempt to create an <literal>AclObjectIdentity</literal> by
  3174. passing the domain object instance to the constructor of a class
  3175. defined by the
  3176. <literal>BasicAclProvider.getDefaultAclObjectIdentity()</literal>
  3177. method. By default the defined class is
  3178. <literal>NamedEntityObjectIdentity</literal>, which was described in
  3179. more detail above. Therefore, you will need to either (i) provide a
  3180. <literal>getId()</literal> method on your domain objects, (ii)
  3181. implement <literal>AclObjectIdentityAware</literal> on your domain
  3182. objects, (iii) provide an alternative
  3183. <literal>AclObjectIdentity</literal> implementation that will accept
  3184. your domain object in its constructor, or (iv) override the
  3185. <literal>obtainIdentity(Object)</literal> method.</para>
  3186. <para>Once the <literal>AclObjectIdentity</literal> of the domain
  3187. object instance is determined, the <literal>BasicAclProvider</literal>
  3188. will poll the DAO to obtain its <literal>BasicAclEntry</literal>[]s.
  3189. If any of the entries returned by the DAO indicate there is a parent,
  3190. that parent will be polled, and the process will repeat until there is
  3191. no further parent. The permissions assigned to a
  3192. <literal>recipient</literal> closest to the domain object instance
  3193. will always take priority and override any inherited permissions. From
  3194. the sample data above, the following inherited permissions would
  3195. apply:</para>
  3196. <para><programlisting>--- Mask integer 0 = no permissions
  3197. --- Mask integer 1 = administer
  3198. --- Mask integer 2 = read
  3199. --- Mask integer 6 = read and write permissions
  3200. --- Mask integer 14 = read and write and create permissions
  3201. ---------------------------------------------------------------------
  3202. --- *** INHERITED RIGHTS FOR DIFFERENT INSTANCES AND RECIPIENTS ***
  3203. --- INSTANCE RECIPIENT PERMISSION(S) (COMMENT #INSTANCE)
  3204. ---------------------------------------------------------------------
  3205. --- 1 ROLE_SUPERVISOR Administer
  3206. --- 2 ROLE_SUPERVISOR None (overrides parent #1)
  3207. --- marissa Read
  3208. --- 3 ROLE_SUPERVISOR Administer (from parent #1)
  3209. --- scott Read, Write, Create
  3210. --- 4 ROLE_SUPERVISOR Administer (from parent #1)
  3211. --- 5 ROLE_SUPERVISOR Administer (from parent #3)
  3212. --- scott Read, Write, Create (from parent #3)
  3213. --- 6 ROLE_SUPERVISOR Administer (from parent #3)
  3214. --- scott Administer (overrides parent #3)</programlisting></para>
  3215. <para>So the above explains how a domain object instance has its
  3216. <literal>AclObjectIdentity</literal> discovered, and the
  3217. <literal>BasicAclDao</literal> will be polled successively until an
  3218. array of inherited permissions is constructed for the domain object
  3219. instance. The final step is to determine the
  3220. <literal>BasicAclEntry</literal>[]s that are actually applicable to a
  3221. given <literal>Authentication</literal> object.</para>
  3222. <para>As you would recall, the <literal>AclManager</literal> (and all
  3223. delegates, up to and including <literal>BasicAclProvider</literal>)
  3224. provides a method which returns only those
  3225. <literal>BasicAclEntry</literal>[]s applying to a passed
  3226. <literal>Authentication</literal> object.
  3227. <literal>BasicAclProvider</literal> delivers this functionality by
  3228. delegating the filtering operation to an
  3229. <literal>EffectiveAclsResolver</literal> implementation. The default
  3230. implementation,
  3231. <literal>GrantedAuthorityEffectiveAclsResolver</literal>, will iterate
  3232. through the <literal>BasicAclEntry</literal>[]s and include only those
  3233. where the <literal>recipient</literal> is equal to either the
  3234. <literal>Authentication</literal>'s <literal>principal</literal> or
  3235. any of the <literal>Authentication</literal>'s
  3236. <literal>GrantedAuthority</literal>[]s. Please refer to the JavaDocs
  3237. for more information.</para>
  3238. </sect2>
  3239. <sect2 id="acls-conclusion">
  3240. <title>Conclusion</title>
  3241. <para>Acegi Security's instance-specific ACL packages shield you from
  3242. much of the complexity of developing your own ACL approach. The
  3243. interfaces and classes detailed above provide a scalable, customisable
  3244. ACL solution that is decoupled from your application code. Whilst the
  3245. reference documentation may suggest complexity, the basic
  3246. implementation is able to support most typical applications
  3247. out-of-the-box.</para>
  3248. </sect2>
  3249. </sect1>
  3250. <sect1 id="security-filters">
  3251. <title>Filters</title>
  3252. <sect2 id="security-filters-overview">
  3253. <title>Overview</title>
  3254. <para>The Acegi Security System for Spring uses filters extensively.
  3255. Each filter is covered in detail in a respective section of this
  3256. document. This section includes information that applies to all
  3257. filters.</para>
  3258. </sect2>
  3259. <sect2 id="security-filters-filtertobeanproxy">
  3260. <title>FilterToBeanProxy</title>
  3261. <para>Most filters are configured using the
  3262. <literal>FilterToBeanProxy</literal>. An example configuration from
  3263. <literal>web.xml</literal> follows:</para>
  3264. <para><programlisting>&lt;filter&gt;
  3265. &lt;filter-name&gt;Acegi HTTP Request Security Filter&lt;/filter-name&gt;
  3266. &lt;filter-class&gt;net.sf.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  3267. &lt;init-param&gt;
  3268. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  3269. &lt;param-value&gt;net.sf.acegisecurity.ClassThatImplementsFilter&lt;/param-value&gt;
  3270. &lt;/init-param&gt;
  3271. &lt;/filter&gt;</programlisting></para>
  3272. <para>Notice that the filter in <literal>web.xml</literal> is actually
  3273. a <literal>FilterToBeanProxy</literal>, and not the filter that will
  3274. actually implements the logic of the filter. What
  3275. <literal>FilterToBeanProxy</literal> does is delegate the
  3276. <literal>Filter</literal>'s methods through to a bean which is
  3277. obtained from the Spring application context. This enables the bean to
  3278. benefit from the Spring application context lifecycle support and
  3279. configuration flexibility. The bean must implement
  3280. <literal>javax.servlet.Filter</literal>.</para>
  3281. <para>The <literal>FilterToBeanProxy</literal> only requires a single
  3282. initialization parameter, <literal>targetClass</literal> or
  3283. <literal>targetBean</literal>. The <literal>targetClass</literal>
  3284. parameter locates the first object in the application context of the
  3285. specified class, whilst <literal>targetBean</literal> locates the
  3286. object by bean name. Like standard Spring web applications, the
  3287. <literal>FilterToBeanProxy</literal> accesses the application context
  3288. via<literal>
  3289. WebApplicationContextUtils.getWebApplicationContext(ServletContext)</literal>,
  3290. so you should configure a <literal>ContextLoaderListener</literal> in
  3291. <literal>web.xml</literal>.</para>
  3292. </sect2>
  3293. <sect2 id="security-filters-order">
  3294. <title>Filter Ordering</title>
  3295. <para>The order that filters are defined in <literal>web.xml</literal>
  3296. is important.</para>
  3297. <para>Irrespective of which filters you are actually using, the order
  3298. of the <literal>&lt;filter-mapping&gt;</literal>s should be as
  3299. follows:</para>
  3300. <orderedlist>
  3301. <listitem>
  3302. <para>Acegi Channel Processing Filter
  3303. (<literal>ChannelProcessingFilter</literal>)</para>
  3304. </listitem>
  3305. <listitem>
  3306. <para>Acegi Authentication Processing Filter
  3307. (<literal>AuthenticationProcessingFilter</literal>)</para>
  3308. </listitem>
  3309. <listitem>
  3310. <para>Acegi CAS Processing Filter
  3311. (<literal>CasProcessingFilter</literal>)</para>
  3312. </listitem>
  3313. <listitem>
  3314. <para>Acegi HTTP BASIC Authorization Filter
  3315. (<literal>BasicProcessingFilter</literal>)</para>
  3316. </listitem>
  3317. <listitem>
  3318. <para>Acegi Security System for Spring Auto Integration Filter
  3319. (<literal>AutoIntegrationFilter</literal>)</para>
  3320. </listitem>
  3321. <listitem>
  3322. <para>Acegi HTTP Request Security Filter
  3323. (<literal>SecurityEnforcementFilter</literal>)</para>
  3324. </listitem>
  3325. </orderedlist>
  3326. <para>All of the above filters use
  3327. <literal>FilterToBeanProxy</literal>, which is discussed in the
  3328. previous section.</para>
  3329. <para>If you're using SiteMesh, ensure the Acegi Security filters
  3330. execute before the SiteMesh filters are called. This enables the
  3331. <literal>ContextHolder</literal> to be populated in time for use by
  3332. SiteMesh decorators.</para>
  3333. </sect2>
  3334. </sect1>
  3335. <sect1 id="security-sample">
  3336. <title>Contacts Sample Application</title>
  3337. <para>Included with the Acegi Security System for Spring is a very
  3338. simple application that can demonstrate the basic security facilities
  3339. provided by the system (and confirm your Container Adapter is properly
  3340. configured if you're using one).</para>
  3341. <para>The Contacts sample application includes two deployable versions:
  3342. <literal>contacts.war</literal> is configured with the HTTP Session
  3343. Authentication approach, and does not use Container Adapters. The
  3344. <literal>contacts-container-adapter.war</literal> is configured to use a
  3345. Container Adapter. If you're just wanting to see how the sample
  3346. application works, please use <literal>contacts.war</literal> as it does
  3347. not require special configuration of your container.</para>
  3348. <para>If you are going to use the
  3349. <literal>contacts-container-adapter.war</literal> version, first
  3350. configure your container as described in the Container Adapters section
  3351. of this chapter. Do not modify <literal>acegisecurity.xml</literal>. It
  3352. contains a very basic in-memory authentication configuration that is
  3353. compatible with the sample application.</para>
  3354. <para>To deploy, simply copy the relevant
  3355. <literal>contacts.war</literal> or
  3356. <literal>contacts-container-adapter.war</literal> file from the Acegi
  3357. Security System for Spring distribution into your container’s
  3358. <literal>webapps</literal> directory.</para>
  3359. <para>After starting your container, check the application can load.
  3360. Visit <literal>http://localhost:8080/contacts</literal> (or whichever
  3361. URL is appropriate for your web container and the WAR you deployed). A
  3362. random contact should be displayed. Click "Refresh" several times and
  3363. you will see different contacts. The business method that provides this
  3364. random contact is not secured.</para>
  3365. <para>Next, click "Debug". You will be prompted to authenticate, and a
  3366. series of usernames and passwords are suggested on that page. Simply
  3367. authenticate with any of these and view the resulting page. It should
  3368. contain a success message similar to the following:</para>
  3369. <blockquote>
  3370. <para>Context on ContextHolder is of type:
  3371. net.sf.acegisecurity.context.SecureContextImpl</para>
  3372. <para>The Context implements SecureContext.</para>
  3373. <para>Authentication object is of type:
  3374. net.sf.acegisecurity.adapters.PrincipalAcegiUserToken</para>
  3375. <para>Authentication object as a String:
  3376. net.sf.acegisecurity.adapters.PrincipalAcegiUserToken@e9a7c2:
  3377. Username: marissa; Password: [PROTECTED]; Authenticated: true; Granted
  3378. Authorities: ROLE_TELLER, ROLE_SUPERVISOR</para>
  3379. <para>Authentication object holds the following granted
  3380. authorities:</para>
  3381. <para>ROLE_TELLER (getAuthority(): ROLE_TELLER)</para>
  3382. <para>ROLE_SUPERVISOR (getAuthority(): ROLE_SUPERVISOR)</para>
  3383. <para>SUCCESS! Your [container adapter|web filter] appears to be
  3384. properly configured!</para>
  3385. </blockquote>
  3386. <para>If you receive a different message, and deployed
  3387. <literal>contacts-container-adapter.war</literal>, check you have
  3388. properly configured your Container Adapter. Refer to the instructions
  3389. provided above.</para>
  3390. <para>Once you successfully receive the above message, return to the
  3391. sample application's home page and click "Manage". You can then try out
  3392. the application. Notice that only the contacts belonging to the
  3393. currently logged on user are displayed, and only users with
  3394. <literal>ROLE_SUPERVISOR</literal> are granted access to delete their
  3395. contacts. Behind the scenes, the
  3396. <literal>MethodSecurityInterceptor</literal> is securing the business
  3397. objects. If you're using <literal>contacts.war</literal>, the
  3398. <literal>FilterSecurityInterceptor</literal> is also securing the HTTP
  3399. requests. If using <literal>contacts.war</literal>, be sure to try
  3400. visiting <literal>http://localhost:8080/contacts/secure/super</literal>,
  3401. which will demonstrate access being denied by the
  3402. <literal>SecurityEnforcementFilter</literal>.</para>
  3403. <para>The Contacts sample application also include a
  3404. <literal>client</literal> directory. Inside you will find a small
  3405. application that queries the backend business objects using the Hessian
  3406. and Burlap protocols. This demonstrates how to use the Acegi Security
  3407. System for Spring for authentication with Spring remoting protocols. To
  3408. try this client, ensure your servlet container is still running the
  3409. Contacts sample application, and then execute <literal>client marissa
  3410. marissa koala</literal>. The command-line parameters respectively
  3411. represent the owner of the contacts to extract, the username to use, and
  3412. the password to use. Note that you may need to edit
  3413. <literal>client.properties</literal> to use a different target URL. To
  3414. see that security does indeed work, try running <literal>client scott
  3415. marissa koala</literal>, which will try to obtain
  3416. <literal>scott</literal>'s contacts when authenticating as
  3417. <literal>marissa</literal>. To see it work properly, use <literal>client
  3418. scott scott wombat</literal>.</para>
  3419. <para>Please note the sample application's <literal>client</literal>
  3420. does not currently support CAS. You can still give it a try, though, if
  3421. you're ambitious: try <literal>client scott _cas_stateless_
  3422. YOUR-SERVICE-TICKET-ID-FOR-SCOTT</literal>.</para>
  3423. </sect1>
  3424. <sect1 id="security-become-involved">
  3425. <title>Become Involved</title>
  3426. <para>We welcome you to become involved in the Acegi Security System for
  3427. Spring project. There are many ways of contributing, including reading
  3428. the mailing list and responding to questions from other people, writing
  3429. new code, improving existing code, assisting with documentation, or
  3430. simply making suggestions.</para>
  3431. <para>SourceForge provides CVS services for the project, allowing
  3432. anybody to access the latest code. If you wish to contribute new code,
  3433. please observe the following requirements. These exist to maintain the
  3434. quality and consistency of the project:</para>
  3435. <itemizedlist>
  3436. <listitem>
  3437. <para>Run the Ant <literal>format</literal> task (or use a suitable
  3438. IDE plug-in) to convert your code into the project's consistent
  3439. style</para>
  3440. </listitem>
  3441. <listitem>
  3442. <para>Ensure your code does not break any unit tests (run the Ant
  3443. <literal>tests</literal> target)</para>
  3444. </listitem>
  3445. <listitem>
  3446. <para>Please use the container integration test system to test your
  3447. code in the project's officially supported containers</para>
  3448. </listitem>
  3449. <listitem>
  3450. <para>When writing a new container adapter, expand the container
  3451. integration test system to properly test it</para>
  3452. </listitem>
  3453. <listitem>
  3454. <para>If you have added new code, please provide suitable unit tests
  3455. (use <literal>ant clover.html</literal> to view coverage)</para>
  3456. </listitem>
  3457. <listitem>
  3458. <para>Join the acegisecurity-developer and acegisecurity-cvs mailing
  3459. lists so you're in the loop</para>
  3460. </listitem>
  3461. <listitem>
  3462. <para>Use CamelCase</para>
  3463. </listitem>
  3464. <listitem>
  3465. <para>Add a CVS <literal>$Id: index.xml,v 1.3 2004/04/02 21:12:25
  3466. fbos Exp $</literal> tag to the JavaDocs for any new class you
  3467. create</para>
  3468. </listitem>
  3469. </itemizedlist>
  3470. <para>Mentioned above is our container integration test system, which
  3471. aims to test the Acegi Security System for Spring container adapters
  3472. with current, production versions of each container. Some containers
  3473. might not be supported due to difficulties with starting or stopping the
  3474. container within an Ant target. You will need to download the container
  3475. release files as specified in the integration test
  3476. <literal>readme.txt</literal> file. These files are intentionally
  3477. excluded from CVS due to their large size.</para>
  3478. </sect1>
  3479. <sect1 id="security-further">
  3480. <title>Further Information</title>
  3481. <para>Questions and comments on the Acegi Security System for Spring are
  3482. welcome. Please use the Spring Community Forum web site at
  3483. <literal>http://forum.springframework.org</literal>. You're also welcome
  3484. to join the acegisecurity-developer mailing list. Our project home page
  3485. (where you can obtain the latest release of the project and access to
  3486. CVS, mailing lists, forums etc) is at
  3487. <literal>http://acegisecurity.sourceforge.net</literal>.</para>
  3488. </sect1>
  3489. </chapter>
  3490. </book>