index.xml 221 KB

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