index.xml 223 KB

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