index.xml 223 KB

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