acegi.xml 282 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402
  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>1.0.0 RC 2</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>We are now at release 0.9.0, and a lot of effort has been made
  92. to implement all non-backward compatible changes either in or before
  93. this release. Some minor improvements are currently intended to the
  94. 1.0.0 release, although they will in no way modify the project's
  95. central interfaces or classes. Users of Acegi Security System for
  96. Spring should therefore be comfortable depending on the current
  97. version of the project in their applications. Please note that we will
  98. be changing the package name prefix in the 1.0.0 release, but this
  99. should be a simple "find and replace" type operation in your
  100. code.</para>
  101. </sect2>
  102. </sect1>
  103. <sect1 id="security-high-level-design">
  104. <title>High Level Design</title>
  105. <sect2 id="security-high-level-design-key-components">
  106. <title>Key Components</title>
  107. <para>Most enterprise applications have four basic security
  108. requirements. First, they need to be able to authenticate a principal.
  109. Second, they need to be able to secure web requests. Third, enterprise
  110. applications need to be able to secure services layer methods.
  111. Finally, quite often an enterprise application will need to secure
  112. domain object instances. Acegi Security provides a comprehensive
  113. framework for achieving all of these four common enterprise
  114. application security requirements.</para>
  115. <para>The Acegi Security System for Spring essentially comprises eight
  116. key functional parts:</para>
  117. <itemizedlist spacing="compact">
  118. <listitem>
  119. <para>An <literal>Authentication</literal> object which holds the
  120. principal, credentials and the authorities granted to the
  121. principal. The object can also store additional information
  122. associated with an authentication request, such as the source
  123. TCP/IP address.</para>
  124. </listitem>
  125. <listitem>
  126. <para>A <literal>SecurityContextHolder</literal> which holds the
  127. <literal>Authentication</literal> object in a
  128. <literal>ThreadLocal</literal>-bound object.</para>
  129. </listitem>
  130. <listitem>
  131. <para>An <literal>AuthenticationManager</literal> to authenticate
  132. the <literal>Authentication</literal> object presented via the
  133. <literal>ContextHolder</literal>.</para>
  134. </listitem>
  135. <listitem>
  136. <para>An <literal>AccessDecisionManager</literal> to authorize a
  137. given operation.</para>
  138. </listitem>
  139. <listitem>
  140. <para>A <literal>RunAsManager</literal> to optionally replace the
  141. <literal>Authentication</literal> object whilst a given operation
  142. is being executed.</para>
  143. </listitem>
  144. <listitem>
  145. <para>A "secure object" interceptor, which coordinates the
  146. authentication, authorization, run-as replacement, after
  147. invocation handling and execution of a given operation.</para>
  148. </listitem>
  149. <listitem>
  150. <para>An <literal>AfterInvocationManager</literal> which can
  151. modify an <literal>Object</literal> returned from a "secure
  152. object" invocation, such as removing <literal>Collection</literal>
  153. elements a principal does not have authority to access.</para>
  154. </listitem>
  155. <listitem>
  156. <para>An acess control list (ACL) management package, which can be
  157. used to obtain the ACLs applicable for domain object
  158. instances.</para>
  159. </listitem>
  160. </itemizedlist>
  161. <para>A "secure object" interceptor executes most of the Acegi
  162. Security key classes and in doing so delivers the framework's major
  163. features. Given its importance, Figure 1 shows the key relationships
  164. and concrete implementations of
  165. <literal>AbstractSecurityInterceptor</literal>.</para>
  166. <para><mediaobject>
  167. <imageobject role="html">
  168. <imagedata align="center"
  169. fileref="images/SecurityInterception.gif"
  170. format="GIF" />
  171. </imageobject>
  172. <caption>
  173. <para>Figure 1: The key "secure object" model</para>
  174. </caption>
  175. </mediaobject></para>
  176. <para>Each "secure object" interceptor (hereinafter called a "security
  177. interceptor") works with a particular type of "secure object". So,
  178. what is a secure object? Secure objects refer to any type of object
  179. that can have security applied to it. A secure object must provide
  180. some form of callback, so that the security interceptor can
  181. transparently do its work as required, and callback the object when it
  182. is time for it to proceed with the requested operation. If secure
  183. objects cannot provide a native callback approach, a wrapper needs to
  184. be written so this becomes possible.</para>
  185. <para>Each secure object has its own package under
  186. <literal>org.acegisecurity.intercept</literal>. Every other package in
  187. the security system is secure object independent, in that it can
  188. support any type of secure object presented.</para>
  189. <para>Only developers contemplating an entirely new way of
  190. intercepting and authorizing requests would need to use secure objects
  191. directly. For example, it would be possible to build a new secure
  192. object to secure calls to a messaging system that does not use
  193. <literal>MethodInvocation</literal>s. Most Spring applications will
  194. simply use the three currently supported secure object types (AOP
  195. Alliance <literal>MethodInvocation</literal>, AspectJ
  196. <literal>JoinPoint</literal> and web request
  197. <literal>FilterInterceptor</literal>) with complete
  198. transparency.</para>
  199. <para>Each of the eight key parts of Acegi Security are discussed in
  200. detail throughout this document.</para>
  201. </sect2>
  202. <sect2 id="security-high-level-design-supported-secure-objects">
  203. <title>Supported Secure Objects</title>
  204. <para>As shown in the base of Figure 1, the Acegi Security System for
  205. Spring currently supports three secure objects.</para>
  206. <para>The first handles an AOP Alliance
  207. <literal>MethodInvocation</literal>. This is the secure object type
  208. used to protect Spring beans. Developers will generally use this
  209. secure object type to secure their business objects. To make a
  210. standard Spring-hosted bean available as a
  211. <literal>MethodInvocation</literal>, the bean is simply published
  212. through a <literal>ProxyFactoryBean</literal> or
  213. <literal>BeanNameAutoProxyCreator</literal> or
  214. <literal>DefaultAdvisorAutoProxyCreator</literal>. Most Spring
  215. developers would already be familiar with these due to their use in
  216. transactions and other areas of Spring.</para>
  217. <para>The second type is an AspectJ <literal>JoinPoint</literal>.
  218. AspectJ has a particular use in securing domain object instances, as
  219. these are most often managed outside the Spring bean container. By
  220. using AspectJ, standard constructs such as <literal>new
  221. Person();</literal> can be used and full security will be applied to
  222. them by Acegi Security. The
  223. <literal>AspectJSecurityInterceptor</literal> is still managed by
  224. Spring, which creates the aspect singleton and wires it with the
  225. appropriate authentication managers, access decision managers and so
  226. on.</para>
  227. <para>The third type is a <literal>FilterInvocation</literal>. This is
  228. an object included with the Acegi Security System for Spring. It is
  229. created by an included filter and simply wraps the HTTP
  230. <literal>ServletRequest</literal>, <literal>ServletResponse</literal>
  231. and <literal>FilterChain</literal>. The
  232. <literal>FilterInvocation</literal> enables HTTP resources to be
  233. secured. Developers do not usually need to understand the mechanics of
  234. how this works, because they just add the filters to their
  235. <literal>web.xml</literal> and let the security system do its
  236. work.</para>
  237. </sect2>
  238. <sect2 id="security-high-level-design-configuration-attributes">
  239. <title>Configuration Attributes</title>
  240. <para>Every secure object can represent an infinite number of
  241. individual requests. For example, a
  242. <literal>MethodInvocation</literal> can represent the invocation of
  243. any method with any arguments, whilst a
  244. <literal>FilterInvocation</literal> can represent any HTTP URL.</para>
  245. <para>The Acegi Security System for Spring needs to record the
  246. configuration that applies to each of these possible requests. The
  247. security configuration of a request to
  248. <literal>BankManager.getBalance(int accountNumber)</literal> needs to
  249. be very different from the security configuration of a request to
  250. <literal>BankManager.approveLoan(int applicationNumber)</literal>.
  251. Similarly, the security configuration of a request to
  252. <literal>http://some.bank.com/index.htm</literal> needs to be very
  253. different from the security configuration of
  254. <literal>http://some.bank.com/manage/timesheet.jsp</literal>.</para>
  255. <para>To store the various security configurations associated with
  256. different requests, a configuration attribute is used. At an
  257. implementation level a configuration attribute is represented by the
  258. <literal>ConfigAttribute</literal> interface. One concrete
  259. implementation of <literal>ConfigAttribute</literal> is provided,
  260. <literal>SecurityConfig</literal>, which simply stores a configuration
  261. attribute as a <literal>String</literal>.</para>
  262. <para>The collection of <literal>ConfigAttribute</literal>s associated
  263. with a particular request is held in a
  264. <literal>ConfigAttributeDefinition</literal>. This concrete class is
  265. simply a holder of <literal>ConfigAttribute</literal>s and does
  266. nothing special.</para>
  267. <para>When a request is received by the security interceptor, it needs
  268. to determine which configuration attributes apply. In other words, it
  269. needs to find the <literal>ConfigAttributeDefinition</literal> which
  270. applies to the request. This decision is handled by the
  271. <literal>ObjectDefinitionSource</literal> interface. The main method
  272. provided by this interface is <literal>public
  273. ConfigAttributeDefinition getAttributes(Object object)</literal>, with
  274. the <literal>Object</literal> being the secure object. Recall the
  275. secure object contains details of the request, so the
  276. <literal>ObjectDefinitionSource</literal> implementation will be able
  277. to extract the details it requires to lookup the relevant
  278. <literal>ConfigAttributeDefinition</literal>.</para>
  279. </sect2>
  280. </sect1>
  281. <sect1 id="security-request-contexts">
  282. <title>Request Contexts</title>
  283. <sect2 id="security-contexts-history">
  284. <title>Historical Approach</title>
  285. <para>Prior to release 0.9.0, Acegi Security used a
  286. <literal>ContextHolder</literal> to store a <literal>Context</literal>
  287. between sessions. A particular subclass of <literal>Context</literal>,
  288. <literal>SecureContext</literal> defined an interface used for storage
  289. of the <literal>Authentication</literal> object. The
  290. <literal>ContextHolder</literal> was a <literal>ThreadLocal</literal>.
  291. A fuller discussion of the <literal>ThreadLocal</literal> usage with
  292. Acegi Security follows in this document.
  293. <literal>ContextHolder</literal> and <literal>SecureContext</literal>
  294. was removed from 0.9.0 after discussion with other Spring developers
  295. for the sake of consistency. See for example
  296. <literal>http://article.gmane.org/gmane.comp.java.springframework.devel/8290</literal>
  297. and JIRA task SEC-77. This history is mentioned as the long period
  298. <literal>ContextHolder</literal> was used will likely mean that
  299. certain documentation you encounter concerning Acegi Security might
  300. still refer to <literal>ContextHolder</literal>. Generally you can
  301. just substitute "<literal>SecurityContextHolder</literal>" for
  302. "<literal>ContextHolder</literal>", and
  303. "<literal>SecurityContext</literal>" for
  304. "<literal>SecureContext</literal>", and you'll have the primary
  305. meaning of such documentation.</para>
  306. </sect2>
  307. <sect2 id="security-contexts-security-context">
  308. <title>SecurityContext</title>
  309. <para>The Acegi Security System for Spring uses a
  310. <literal>SecurityContextHolder</literal> to store the
  311. <literal>SecurityContext</literal>. The
  312. <literal>SecurityContext</literal> contains a single getter/setter for
  313. <literal>Authentication</literal>. All Acegi Security classes query
  314. the <literal>SecurityContextHolder</literal> for obtaining the current
  315. <literal>SecurityContext</literal> (and in turn the principal).
  316. <literal>SecurityContextHolder</literal> is a
  317. <literal>ThreadLocal</literal>, meaning it is associated with the
  318. current thread of execution.</para>
  319. </sect2>
  320. <sect2 id="security-contexts-storage">
  321. <title>Context Storage</title>
  322. <para>Central to Acegi Security's design is that the contents of the
  323. <literal>SecurityContextHolder</literal> (which is simply a
  324. <literal>SecurityContext</literal> implementation) can be stored
  325. between web requests. This is so that a successfully authenticated
  326. principal can be identified on subsequent requests through the
  327. <literal>Authentication</literal> stored inside the
  328. <literal>SecurityContext</literal> obtained from the
  329. <literal>SecurityContextHolder</literal>. The
  330. <literal>HttpSessionContextIntegrationFilter</literal> exists to
  331. automatically copy the contents of a well-defined
  332. <literal>HttpSession</literal> attribute into the
  333. <literal>SecurityContextHolder</literal>, then at the end of each
  334. request, copy the <literal>SecurityContextHolder</literal> contents
  335. back into the <literal>HttpSession</literal> ready for next
  336. request.</para>
  337. <para>It is essential - and an extremely common error of end users -
  338. that <literal>HttpSessionContextIntegrationFilter</literal> appears
  339. before any other Acegi Security filter. Acegi Security filters expect
  340. to be able to modify the <literal>SecurityContextHolder</literal>
  341. contents as they see fit, and something else (namely
  342. <literal>HttpSessionContextIntegrationFilter</literal>) will store
  343. those between requests if necessary. This is why
  344. <literal>HttpSessionContextIntegrationFilter</literal> must be the
  345. first filter used.</para>
  346. <para>You can define a custom <literal>SecurityContext</literal>
  347. implementation be used in your application by setting the
  348. <literal>context</literal> property on the
  349. <literal>HttpSessionContextIntegrationFilter</literal> bean.</para>
  350. </sect2>
  351. <sect2 id="security-localization">
  352. <title>Localization</title>
  353. <para>From 1.0.0, Acegi Security supports localization of exception
  354. messages that end users are likely to see. Such exceptions include
  355. authentication failures and access being denied (authorization
  356. failures). Exceptions and logging that is focused on developers or
  357. system deployers (including incorrect attributes, interface contract
  358. violations, using incorrect constructors, startup time validation,
  359. debug-level logging) etc are not localized and instead are hard-coded
  360. in English within Acegi Security's code.</para>
  361. <para>Shipping in the <literal>acegi-security-xx.jar</literal> inside
  362. the <literal>org.acegisecurity</literal> package is a
  363. <literal>messages.properties</literal> file. This should be referred
  364. to by your <literal>ApplicationContext</literal>, as Acegi Security
  365. classes implement Spring's <literal>MessageSourceAware</literal>
  366. interface and expect the message resolver to be dependency injected at
  367. application context startup time. Usually all you need to do is
  368. register a bean inside your application context to refer to the
  369. messages. An example is shown below:</para>
  370. <para><programlisting>&lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
  371. &lt;property name="basename"&gt;&lt;value&gt;org/acegisecurity/messages&lt;/value&gt;&lt;/property&gt;
  372. &lt;/bean&gt;</programlisting></para>
  373. <para>The <literal>messages.properties</literal> is named in
  374. accordance with standard resource bundles and represents the default
  375. language supported by Acegi Securtiy messages. This default file is in
  376. English. If you do not register a message source, Acegi Security will
  377. still work correctly and fallback to hard-coded English versions of
  378. the messages.</para>
  379. <para>If you wish to customize the
  380. <literal>messages.properties</literal> file, or support other
  381. languages, you should copy the file, rename it accordingly, and
  382. register it inside the above bean definition. There are not a large
  383. number of message keys inside this file, so localization should not be
  384. considered a major initiative. If you do perform localization of this
  385. file, please consider sharing your work with the community by logging
  386. a JIRA task and attaching your appropriately-named localized version
  387. of <literal>messages.properties</literal>.</para>
  388. <para>Rounding out the discussion on localization is the Spring
  389. <literal>ThreadLocal</literal> known as
  390. <literal>org.springframework.context.i18n.LocaleContextHolder</literal>.
  391. You should set the <literal>LocaleContextHolder</literal> to represent
  392. the preferred <literal>Locale</literal> of each user. Acegi Security
  393. will attempt to locate a message from the message source using the
  394. <literal>Locale</literal> obtained from this
  395. <literal>ThreadLocal</literal>. Please refer to Spring documentation
  396. for further details on using <literal>LocaleContextHolder</literal>
  397. and the helper classes that can automatically set it for you (eg
  398. <literal>AcceptHeaderLocaleResolver</literal>,
  399. <literal>CookieLocaleResolver</literal>,
  400. <literal>FixedLocaleResolver</literal>,
  401. <literal>SessionLocaleResolver</literal> etc)</para>
  402. </sect2>
  403. </sect1>
  404. <sect1 id="security-interception">
  405. <title>Security Interception</title>
  406. <sect2 id="security-interception-all-secure-objects">
  407. <title>All Secure Objects</title>
  408. <para>As described in the High Level Design section, each secure
  409. object has its own security interceptor which is responsible for
  410. handling each request. Handling involves a number of
  411. operations:</para>
  412. <orderedlist>
  413. <listitem>
  414. <para>Store the configuration attributes that are associated with
  415. each secure request.</para>
  416. </listitem>
  417. <listitem>
  418. <para>Extract the <literal>ConfigAttributeDefinition</literal>
  419. that applies to the request from the relevant
  420. <literal>ObjectDefinitionSource</literal>.</para>
  421. </listitem>
  422. <listitem>
  423. <para>Obtain the <literal>Authentication</literal> object from the
  424. <literal>SecurityContext</literal>, which is held in the
  425. <literal>SecurityContextHolder</literal>.</para>
  426. </listitem>
  427. <listitem>
  428. <para>Pass the <literal>Authentication</literal> object to the
  429. <literal>AuthenticationManager</literal>, update the
  430. <literal>SecurityContextHolder</literal> with the response.</para>
  431. </listitem>
  432. <listitem>
  433. <para>Pass the <literal>Authentication</literal> object, the
  434. <literal>ConfigAttributeDefinition</literal>, and the secure
  435. object to the <literal>AccessDecisionManager</literal>.</para>
  436. </listitem>
  437. <listitem>
  438. <para>Pass the <literal>Authentication</literal> object, the
  439. <literal>ConfigAttributeDefinition</literal>, and the secure
  440. object to the <literal>RunAsManager</literal>.</para>
  441. </listitem>
  442. <listitem>
  443. <para>If the <literal>RunAsManager</literal> returns a new
  444. <literal>Authentication</literal> object, update the
  445. <literal>SecurityContextHolder</literal> with it.</para>
  446. </listitem>
  447. <listitem>
  448. <para>Proceed with the request execution of the secure
  449. object.</para>
  450. </listitem>
  451. <listitem>
  452. <para>If the <literal>RunAsManager</literal> earlier returned a
  453. new <literal>Authentication</literal> object, update the
  454. <literal>SecurityContextHolder</literal> with the
  455. <literal>Authentication</literal> object that was previously
  456. returned by the <literal>AuthenticationManager</literal>.</para>
  457. </listitem>
  458. <listitem>
  459. <para>If an <literal>AfterInvocationManager</literal> is defined,
  460. pass it the result of the secure object execution so that it may
  461. throw an <literal>AccessDeniedException</literal> or mutate the
  462. returned object if required.</para>
  463. </listitem>
  464. <listitem>
  465. <para>Return any result received from the
  466. <literal>AfterInvocationManager</literal>, or if no
  467. <literal>AfterInvocationManager</literal> is defined, simply
  468. return the result provided by the secure object execution.</para>
  469. </listitem>
  470. </orderedlist>
  471. <para>Whilst this may seem quite involved, don't worry. Developers
  472. interact with the security process by simply implementing basic
  473. interfaces (such as <literal>AccessDecisionManager</literal>), which
  474. are fully discussed below.</para>
  475. <para>The <literal>AbstractSecurityInterceptor</literal> handles the
  476. majority of the flow listed above. As shown in Figure 1, each secure
  477. object has its own security interceptor which subclasses
  478. <literal>AbstractSecurityInterceptor</literal>. Each of these secure
  479. object-specific security interceptors are discussed below.</para>
  480. </sect2>
  481. <sect2 id="security-interception-aopalliance">
  482. <title>AOP Alliance (MethodInvocation) Security Interceptor</title>
  483. <para>To secure <literal>MethodInvocation</literal>s, developers
  484. simply add a properly configured
  485. <literal>MethodSecurityInterceptor</literal> into the application
  486. context. Next the beans requiring security are chained into the
  487. interceptor. This chaining is accomplished using Spring’s
  488. <literal>ProxyFactoryBean</literal> or
  489. <literal>BeanNameAutoProxyCreator</literal>, as commonly used by many
  490. other parts of Spring (refer to the sample application for examples).
  491. Alternatively, Acegi Security provides a
  492. <literal>MethodDefinitionSourceAdvisor</literal> which may be used
  493. with Spring's <literal>DefaultAdvisorAutoProxyCreator</literal> to
  494. automatically chain the security interceptor in front of any beans
  495. defined against the <literal>MethodSecurityInterceptor</literal>. The
  496. <literal>MethodSecurityInterceptor</literal> itself is configured as
  497. follows:</para>
  498. <para><programlisting>&lt;bean id="bankManagerSecurity" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"&gt;
  499. &lt;property name="validateConfigAttributes"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
  500. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  501. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  502. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  503. &lt;property name="afterInvocationManager"&gt;&lt;ref bean="afterInvocationManager"/&gt;&lt;/property&gt;
  504. &lt;property name="objectDefinitionSource"&gt;
  505. &lt;value&gt;
  506. org.acegisecurity.context.BankManager.delete*=ROLE_SUPERVISOR,RUN_AS_SERVER
  507. org.acegisecurity.context.BankManager.getBalance=ROLE_TELLER,ROLE_SUPERVISOR,BANKSECURITY_CUSTOMER,RUN_AS_SERVER
  508. &lt;/value&gt;
  509. &lt;/property&gt;
  510. &lt;/bean&gt;</programlisting></para>
  511. <para>As shown above, the <literal>MethodSecurityInterceptor</literal>
  512. is configured with a reference to an
  513. <literal>AuthenticationManager</literal>,
  514. <literal>AccessDecisionManager</literal> and
  515. <literal>RunAsManager</literal>, which are each discussed in separate
  516. sections below. In this case we've also defined an
  517. <literal>AfterInvocationManager</literal>, although this is entirely
  518. optional. The <literal>MethodSecurityInterceptor</literal> is also
  519. configured with configuration attributes that apply to different
  520. method signatures. A full discussion of configuration attributes is
  521. provided in the High Level Design section of this document.</para>
  522. <para>The <literal>MethodSecurityInterceptor</literal> can be
  523. configured with configuration attributes in three ways. The first is
  524. via a property editor and the application context, which is shown
  525. above. The second is via defining the configuration attributes in your
  526. source code using Jakarta Commons Attributes or Java 5 Annotations.
  527. The third is via writing your own
  528. <literal>ObjectDefinitionSource</literal>, although this is beyond the
  529. scope of this document. Irrespective of the approach used, the
  530. <literal>ObjectDefinitionSource</literal> is responsible for returning
  531. a <literal>ConfigAttributeDefinition</literal> object that contains
  532. all of the configuration attributes associated with a single secure
  533. method.</para>
  534. <para>It should be noted that the
  535. <literal>MethodSecurityInterceptor.setObjectDefinitionSource()</literal>
  536. method actually expects an instance of
  537. <literal>MethodDefinitionSource</literal>. This is a marker interface
  538. which subclasses <literal>ObjectDefinitionSource</literal>. It simply
  539. denotes the <literal>ObjectDefinitionSource</literal> understands
  540. <literal>MethodInvocation</literal>s. In the interests of simplicity
  541. we'll continue to refer to the
  542. <literal>MethodDefinitionSource</literal> as an
  543. <literal>ObjectDefinitionSource</literal>, as the distinction is of
  544. little relevance to most users of the
  545. <literal>MethodSecurityInterceptor</literal>.</para>
  546. <para>If using the application context property editor approach (as
  547. shown above), commas are used to delimit the different configuration
  548. attributes that apply to a given method pattern. Each configuration
  549. attribute is assigned into its own <literal>SecurityConfig</literal>
  550. object. The <literal>SecurityConfig</literal> object is discussed in
  551. the High Level Design section.</para>
  552. <para>If you are using the Jakarta Commons Attributes approach, your
  553. bean context will be configured differently:</para>
  554. <para><programlisting>&lt;bean id="attributes" class="org.springframework.metadata.commons.CommonsAttributes"/&gt;
  555. &lt;bean id="objectDefinitionSource" class="org.acegisecurity.intercept.method.MethodDefinitionAttributes"&gt;
  556. &lt;property name="attributes"&gt;&lt;ref local="attributes"/&gt;&lt;/property&gt;
  557. &lt;/bean&gt;
  558. &lt;bean id="bankManagerSecurity" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"&gt;
  559. &lt;property name="validateConfigAttributes"&gt;&lt;value&gt;false&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="objectDefinitionSource"&gt;&lt;ref bean="objectDefinitionSource"/&gt;&lt;/property&gt;
  564. &lt;/bean&gt;</programlisting></para>
  565. <para>In addition, your source code will contain Jakarta Commons
  566. Attributes tags that refer to a concrete implementation of
  567. <literal>ConfigAttribute</literal>. The following example uses the
  568. <literal>SecurityConfig</literal> implementation to represent the
  569. configuration attributes, and results in the same security
  570. configuration as provided by the property editor approach
  571. above:</para>
  572. <para><programlisting>public interface BankManager {
  573. /**
  574. * @@SecurityConfig("ROLE_SUPERVISOR")
  575. * @@SecurityConfig("RUN_AS_SERVER")
  576. */
  577. public void deleteSomething(int id);
  578. /**
  579. * @@SecurityConfig("ROLE_SUPERVISOR")
  580. * @@SecurityConfig("RUN_AS_SERVER")
  581. */
  582. public void deleteAnother(int id);
  583. /**
  584. * @@SecurityConfig("ROLE_TELLER")
  585. * @@SecurityConfig("ROLE_SUPERVISOR")
  586. * @@SecurityConfig("BANKSECURITY_CUSTOMER")
  587. * @@SecurityConfig("RUN_AS_SERVER")
  588. */
  589. public float getBalance(int id);
  590. }</programlisting></para>
  591. <para>If you are using the Spring Security Java 5 Annotations
  592. approach, your bean context will be configured as follows:</para>
  593. <para><programlisting>&lt;bean id="attributes" class="org.acegisecurity.annotation.SecurityAnnotationAttributes"/&gt;
  594. &lt;bean id="objectDefinitionSource" class="org.acegisecurity.intercept.method.MethodDefinitionAttributes"&gt;
  595. &lt;property name="attributes"&gt;&lt;ref local="attributes"/&gt;&lt;/property&gt;
  596. &lt;/bean&gt;
  597. &lt;bean id="bankManagerSecurity" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor"&gt;
  598. &lt;property name="validateConfigAttributes"&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
  599. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  600. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  601. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  602. &lt;property name="objectDefinitionSource"&gt;&lt;ref bean="objectDefinitionSource"/&gt;&lt;/property&gt;
  603. &lt;/bean&gt;</programlisting></para>
  604. <para>In addition, your source code will contain the Acegi Java 5
  605. Security Annotations that represent the
  606. <literal>ConfigAttribute</literal>. The following example uses the
  607. <literal>@Secured</literal> annotations to represent the configuration
  608. attributes, and results in the same security configuration as provided
  609. by the property editor approach:</para>
  610. <para><programlisting>import org.acegisecurity.annotation.Secured;
  611. public interface BankManager {
  612. /**
  613. * Delete something
  614. */
  615. @Secured({"ROLE_SUPERVISOR","RUN_AS_SERVER" })
  616. public void deleteSomething(int id);
  617. /**
  618. * Delete another
  619. */
  620. @Secured({"ROLE_SUPERVISOR","RUN_AS_SERVER" })
  621. public void deleteAnother(int id);
  622. /**
  623. * Get balance
  624. */
  625. @Secured({"ROLE_TELLER","ROLE_SUPERVISOR","BANKSECURITY_CUSTOMER","RUN_AS_SERVER" })
  626. public float getBalance(int id);
  627. }</programlisting></para>
  628. <para>You might have noticed the
  629. <literal>validateConfigAttributes</literal> property in the above
  630. <literal>MethodSecurityInterceptor</literal> examples. When set to
  631. <literal>true</literal> (the default), at startup time the
  632. <literal>MethodSecurityInterceptor</literal> will evaluate if the
  633. provided configuration attributes are valid. It does this by checking
  634. each configuration attribute can be processed by either the
  635. <literal>AccessDecisionManager</literal> or the
  636. <literal>RunAsManager</literal>. If neither of these can process a
  637. given configuration attribute, an exception is thrown. If using the
  638. Jakarta Commons Attributes method of configuration, you should set
  639. <literal>validateConfigAttributes</literal> to
  640. <literal>false</literal>.</para>
  641. <para>Please note that when using
  642. <literal>BeanNameAutoProxyCreator</literal> to create the required
  643. proxy for security, the configuration must contain the property
  644. <literal>proxyTargetClass</literal> set to <literal>true</literal>.
  645. Otherwise, the method passed to
  646. <literal>MethodSecurityInterceptor.invoke</literal> is the proxy's
  647. caller, not the proxy's target. Note that this introduces a
  648. requirement on CGLIB. See an example of using
  649. <literal>BeanNameAutoProxyCreator</literal> below:</para>
  650. <para><programlisting>&lt;bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;
  651. &lt;property name="interceptorNames"&gt;
  652. &lt;list&gt;&lt;value&gt;methodSecurityInterceptor&lt;/value&gt;&lt;/list&gt;
  653. &lt;/property&gt;
  654. &lt;property name="beanNames"&gt;
  655. &lt;list&gt;&lt;value&gt;targetObjectName&lt;/value&gt;&lt;/list&gt;
  656. &lt;/property&gt;
  657. &lt;property name="proxyTargetClass" value="true"/&gt;
  658. &lt;/bean&gt; </programlisting></para>
  659. </sect2>
  660. <sect2 id="security-interception-aspectj">
  661. <title>AspectJ (JoinPoint) Security Interceptor</title>
  662. <para>The AspectJ security interceptor is very similar to the AOP
  663. Alliance security interceptor discussed in the previous section.
  664. Indeed we will only discuss the differences in this section.</para>
  665. <para>The AspectJ interceptor is named
  666. <literal>AspectJSecurityInterceptor</literal>. Unlike the AOP Alliance
  667. security interceptor, which relies on the Spring application context
  668. to weave in the security interceptor via proxying, the
  669. <literal>AspectJSecurityInterceptor</literal> is weaved in via the
  670. AspectJ compiler. It would not be uncommon to use both types of
  671. security interceptors in the same application, with
  672. <literal>AspectJSecurityInterceptor</literal> being used for domain
  673. object instance security and the AOP Alliance
  674. <literal>MethodSecurityInterceptor</literal> being used for services
  675. layer security.</para>
  676. <para>Let's first consider how the
  677. <literal>AspectJSecurityInterceptor</literal> is configured in the
  678. Spring application context:</para>
  679. <para><programlisting>&lt;bean id="bankManagerSecurity" class="org.acegisecurity.intercept.method.aspectj.AspectJSecurityInterceptor"&gt;
  680. &lt;property name="validateConfigAttributes"&gt;&lt;value&gt;true&lt;/value&gt;&lt;/property&gt;
  681. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  682. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  683. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  684. &lt;property name="afterInvocationManager"&gt;&lt;ref bean="afterInvocationManager"/&gt;&lt;/property&gt;
  685. &lt;property name="objectDefinitionSource"&gt;
  686. &lt;value&gt;
  687. org.acegisecurity.context.BankManager.delete*=ROLE_SUPERVISOR,RUN_AS_SERVER
  688. org.acegisecurity.context.BankManager.getBalance=ROLE_TELLER,ROLE_SUPERVISOR,BANKSECURITY_CUSTOMER,RUN_AS_SERVER
  689. &lt;/value&gt;
  690. &lt;/property&gt;
  691. &lt;/bean&gt;</programlisting></para>
  692. <para>As you can see, aside from the class name, the
  693. <literal>AspectJSecurityInterceptor</literal> is exactly the same as
  694. the AOP Alliance security interceptor. Indeed the two interceptors can
  695. share the same <literal>objectDefinitionSource</literal>, as the
  696. <literal>ObjectDefinitionSource</literal> works with
  697. <literal>java.lang.reflect.Method</literal>s rather than an AOP
  698. library-specific class. Of course, your access decisions have access
  699. to the relevant AOP library-specific invocation (ie
  700. <literal>MethodInvocation</literal> or <literal>JoinPoint</literal>)
  701. and as such can consider a range of addition criteria when making
  702. access decisions (such as method arguments).</para>
  703. <para>Next you'll need to define an AspectJ <literal>aspect</literal>.
  704. For example:</para>
  705. <para><programlisting>package org.acegisecurity.samples.aspectj;
  706. import org.acegisecurity.intercept.method.aspectj.AspectJSecurityInterceptor;
  707. import org.acegisecurity.intercept.method.aspectj.AspectJCallback;
  708. import org.springframework.beans.factory.InitializingBean;
  709. public aspect DomainObjectInstanceSecurityAspect implements InitializingBean {
  710. private AspectJSecurityInterceptor securityInterceptor;
  711. pointcut domainObjectInstanceExecution(): target(PersistableEntity)
  712. &amp;&amp; execution(public * *(..)) &amp;&amp; !within(DomainObjectInstanceSecurityAspect);
  713. Object around(): domainObjectInstanceExecution() {
  714. if (this.securityInterceptor != null) {
  715. AspectJCallback callback = new AspectJCallback() {
  716. public Object proceedWithObject() {
  717. return proceed();
  718. }
  719. };
  720. return this.securityInterceptor.invoke(thisJoinPoint, callback);
  721. } else {
  722. return proceed();
  723. }
  724. }
  725. public AspectJSecurityInterceptor getSecurityInterceptor() {
  726. return securityInterceptor;
  727. }
  728. public void setSecurityInterceptor(AspectJSecurityInterceptor securityInterceptor) {
  729. this.securityInterceptor = securityInterceptor;
  730. }
  731. public void afterPropertiesSet() throws Exception {
  732. if (this.securityInterceptor == null)
  733. throw new IllegalArgumentException("securityInterceptor required");
  734. }
  735. }</programlisting></para>
  736. <para>In the above example, the security interceptor will be applied
  737. to every instance of <literal>PersistableEntity</literal>, which is an
  738. abstract class not shown (you can use any other class or
  739. <literal>pointcut</literal> expression you like). For those curious,
  740. <literal>AspectJCallback</literal> is needed because the
  741. <literal>proceed();</literal> statement has special meaning only
  742. within an <literal>around()</literal> body. The
  743. <literal>AspectJSecurityInterceptor</literal> calls this anonymous
  744. <literal>AspectJCallback</literal> class when it wants the target
  745. object to continue.</para>
  746. <para>You will need to configure Spring to load the aspect and wire it
  747. with the <literal>AspectJSecurityInterceptor</literal>. A bean
  748. declaration which achieves this is shown below:</para>
  749. <para><programlisting>&lt;bean id="domainObjectInstanceSecurityAspect"
  750. class="org.acegisecurity.samples.aspectj.DomainObjectInstanceSecurityAspect"
  751. factory-method="aspectOf"&gt;
  752. &lt;property name="securityInterceptor"&gt;&lt;ref bean="aspectJSecurityInterceptor"/&gt;&lt;/property&gt;
  753. &lt;/bean&gt;</programlisting></para>
  754. <para>That's it! Now you can create your beans from anywhere within
  755. your application, using whatever means you think fit (eg <literal>new
  756. Person();</literal>) and they will have the security interceptor
  757. applied.</para>
  758. </sect2>
  759. <sect2 id="security-interception-filterinvocation">
  760. <title>FilterInvocation Security Interceptor</title>
  761. <para>To secure <literal>FilterInvocation</literal>s, developers need
  762. to add a filter to their <literal>web.xml</literal> that delegates to
  763. the <literal>SecurityEnforcementFilter</literal>. A typical
  764. configuration example is provided below: <programlisting>&lt;filter&gt;
  765. &lt;filter-name&gt;Acegi HTTP Request Security Filter&lt;/filter-name&gt;
  766. &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  767. &lt;init-param&gt;
  768. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  769. &lt;param-value&gt;org.acegisecurity.intercept.web.SecurityEnforcementFilter&lt;/param-value&gt;
  770. &lt;/init-param&gt;
  771. &lt;/filter&gt;
  772. &lt;filter-mapping&gt;
  773. &lt;filter-name&gt;Acegi HTTP Request Security Filter&lt;/filter-name&gt;
  774. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  775. &lt;/filter-mapping&gt;</programlisting></para>
  776. <para>Notice that the filter is actually a
  777. <literal>FilterToBeanProxy</literal>. Most of the filters used by the
  778. Acegi Security System for Spring use this class. Refer to the Filters
  779. section to learn more about this bean.</para>
  780. <para>In the application context you will need to configure three
  781. beans:</para>
  782. <programlisting>&lt;bean id="securityEnforcementFilter" class="org.acegisecurity.intercept.web.SecurityEnforcementFilter"&gt;
  783. &lt;property name="filterSecurityInterceptor"&gt;&lt;ref bean="filterInvocationInterceptor"/&gt;&lt;/property&gt;
  784. &lt;property name="authenticationEntryPoint"&gt;&lt;ref bean="authenticationEntryPoint"/&gt;&lt;/property&gt;
  785. &lt;/bean&gt;
  786. &lt;bean id="authenticationEntryPoint" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint"&gt;
  787. &lt;property name="loginFormUrl"&gt;&lt;value&gt;/acegilogin.jsp&lt;/value&gt;&lt;/property&gt;
  788. &lt;property name="forceHttps"&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
  789. &lt;/bean&gt;
  790. &lt;bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor"&gt;
  791. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  792. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  793. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  794. &lt;property name="objectDefinitionSource"&gt;
  795. &lt;value&gt;
  796. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  797. \A/secure/super/.*\Z=ROLE_WE_DONT_HAVE
  798. \A/secure/.*\Z=ROLE_SUPERVISOR,ROLE_TELLER
  799. &lt;/value&gt;
  800. &lt;/property&gt;
  801. &lt;/bean&gt;</programlisting>
  802. <para>The <literal>AuthenticationEntryPoint</literal> will be called
  803. if the user requests a secure HTTP resource but they are not
  804. authenticated. The class handles presenting the appropriate response
  805. to the user so that authentication can begin. Three concrete
  806. implementations are provided with the Acegi Security System for
  807. Spring: <literal>AuthenticationProcessingFilterEntryPoint</literal>
  808. for commencing a form-based authentication,
  809. <literal>BasicProcessingFilterEntryPoint</literal> for commencing a
  810. HTTP Basic authentication process, and
  811. <literal>CasProcessingFilterEntryPoint</literal> for commencing a Yale
  812. Central Authentication Service (CAS) login. The
  813. <literal>AuthenticationProcessingFilterEntryPoint</literal> and
  814. <literal>CasProcessingFilterEntryPoint</literal> have optional
  815. properties related to forcing the use of HTTPS, so please refer to the
  816. JavaDocs if you require this.</para>
  817. <para>The <literal>PortMapper</literal> provides information on which
  818. HTTPS ports correspond to which HTTP ports. This is used by the
  819. <literal>AuthenticationProcessingFilterEntryPoint</literal> and
  820. several other beans. The default implementation,
  821. <literal>PortMapperImpl</literal>, knows the common HTTP ports 80 and
  822. 8080 map to HTTPS ports 443 and 8443 respectively. You can customise
  823. this mapping if desired.</para>
  824. <para>The <literal>SecurityEnforcementFilter</literal> primarily
  825. provides session management support and initiates authentication when
  826. required. It delegates actual <literal>FilterInvocation</literal>
  827. security decisions to the configured
  828. <literal>FilterSecurityInterceptor</literal>.</para>
  829. <para>Like any other security interceptor, the
  830. <literal>FilterSecurityInterceptor</literal> requires a reference to
  831. an <literal>AuthenticationManager</literal>,
  832. <literal>AccessDecisionManager</literal> and
  833. <literal>RunAsManager</literal>, which are each discussed in separate
  834. sections below. The <literal>FilterSecurityInterceptor</literal> is
  835. also configured with configuration attributes that apply to different
  836. HTTP URL requests. A full discussion of configuration attributes is
  837. provided in the High Level Design section of this document.</para>
  838. <para>The <literal>FilterSecurityInterceptor</literal> can be
  839. configured with configuration attributes in two ways. The first is via
  840. a property editor and the application context, which is shown above.
  841. The second is via writing your own
  842. <literal>ObjectDefinitionSource</literal>, although this is beyond the
  843. scope of this document. Irrespective of the approach used, the
  844. <literal>ObjectDefinitionSource</literal> is responsible for returning
  845. a <literal>ConfigAttributeDefinition</literal> object that contains
  846. all of the configuration attributes associated with a single secure
  847. HTTP URL.</para>
  848. <para>It should be noted that the
  849. <literal>FilterSecurityInterceptor.setObjectDefinitionSource()</literal>
  850. method actually expects an instance of
  851. <literal>FilterInvocationDefinitionSource</literal>. This is a marker
  852. interface which subclasses <literal>ObjectDefinitionSource</literal>.
  853. It simply denotes the <literal>ObjectDefinitionSource</literal>
  854. understands <literal>FilterInvocation</literal>s. In the interests of
  855. simplicity we'll continue to refer to the
  856. <literal>FilterInvocationDefinitionSource</literal> as an
  857. <literal>ObjectDefinitionSource</literal>, as the distinction is of
  858. little relevance to most users of the
  859. <literal>FilterSecurityInterceptor</literal>.</para>
  860. <para>If using the application context property editor approach (as
  861. shown above), commas are used to delimit the different configuration
  862. attributes that apply to each HTTP URL. Each configuration attribute
  863. is assigned into its own <literal>SecurityConfig</literal> object. The
  864. <literal>SecurityConfig</literal> object is discussed in the High
  865. Level Design section. The <literal>ObjectDefinitionSource</literal>
  866. created by the property editor,
  867. <literal>FilterInvocationDefinitionSource</literal>, matches
  868. configuration attributes against <literal>FilterInvocations</literal>
  869. based on expression evaluation of the request URL. Two standard
  870. expression syntaxes are supported. The default is to treat all
  871. expressions as regular expressions. Alternatively, the presence of a
  872. <literal>PATTERN_TYPE_APACHE_ANT</literal> directive will cause all
  873. expressions to be treated as Apache Ant paths. It is not possible to
  874. mix expression syntaxes within the same definition. For example, the
  875. earlier configuration could be generated using Apache Ant paths as
  876. follows:</para>
  877. <para><programlisting>&lt;bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor"&gt;
  878. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  879. &lt;property name="accessDecisionManager"&gt;&lt;ref bean="accessDecisionManager"/&gt;&lt;/property&gt;
  880. &lt;property name="runAsManager"&gt;&lt;ref bean="runAsManager"/&gt;&lt;/property&gt;
  881. &lt;property name="objectDefinitionSource"&gt;
  882. &lt;value&gt;
  883. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  884. PATTERN_TYPE_APACHE_ANT
  885. /secure/super/**=ROLE_WE_DONT_HAVE
  886. /secure/**=ROLE_SUPERVISOR,ROLE_TELLER
  887. &lt;/value&gt;
  888. &lt;/property&gt;
  889. &lt;/bean&gt;</programlisting></para>
  890. <para>Irrespective of the type of expression syntax used, expressions
  891. are always evaluated in the order they are defined. Thus it is
  892. important that more specific expressions are defined higher in the
  893. list than less specific expressions. This is reflected in our example
  894. above, where the more specific <literal>/secure/super/</literal>
  895. pattern appears higher than the less specific
  896. <literal>/secure/</literal> pattern. If they were reversed, the
  897. <literal>/secure/</literal> pattern would always match and the
  898. <literal>/secure/super/</literal> pattern would never be
  899. evaluated.</para>
  900. <para>The special keyword
  901. <literal>CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON</literal> causes
  902. the <literal>FilterInvocationDefinitionSource</literal> to
  903. automatically convert a request URL to lowercase before comparison
  904. against the expressions. Whilst by default the case of the request URL
  905. is not converted, it is generally recommended to use
  906. <literal>CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON</literal> and
  907. write each expression assuming lowercase.</para>
  908. <para>As with other security interceptors, the
  909. <literal>validateConfigAttributes</literal> property is observed. When
  910. set to <literal>true</literal> (the default), at startup time the
  911. <literal>FilterSecurityInterceptor</literal> will evaluate if the
  912. provided configuration attributes are valid. It does this by checking
  913. each configuration attribute can be processed by either the
  914. <literal>AccessDecisionManager</literal> or the
  915. <literal>RunAsManager</literal>. If neither of these can process a
  916. given configuration attribute, an exception is thrown.</para>
  917. </sect2>
  918. </sect1>
  919. <sect1 id="security-authentication">
  920. <title>Authentication</title>
  921. <sect2 id="security-authentication-requests">
  922. <title>Authentication Requests</title>
  923. <para>Authentication requires a way for client code to present its
  924. security identification to the Acegi Security System for Spring. This
  925. is the role of the <literal>Authentication</literal> interface. The
  926. <literal>Authentication</literal> interface holds three important
  927. objects: the principal (the identity of the caller), the credentials
  928. (the proof of the identity of the caller, such as a password), and the
  929. authorities that have been granted to the principal. The principal and
  930. its credentials are populated by the client code, whilst the granted
  931. authorities are populated by the
  932. <literal>AuthenticationManager</literal>.</para>
  933. <para><mediaobject>
  934. <imageobject role="html">
  935. <imagedata align="center" fileref="images/Authentication.gif"
  936. format="GIF" />
  937. </imageobject>
  938. <caption>
  939. <para>Figure 3: Key Authentication Architecture</para>
  940. </caption>
  941. </mediaobject></para>
  942. <para>As shown in Figure 3, the Acegi Security System for Spring
  943. includes several concrete <literal>Authentication</literal>
  944. implementations:</para>
  945. <itemizedlist spacing="compact">
  946. <listitem>
  947. <para><literal>UsernamePasswordAuthenticationToken</literal>
  948. allows a username and password to be presented as the principal
  949. and credentials respectively. It is also what is created by the
  950. HTTP Session Authentication system.</para>
  951. </listitem>
  952. <listitem>
  953. <para><literal>TestingAuthenticationToken</literal> facilitates
  954. unit testing by automatically being considered an authenticated
  955. object by its associated
  956. <literal>AuthenticationProvider</literal>.</para>
  957. </listitem>
  958. <listitem>
  959. <para><literal>RunAsUserToken</literal> is used by the default
  960. run-as authentication replacement implementation. This is
  961. discussed further in the Run-As Authentication Replacement
  962. section.</para>
  963. </listitem>
  964. <listitem>
  965. <para><literal>CasAuthenticationToken</literal> is used to
  966. represent a successful Yale Central Authentication Service (CAS)
  967. authentication. This is discussed further in the CAS
  968. section.</para>
  969. </listitem>
  970. <listitem>
  971. <para><literal>PrincipalAcegiUserToken</literal> and
  972. <literal>JettyAcegiUserToken</literal> implement
  973. <literal>AuthByAdapter</literal> (a subclass of
  974. <literal>Authentication</literal>) and are used whenever
  975. authentication is completed by Acegi Security System for Spring
  976. container adapters. This is discussed further in the Container
  977. Adapters section.</para>
  978. </listitem>
  979. </itemizedlist>
  980. <para>The authorities granted to a principal are represented by the
  981. <literal>GrantedAuthority</literal> interface. The
  982. <literal>GrantedAuthority</literal> interface is discussed at length
  983. in the Authorization section.</para>
  984. </sect2>
  985. <sect2 id="security-authentication-manager">
  986. <title>Authentication Manager</title>
  987. <para>As discussed in the Security Interception section, the
  988. <literal>AbstractSecurityInterceptor</literal> extracts the
  989. <literal>Authentication</literal> object from the
  990. <literal>SecurityContext</literal> in the
  991. <literal>SecurityContextHolder</literal>. This is then passed to an
  992. <literal>AuthenticationManager</literal>. The
  993. <literal>AuthenticationManager</literal> interface is very
  994. simple:</para>
  995. <programlisting>public Authentication authenticate(Authentication authentication) throws AuthenticationException;</programlisting>
  996. <para>Implementations of <literal>AuthenticationManager</literal> are
  997. required to throw an <literal>AuthenticationException</literal> should
  998. authentication fail, or return a fully populated
  999. <literal>Authentication</literal> object. In particular, the returned
  1000. <literal>Authentication</literal> object should contain an array of
  1001. <literal>GrantedAuthority</literal> objects. The
  1002. <literal>SecurityInterceptor</literal> places the populated
  1003. <literal>Authentication</literal> object back in the
  1004. <literal>SecurityContext</literal> in the
  1005. <literal>SecurityContextHolder</literal>, overwriting the original
  1006. <literal>Authentication</literal> object.</para>
  1007. <para>The <literal>AuthenticationException</literal> has a number of
  1008. subclasses. The most important are
  1009. <literal>BadCredentialsException</literal> (an incorrect principal or
  1010. credentials), <literal>DisabledException</literal> and
  1011. <literal>LockedException</literal>. The latter two exceptions indicate
  1012. the principal was found, but the credentials were not checked and
  1013. authentication is denied. An
  1014. <literal>AuthenticationServiceException</literal> is also provided,
  1015. which indicates the authentication system could not process the
  1016. request (eg a database was unavailable).
  1017. <literal>AuthenticationException</literal> also has a
  1018. <literal>CredentialsExpiredException</literal> and
  1019. <literal>AccoungtExpiredException</literal> subclass, although these
  1020. are less commonly used.</para>
  1021. </sect2>
  1022. <sect2 id="security-authentication-provider">
  1023. <title>Provider-Based Authentication</title>
  1024. <para>Whilst the basic <literal>Authentication</literal> and
  1025. <literal>AuthenticationManager</literal> interfaces enable users to
  1026. develop their own authentication systems, users should consider using
  1027. the provider-based authentication packages provided by the Acegi
  1028. Security System for Spring. The key class,
  1029. <literal>ProviderManager</literal>, is configured via the bean context
  1030. with a list of <literal>AuthenticationProvider</literal>s:</para>
  1031. <para><programlisting>&lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt;
  1032. &lt;property name="providers"&gt;
  1033. &lt;list&gt;
  1034. &lt;ref bean="daoAuthenticationProvider"/&gt;
  1035. &lt;ref bean="someOtherAuthenticationProvider"/&gt;
  1036. &lt;/list&gt;
  1037. &lt;/property&gt;
  1038. &lt;/bean&gt;</programlisting></para>
  1039. <para><literal>ProviderManager</literal> calls a series of registered
  1040. <literal>AuthenticationProvider</literal> implementations, until one
  1041. is found that indicates it is able to authenticate a given
  1042. <literal>Authentication</literal> class. When the first compatible
  1043. <literal>AuthenticationProvider</literal> is located, it is passed the
  1044. authentication request. The <literal>AuthenticationProvider</literal>
  1045. will then either throw an <literal>AuthenticationException</literal>
  1046. or return a fully populated <literal>Authentication</literal>
  1047. object.</para>
  1048. <para>Note the <literal>ProviderManager</literal> may throw a
  1049. <literal>ProviderNotFoundException</literal> (a subclass of
  1050. <literal>AuthenticationException</literal>) if it none of the
  1051. registered <literal>AuthenticationProviders</literal> can validate the
  1052. <literal>Authentication</literal> object.</para>
  1053. <para>The <literal>ProviderManager</literal> also has several other
  1054. important functions. It integrates with concurrent session handling
  1055. support, and it also converts any exceptions thrown by an
  1056. <literal>AuthenticationProvider</literal> and publishes a suitable
  1057. event. The events that are published are located in the
  1058. <literal>org.acegisecurity.event.authentication</literal> package and
  1059. advanced users can map different exceptions to different events by
  1060. configuring the <literal>ProviderManager.exceptionMappings</literal>
  1061. property (generally this is not required and the default event
  1062. propagation is appropriate - especially as events will simply be
  1063. ignored if you don't have an <literal>ApplicationListener</literal>
  1064. configured in the <literal>ApplicationContext</literal>).</para>
  1065. <para>Several <literal>AuthenticationProvider</literal>
  1066. implementations are provided with the Acegi Security System for
  1067. Spring:</para>
  1068. <para><itemizedlist spacing="compact">
  1069. <listitem>
  1070. <para><literal>TestingAuthenticationProvider</literal> is able
  1071. to authenticate a <literal>TestingAuthenticationToken</literal>.
  1072. The limit of its authentication is simply to treat whatever is
  1073. contained in the <literal>TestingAuthenticationToken</literal>
  1074. as valid. This makes it ideal for use during unit testing, as
  1075. you can create an <literal>Authentication</literal> object with
  1076. precisely the <literal>GrantedAuthority</literal> objects
  1077. required for calling a given method. You definitely would not
  1078. register this <literal>AuthenticationProvider</literal> on a
  1079. production system.</para>
  1080. </listitem>
  1081. <listitem>
  1082. <para><literal>DaoAuthenticationProvider</literal> is able to
  1083. authenticate a
  1084. <literal>UsernamePasswordAuthenticationToken</literal> by
  1085. accessing an authentication respository via a data access
  1086. object. This is discussed further below, as it is the main way
  1087. authentication is initially handled.</para>
  1088. </listitem>
  1089. <listitem>
  1090. <para><literal>RunAsImplAuthenticationProvider</literal> is able
  1091. to authenticate a <literal>RunAsUserToken</literal>. This is
  1092. discussed further in the Run-As Authentication Replacement
  1093. section. You would not register this
  1094. <literal>AuthenticationProvider</literal> if you were not using
  1095. run-as replacement.</para>
  1096. </listitem>
  1097. <listitem>
  1098. <para><literal>AuthByAdapterProvider</literal> is able to
  1099. authenticate any <literal>AuthByAdapter</literal> (a subclass of
  1100. <literal>Authentication</literal> used with container adapters).
  1101. This is discussed further in the Container Adapters section. You
  1102. would not register this
  1103. <literal>AuthenticationProvider</literal> if you were not using
  1104. container adapters.</para>
  1105. </listitem>
  1106. <listitem>
  1107. <para><literal>CasAuthenticationProvider</literal> is able to
  1108. authenticate Yale Central Authentication Service (CAS) tickets.
  1109. This is discussed further in the CAS Single Sign On
  1110. section.</para>
  1111. </listitem>
  1112. <listitem>
  1113. <para><literal>JaasAuthenticationProvider</literal> is able to
  1114. delegate authentication requests to a JAAS
  1115. <literal>LoginModule</literal>. This is discussed further
  1116. below.</para>
  1117. </listitem>
  1118. </itemizedlist></para>
  1119. </sect2>
  1120. <sect2 id="security-authentication-concurrent-login">
  1121. <title>Concurrent Session Support</title>
  1122. <para>Acegi Security is able to stop the same principal authenticating
  1123. to the same web application multiple times concurrently. Put
  1124. differently, you can stop user "Batman" from logging into a web
  1125. application twice at the same time.</para>
  1126. <para>To use concurrent session support, you'll need to add the
  1127. following to web.xml:</para>
  1128. <para><programlisting>&lt;listener&gt;
  1129. &lt;listener-class&gt;org.acegisecurity.ui.session.HttpSessionEventPublisher&lt;/listener-class&gt;
  1130. &lt;/listener&gt;</programlisting></para>
  1131. <para>In addition, you will need to add the
  1132. <literal>org.acegisecurity.concurrent.ConcurrentSessionFilter</literal>
  1133. to your <literal>FilterChainProxy</literal>. The
  1134. ConcurrentSessionFilter requires only one property, sessionRegistry,
  1135. which generally points to an instance of
  1136. <literal>SessionRegistryImpl</literal>.</para>
  1137. <para>The <literal>web.xml</literal>
  1138. <literal>HttpSessionEventPublisher</literal> causes an
  1139. <literal>ApplicationEvent</literal> to be published to the Spring
  1140. <literal>ApplicationContext</literal> every time a
  1141. <literal>HttpSession</literal> commences or terminates. This is
  1142. critical, as it allows the <literal>SessionRegistryImpl</literal> to
  1143. be notified when a session ends.</para>
  1144. <para>You will also need to wire up the
  1145. <literal>ConcurrentSessionControllerImpl</literal> and refer to it
  1146. from your <literal>ProviderManager</literal> bean:</para>
  1147. <para><programlisting>&lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt;
  1148. &lt;property name="providers"&gt;
  1149. &lt;!-- your providers go here --&gt;
  1150. &lt;/property&gt;
  1151. &lt;property name="sessionController"&gt;&lt;ref bean="concurrentSessionController"/&gt;&lt;/property&gt;
  1152. &lt;/bean&gt;
  1153. &lt;bean id="concurrentSessionController" class="org.acegisecurity.concurrent.ConcurrentSessionControllerImpl"&gt;
  1154. &lt;property name="maximumSessions"&gt;&lt;value&gt;1&lt;/value&gt;&lt;/property&gt;
  1155. &lt;property name="sessionRegistry"&gt;&lt;ref local="sessionRegistry"/&gt;&lt;/property&gt;
  1156. &lt;/bean&gt;
  1157. &lt;bean id="sessionRegistry" class="org.acegisecurity.concurrent.SessionRegistryImpl"/&gt;</programlisting></para>
  1158. </sect2>
  1159. <sect2 id="security-authentication-provider-dao">
  1160. <title>Data Access Object Authentication Provider</title>
  1161. <para>The Acegi Security System for Spring includes a
  1162. production-quality <literal>AuthenticationProvider</literal>
  1163. implementation called <literal>DaoAuthenticationProvider</literal>.
  1164. This authentication provider is able to authenticate a
  1165. <literal>UsernamePasswordAuthenticationToken</literal> by obtaining
  1166. authentication details from a data access object configured at bean
  1167. creation time:</para>
  1168. <para><programlisting>&lt;bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;
  1169. &lt;property name="userDetailsService"&gt;&lt;ref bean="inMemoryDaoImpl"/&gt;&lt;/property&gt;
  1170. &lt;property name="saltSource"&gt;&lt;ref bean="saltSource"/&gt;&lt;/property&gt;
  1171. &lt;property name="passwordEncoder"&gt;&lt;ref bean="passwordEncoder"/&gt;&lt;/property&gt;
  1172. &lt;/bean&gt;</programlisting></para>
  1173. <para>The <literal>PasswordEncoder</literal> and
  1174. <literal>SaltSource</literal> are optional. A
  1175. <literal>PasswordEncoder</literal> provides encoding and decoding of
  1176. passwords obtained from the authentication repository. A
  1177. <literal>SaltSource</literal> enables the passwords to be populated
  1178. with a "salt", which enhances the security of the passwords in the
  1179. authentication repository. <literal>PasswordEncoder</literal>
  1180. implementations are provided with the Acegi Security System for Spring
  1181. covering MD5, SHA and cleartext encodings. Two
  1182. <literal>SaltSource</literal> implementations are also provided:
  1183. <literal>SystemWideSaltSource</literal> which encodes all passwords
  1184. with the same salt, and <literal>ReflectionSaltSource</literal>, which
  1185. inspects a given property of the returned
  1186. <literal>UserDetails</literal> object to obtain the salt. Please refer
  1187. to the JavaDocs for further details on these optional features.</para>
  1188. <para>In addition to the properties above, the
  1189. <literal>DaoAuthenticationProvider</literal> supports optional caching
  1190. of <literal>UserDetails</literal> objects. The
  1191. <literal>UserCache</literal> interface enables the
  1192. <literal>DaoAuthenticationProvider</literal> to place a
  1193. <literal>UserDetails</literal> object into the cache, and retrieve it
  1194. from the cache upon subsequent authentication attempts for the same
  1195. username. By default the <literal>DaoAuthenticationProvider</literal>
  1196. uses the <literal>NullUserCache</literal>, which performs no caching.
  1197. A usable caching implementation is also provided,
  1198. <literal>EhCacheBasedUserCache</literal>, which is configured as
  1199. follows:</para>
  1200. <para><programlisting>&lt;bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;
  1201. &lt;property name="userDetailsService"&gt;&lt;ref bean="userDetailsService"/&gt;&lt;/property&gt;
  1202. &lt;property name="userCache"&gt;&lt;ref bean="userCache"/&gt;&lt;/property&gt;
  1203. &lt;/bean&gt;
  1204. &lt;bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&gt;
  1205. &lt;property name="configLocation"&gt;
  1206. &lt;value&gt;classpath:/ehcache-failsafe.xml&lt;/value&gt;
  1207. &lt;/property&gt;
  1208. &lt;/bean&gt;
  1209. &lt;bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean"&gt;
  1210. &lt;property name="cacheManager"&gt;
  1211. &lt;ref local="cacheManager"/&gt;
  1212. &lt;/property&gt;
  1213. &lt;property name="cacheName"&gt;
  1214. &lt;value&gt;userCache&lt;/value&gt;
  1215. &lt;/property&gt;
  1216. &lt;/bean&gt;
  1217. &lt;bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache"&gt;
  1218. &lt;property name="cache"&gt;&lt;ref local="userCacheBackend"/&gt;&lt;/property&gt;
  1219. &lt;/bean&gt;</programlisting></para>
  1220. <para>All Acegi Security EH-CACHE implementations (including
  1221. <literal>EhCacheBasedUserCache</literal>) require an EH-CACHE
  1222. <literal>Cache</literal> object. The <literal>Cache</literal> object
  1223. can be obtained from wherever you like, although we recommend you use
  1224. Spring's factory classes as shown in the above configuration. If using
  1225. Spring's factory classes, please refer to the Spring documentation for
  1226. further details on how to optimise the cache storage location, memory
  1227. usage, eviction policies, timeouts etc.</para>
  1228. <para>For a class to be able to provide the
  1229. <literal>DaoAuthenticationProvider</literal> with access to an
  1230. authentication repository, it must implement the
  1231. <literal>UserDetailsService</literal> interface:</para>
  1232. <para><programlisting>public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException;</programlisting></para>
  1233. <para>The <literal>UserDetails</literal> is an interface that provides
  1234. getters that guarantee non-null provision of basic authentication
  1235. information such as the username, password, granted authorities and
  1236. whether the user is enabled or disabled. A concrete implementation,
  1237. <literal>User</literal>, is also provided. Acegi Security users will
  1238. need to decide when writing their
  1239. <literal>UserDetailsService</literal> what type of
  1240. <literal>UserDetails</literal> to return. In most cases
  1241. <literal>User</literal> will be used directly or subclassed, although
  1242. special circumstances (such as object relational mappers) may require
  1243. users to write their own <literal>UserDetails</literal> implementation
  1244. from scratch. <literal>UserDetails</literal> is often used to store
  1245. additional principal-related properties (such as their telephone
  1246. number and email address), so they can be easily used by web
  1247. views.</para>
  1248. <para>Given <literal>UserDetailsService</literal> is so simple to
  1249. implement, it should be easy for users to retrieve authentication
  1250. information using a persistence strategy of their choice.</para>
  1251. <para>A design decision was made not to support account locking in the
  1252. <literal>DaoAuthenticationProvider</literal>, as doing so would have
  1253. increased the complexity of the <literal>UserDetailsService</literal>
  1254. interface. For instance, a method would be required to increase the
  1255. count of unsuccessful authentication attempts. Such functionality
  1256. could be easily provided by leveraging the application event
  1257. publishing features discussed below.</para>
  1258. <para><literal>DaoAuthenticationProvider</literal> returns an
  1259. <literal>Authentication</literal> object which in turn has its
  1260. <literal>principal</literal> property set. The principal will be
  1261. either a <literal>String</literal> (which is essentially the username)
  1262. or a <literal>UserDetails</literal> object (which was looked up from
  1263. the <literal>UserDetailsService</literal>). By default the
  1264. <literal>UserDetails</literal> is returned, as this enables
  1265. applications to add extra properties potentially of use in
  1266. applications, such as the user's full name, email address etc. If
  1267. using container adapters, or if your applications were written to
  1268. operate with <literal>String</literal>s (as was the case for releases
  1269. prior to Acegi Security 0.6), you should set the
  1270. <literal>DaoAuthenticationProvider.forcePrincipalAsString</literal>
  1271. property to <literal>true</literal> in your application
  1272. context.</para>
  1273. </sect2>
  1274. <sect2 id="security-authentication-provider-in-memory">
  1275. <title>In-Memory Authentication</title>
  1276. <para>Whilst it is easy to use the
  1277. <literal>DaoAuthenticationProvider</literal> and create a custom
  1278. <literal>UserDetailsService</literal> implementation that extracts
  1279. information from a persistence engine of choice, many applications do
  1280. not require such complexity. One alternative is to configure an
  1281. authentication repository in the application context itself using the
  1282. <literal>InMemoryDaoImpl</literal>:</para>
  1283. <para><programlisting>&lt;bean id="inMemoryDaoImpl" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl"&gt;
  1284. &lt;property name="userMap"&gt;
  1285. &lt;value&gt;
  1286. marissa=koala,ROLE_TELLER,ROLE_SUPERVISOR
  1287. dianne=emu,ROLE_TELLER
  1288. scott=wombat,ROLE_TELLER
  1289. peter=opal,disabled,ROLE_TELLER
  1290. &lt;/value&gt;
  1291. &lt;/property&gt;
  1292. &lt;/bean&gt;</programlisting></para>
  1293. <para>The <literal>userMap</literal> property contains each of the
  1294. usernames, passwords, a list of granted authorities and an optional
  1295. enabled/disabled keyword. Commas delimit each token. The username must
  1296. appear to the left of the equals sign, and the password must be the
  1297. first token to the right of the equals sign. The
  1298. <literal>enabled</literal> and <literal>disabled</literal> keywords
  1299. (case insensitive) may appear in the second or any subsequent token.
  1300. Any remaining tokens are treated as granted authorities, which are
  1301. created as <literal>GrantedAuthorityImpl</literal> objects (refer to
  1302. the Authorization section for further discussion on granted
  1303. authorities). Note that if a user has no password and/or no granted
  1304. authorities, the user will not be created in the in-memory
  1305. authentication repository.</para>
  1306. <para><literal>InMemoryDaoImpl</literal> also offers a
  1307. <literal>setUserProperties(Properties)</literal> method, which allows
  1308. you to externalise the <literal>java.util.Properties</literal> in
  1309. another Spring configured bean or an external properties file. This
  1310. might prove useful for simple applications that have a larger number
  1311. of users, or deployment-time configuration changes, but do not wish to
  1312. use a full database for authentication details.</para>
  1313. </sect2>
  1314. <sect2 id="security-authentication-provider-jdbc">
  1315. <title>JDBC Authentication</title>
  1316. <para>The Acegi Security System for Spring also includes an
  1317. authentication provider that can obtain authentication information
  1318. from a JDBC data source. The typical configuration for the
  1319. <literal>JdbcDaoImpl</literal> is shown below:</para>
  1320. <para><programlisting>&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
  1321. &lt;property name="driverClassName"&gt;&lt;value&gt;org.hsqldb.jdbcDriver&lt;/value&gt;&lt;/property&gt;
  1322. &lt;property name="url"&gt;&lt;value&gt;jdbc:hsqldb:hsql://localhost:9001&lt;/value&gt;&lt;/property&gt;
  1323. &lt;property name="username"&gt;&lt;value&gt;sa&lt;/value&gt;&lt;/property&gt;
  1324. &lt;property name="password"&gt;&lt;value&gt;&lt;/value&gt;&lt;/property&gt;
  1325. &lt;/bean&gt;
  1326. &lt;bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl"&gt;
  1327. &lt;property name="dataSource"&gt;&lt;ref bean="dataSource"/&gt;&lt;/property&gt;
  1328. &lt;/bean&gt;</programlisting></para>
  1329. <para>You can use different relational database management systems by
  1330. modifying the <literal>DriverManagerDataSource</literal> shown above.
  1331. Irrespective of the database used, a standard schema must be used as
  1332. indicated in <literal>dbinit.txt</literal>.</para>
  1333. <para>If you default schema is unsuitable for your needs,
  1334. <literal>JdbcDaoImpl</literal> provides two properties that allow
  1335. customisation of the SQL statements. You may also subclass the
  1336. <literal>JdbcDaoImpl</literal> if further customisation is necessary.
  1337. Please refer to the JavaDocs for details.</para>
  1338. </sect2>
  1339. <sect2 id="security-authentication-provider-jaas">
  1340. <title>JAAS Authentication</title>
  1341. <para>Acegi Security provides a package able to delegate
  1342. authentication requests to the Java Authentication and Authorization
  1343. Service (JAAS). This package is discussed in detail below.</para>
  1344. <para>Central to JAAS operation are login configuration files. To
  1345. learn more about JAAS login configuration files, consult the JAAS
  1346. reference documentation available from Sun Microsystems. We expect you
  1347. to have a basic understanding of JAAS and its login configuration file
  1348. syntax in order to understand this section.</para>
  1349. <sect3>
  1350. <title>JaasAuthenticationProvider</title>
  1351. <para>The <literal>JaasAuthenticationProvider</literal> attempts to
  1352. authenticate a user’s principal and credentials through JAAS.</para>
  1353. <para>Let’s assume we have a JAAS login configuration file,
  1354. <literal>/WEB-INF/login.conf</literal>, with the following
  1355. contents:</para>
  1356. <para><programlisting>JAASTest {
  1357. sample.SampleLoginModule required;
  1358. };</programlisting></para>
  1359. <para>Like all Acegi Security beans, the
  1360. <literal>JaasAuthenticationProvider</literal> is configured via the
  1361. application context. The following definitions would correspond to
  1362. the above JAAS login configuration file:</para>
  1363. <para><programlisting>&lt;bean id="jaasAuthenticationProvider" class="org.acegisecurity.providers.jaas.JaasAuthenticationProvider"&gt;
  1364. &lt;property name="loginConfig"&gt;
  1365. &lt;value&gt;/WEB-INF/login.conf&lt;/value&gt;
  1366. &lt;/property&gt;
  1367. &lt;property name="loginContextName"&gt;
  1368. &lt;value&gt;JAASTest&lt;/value&gt;
  1369. &lt;/property&gt;
  1370. &lt;property name="callbackHandlers"&gt;
  1371. &lt;list&gt;
  1372. &lt;bean class="org.acegisecurity.providers.jaas.JaasNameCallbackHandler"/&gt;
  1373. &lt;bean class="org.acegisecurity.providers.jaas.JaasPasswordCallbackHandler"/&gt;
  1374. &lt;/list&gt;
  1375. &lt;/property&gt;
  1376. &lt;property name="authorityGranters"&gt;
  1377. &lt;list&gt;
  1378. &lt;bean class="org.acegisecurity.providers.jaas.TestAuthorityGranter"/&gt;
  1379. &lt;/list&gt;
  1380. &lt;/property&gt;
  1381. &lt;/bean&gt;</programlisting></para>
  1382. <para>The <literal>CallbackHandler</literal>s and
  1383. <literal>AuthorityGranter</literal>s are discussed below.</para>
  1384. </sect3>
  1385. <sect3>
  1386. <title>Callbacks</title>
  1387. <para>Most JAAS <literal>LoginModule</literal>s require a callback
  1388. of some sort. These callbacks are usually used to obtain the
  1389. username and password from the user. In an Acegi Security
  1390. deployment, Acegi Security is responsible for this user interaction
  1391. (typically via a reference to a
  1392. <literal>ContextHolder</literal>-managed
  1393. <literal>Authentication</literal> object). The JAAS package for
  1394. Acegi Security provides two default callback handlers,
  1395. <literal>JaasNameCallbackHandler</literal> and
  1396. <literal>JaasPasswordCallbackHandler</literal>. Each of these
  1397. callback handlers implement
  1398. <literal>JaasAuthenticationCallbackHandler</literal>. In most cases
  1399. these callback handlers can simply be used without understanding the
  1400. internal mechanics. For those needing full control over the callback
  1401. behavior, internally <literal>JaasAutheticationProvider</literal>
  1402. wraps these <literal>JaasAuthenticationCallbackHandler</literal>s
  1403. with an <literal>InternalCallbackHandler</literal>. The
  1404. <literal>InternalCallbackHandler</literal> is the class that
  1405. actually implements JAAS’ normal <literal>CallbackHandler</literal>
  1406. interface. Any time that the JAAS <literal>LoginModule</literal> is
  1407. used, it is passed a list of application context configured
  1408. <literal>InternalCallbackHandler</literal>s. If the
  1409. <literal>LoginModule</literal> requests a callback against the
  1410. <literal>InternalCallbackHandler</literal>s, the callback is in-turn
  1411. passed to the <literal>JaasAuthenticationCallbackHandler</literal>s
  1412. being wrapped.</para>
  1413. </sect3>
  1414. <sect3>
  1415. <title>AuthorityGranters</title>
  1416. <para>JAAS works with principals. Even “roles” are represented as
  1417. principals in JAAS. Acegi Security, on the other hand, works with
  1418. <literal>Authentication</literal> objects. Each
  1419. <literal>Authentication</literal> object contains a single
  1420. principal, and multiple <literal>GrantedAuthority</literal>[]s. To
  1421. facilitate mapping between these different concepts, the Acegi
  1422. Security JAAS package includes an
  1423. <literal>AuthorityGranter</literal> interface. An
  1424. <literal>AuthorityGranter</literal> is responsible for inspecting a
  1425. JAAS principal and returning a <literal>String</literal>. The
  1426. <literal>JaasAuthenticationProvider</literal> then creates a
  1427. <literal>JaasGrantedAuthority</literal> (which implements Acegi
  1428. Security’s <literal>GrantedAuthority</literal> interface) containing
  1429. both the <literal>AuthorityGranter</literal>-returned
  1430. <literal>String</literal> and the JAAS principal that the
  1431. <literal>AuthorityGranter</literal> was passed. The
  1432. <literal>JaasAuthenticationProvider</literal> obtains the JAAS
  1433. principals by firstly successfully authenticating the user’s
  1434. credentials using the JAAS <literal>LoginModule</literal>, and then
  1435. accessing the <literal>LoginContext</literal> it returns. A call to
  1436. <literal>LoginContext.getSubject().getPrincipals()</literal> is
  1437. made, with each resulting principal passed to each
  1438. <literal>AuthorityGranter</literal> defined against the
  1439. <literal>JaasAuthenticationProvider.setAuthorityGranters(List)</literal>
  1440. property. Acegi Security does not include any production
  1441. <literal>AuthorityGranter</literal>s given every JAAS principal has
  1442. an implementation-specific meaning. However, there is a
  1443. <literal>TestAuthorityGranter</literal> in the unit tests that
  1444. demonstrates a simple <literal>AuthorityGranter</literal>
  1445. implementation.</para>
  1446. </sect3>
  1447. </sect2>
  1448. <sect2 id="security-authentication-provider-siteminder">
  1449. <title>Siteminder Authentication</title>
  1450. <para>Acegi Security provides a web filter
  1451. <literal>(org.acegisecurity.ui.webapp.SiteminderAuthenticationProcessingFilter</literal>)
  1452. that can be used to process requests that have been pre-authenticated
  1453. by Computer Associates' Siteminder. This filter assumes that you're
  1454. using Siteminder for <emphasis>authentication</emphasis>, and your
  1455. application (or backing datasource) is used for
  1456. <emphasis>authorization</emphasis>. The use of Siteminder for
  1457. <emphasis>authorization</emphasis> is not yet directly supported by
  1458. Acegi.</para>
  1459. <para>Recall that a Siteminder agent is set up on your web server to
  1460. intercept a user's first call to your application. This agent
  1461. redirects the initial request to a login page, and only after
  1462. successful authentication does your application receive the request.
  1463. Authenticated requests contain one or more HTTP headers populated by
  1464. the Siteminder agent. Below we'll assume that the request header key
  1465. containing the user's identity is "SM_USER", but of course your header
  1466. values may be different based on Siteminder policy server
  1467. configuration. Please refer to your company's "single sign-on" group
  1468. for header details.</para>
  1469. <sect3>
  1470. <title>SiteminderAuthenticationProcessingFilter</title>
  1471. <para>The first step in setting up Acegi's Siteminder support is to
  1472. define an <literal>authenticationProcessingFilter</literal> bean and
  1473. give it an <literal>authenticationManager</literal> to use, as well
  1474. as to tell it where to send users upon success and failure and where
  1475. to find the Siteminder username and password values. Most people
  1476. won't need the password value since Siteminder has already
  1477. authenticated the user, so it's typical to use the same header for
  1478. both.</para>
  1479. <para><programlisting> &lt;!-- ======================== SITEMINDER AUTHENTICATION PROCESSING FILTER ======================= --&gt;
  1480. &lt;bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.SiteminderAuthenticationProcessingFilter"&gt;
  1481. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  1482. &lt;property name="authenticationFailureUrl"&gt;&lt;value&gt;/login.jsp?login_error=1&lt;/value&gt;&lt;/property&gt;
  1483. &lt;property name="defaultTargetUrl"&gt;&lt;value&gt;/security.do?method=getMainMenu&lt;/value&gt;&lt;/property&gt;
  1484. &lt;property name="filterProcessesUrl"&gt;&lt;value&gt;/j_acegi_security_check&lt;/value&gt;&lt;/property&gt;
  1485. &lt;property name="siteminderUsernameHeaderKey"&gt;&lt;value&gt;SM_USER&lt;/value&gt;&lt;/property&gt;
  1486. &lt;property name="siteminderPasswordHeaderKey"&gt;&lt;value&gt;SM_USER&lt;/value&gt;&lt;/property&gt;
  1487. &lt;/bean&gt;</programlisting></para>
  1488. <para>Since this <literal>authenticationProcessingFilter</literal>
  1489. depends on an <literal>authenticationManager</literal>, we'll need
  1490. to define one:</para>
  1491. <para><programlisting> &lt;!-- ======================== AUTHENTICATION ======================= --&gt;
  1492. &lt;!--
  1493. - The top-level Authentication Manager is responsible for all application AUTHENTICATION
  1494. - operations. Note that it must reference one or more provider(s) defined below.
  1495. --&gt;
  1496. &lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt;
  1497. &lt;property name="providers"&gt;
  1498. &lt;list&gt;
  1499. &lt;ref local="daoAuthenticationProvider"/&gt;
  1500. &lt;/list&gt;
  1501. &lt;/property&gt;
  1502. &lt;/bean&gt;</programlisting></para>
  1503. <para>Note that your <literal>daoAuthenticationProvider</literal>
  1504. above will expect the password property to match what it expects. In
  1505. this case, authentication has already been handled by Siteminder and
  1506. you've specified the same HTTP header for both username and
  1507. password, so you can code
  1508. <literal>daoAuthenticationProvider</literal> to simply make sure the
  1509. username and password values match. This may sound like a security
  1510. weakness, but remember that users have to authenticate with
  1511. Siteminder before your application ever receives the requests, so
  1512. the purpose of your <literal>daoAuthenticationProvider</literal>
  1513. should simply be to assign roles and other properties needed by
  1514. subsequent method interceptors, etc.</para>
  1515. <para>Finally we need to tell the
  1516. <literal>filterChainProxy</literal> to include the
  1517. <literal>authenticationProcessingFilter</literal> in its
  1518. operations.</para>
  1519. <para><programlisting> &lt;!-- ======================== FILTER CHAIN ======================= --&gt;
  1520. &lt;!--
  1521. - The web.xml file has a single filter reference to this top-level bean, which
  1522. - invokes the chain of sub-filters specified below.
  1523. --&gt;
  1524. &lt;bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy"&gt;
  1525. &lt;property name="filterInvocationDefinitionSource"&gt;
  1526. &lt;value&gt;
  1527. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  1528. PATTERN_TYPE_APACHE_ANT
  1529. /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,securityEnforcementFilter
  1530. &lt;/value&gt;
  1531. &lt;/property&gt;
  1532. &lt;/bean&gt;</programlisting></para>
  1533. <para>In summary, once the user has authenticated through
  1534. Siteminder, their header-loaded request will be brokered by
  1535. <literal>filterChainProxy</literal> to
  1536. <literal>authenticationProcessingFilter</literal>, which in turn
  1537. will grab the user's identity from the SM_USER request header. The
  1538. user's identity will then be passed to the
  1539. <literal>authenticationManager</literal> and finally
  1540. <literal>daoAuthenticationProvider</literal> will do the work of
  1541. authorizing the user against back-end databases, etc. and loading
  1542. the <literal>UserDetails</literal> implementation with roles,
  1543. username and any other property you deem relevant.</para>
  1544. </sect3>
  1545. </sect2>
  1546. <sect2 id="security-authentication-recommendations">
  1547. <title>Authentication Recommendations</title>
  1548. <para>With the heavy use of interfaces throughout the authentication
  1549. system (<literal>Authentication</literal>,
  1550. <literal>AuthenticationManager</literal>,
  1551. <literal>AuthenticationProvider</literal> and
  1552. <literal>UserDetailsService</literal>) it might be confusing to a new
  1553. user to know which part of the authentication system to customize. In
  1554. general, the following is recommended:</para>
  1555. <itemizedlist>
  1556. <listitem>
  1557. <para>Use the
  1558. <literal>UsernamePasswordAuthenticationToken</literal>
  1559. implementation where possible.</para>
  1560. </listitem>
  1561. <listitem>
  1562. <para>If you simply need to implement a new authentication
  1563. repository (eg to obtain user details from your application’s
  1564. existing database), use the
  1565. <literal>DaoAuthenticationProvider</literal> along with the
  1566. <literal>UserDetailsService</literal>. It is the fastest and
  1567. safest way to integrate an external database.</para>
  1568. </listitem>
  1569. <listitem>
  1570. <para>If you're using Container Adapters or a
  1571. <literal>RunAsManager</literal> that replaces the
  1572. <literal>Authentication</literal> object, ensure you have
  1573. registered the <literal>AuthByAdapterProvider</literal> and
  1574. <literal>RunAsManagerImplProvider</literal> respectively with your
  1575. <literal>ProviderManager</literal>.</para>
  1576. </listitem>
  1577. <listitem>
  1578. <para>Never enable the
  1579. <literal>TestingAuthenticationProvider</literal> on a production
  1580. system. Doing so will allow any client to simply present a
  1581. <literal>TestingAuthenticationToken</literal> and obtain whatever
  1582. access they request.</para>
  1583. </listitem>
  1584. <listitem>
  1585. <para>Adding a new <literal>AuthenticationProvider</literal> is
  1586. sufficient to support most custom authentication requirements.
  1587. Only unusual requirements would require the
  1588. <literal>ProviderManager</literal> to be replaced with a different
  1589. <literal>AuthenticationManager</literal>.</para>
  1590. </listitem>
  1591. </itemizedlist>
  1592. </sect2>
  1593. </sect1>
  1594. <sect1 id="security-authorization">
  1595. <title>Authorization</title>
  1596. <sect2 id="security-authorization-granted-authorities">
  1597. <title>Granted Authorities</title>
  1598. <para>As briefly mentioned in the Authentication section, all
  1599. <literal>Authentication</literal> implementations are required to
  1600. store an array of <literal>GrantedAuthority</literal> objects. These
  1601. represent the authorities that have been granted to the principal. The
  1602. <literal>GrantedAuthority</literal> objects are inserted into the
  1603. <literal>Authentication</literal> object by the
  1604. <literal>AuthenticationManager</literal> and are later read by
  1605. <literal>AccessDecisionManager</literal>s when making authorization
  1606. decisions.</para>
  1607. <para><literal>GrantedAuthority</literal> is an interface with only
  1608. one method:</para>
  1609. <para><programlisting>public String getAuthority();</programlisting></para>
  1610. <para>This method allows <literal>AccessDecisionManager</literal>s to
  1611. obtain a precise <literal>String</literal> representation of the
  1612. <literal>GrantedAuthority</literal>. By returning a representation as
  1613. a <literal>String</literal>, a <literal>GrantedAuthority</literal> can
  1614. be easily "read" by most <literal>AccessDecisionManager</literal>s. If
  1615. a <literal>GrantedAuthority</literal> cannot be precisely represented
  1616. as a <literal>String</literal>, the
  1617. <literal>GrantedAuthority</literal> is considered "complex" and
  1618. <literal>getAuthority()</literal> must return
  1619. <literal>null</literal>.</para>
  1620. <para>An example of a "complex" <literal>GrantedAuthority</literal>
  1621. would be an implementation that stores a list of operations and
  1622. authority thresholds that apply to different customer account numbers.
  1623. Representing this complex <literal>GrantedAuthority</literal> as a
  1624. <literal>String</literal> would be quite complex, and as a result the
  1625. <literal>getAuthority()</literal> method should return
  1626. <literal>null</literal>. This will indicate to any
  1627. <literal>AccessDecisionManager</literal> that it will need to
  1628. specifically support the <literal>GrantedAuthority</literal>
  1629. implementation in order to understand its contents.</para>
  1630. <para>The Acegi Security System for Spring includes one concrete
  1631. <literal>GrantedAuthority</literal> implementation,
  1632. <literal>GrantedAuthorityImpl</literal>. This allows any
  1633. user-specified <literal>String</literal> to be converted into a
  1634. <literal>GrantedAuthority</literal>. All
  1635. <literal>AuthenticationProvider</literal>s included with the security
  1636. architecture use <literal>GrantedAuthorityImpl</literal> to populate
  1637. the <literal>Authentication</literal> object.</para>
  1638. </sect2>
  1639. <sect2 id="security-authorization-access-decision-managers">
  1640. <title>Access Decision Managers</title>
  1641. <para>The <literal>AccessDecisionManager</literal> is called by the
  1642. <literal>AbstractSecurityInterceptor</literal> and is responsible for
  1643. making final access control decisions. The
  1644. <literal>AccessDecisionManager</literal> interface contains three
  1645. methods:</para>
  1646. <para><programlisting>public void decide(Authentication authentication, Object object, ConfigAttributeDefinition config) throws AccessDeniedException;
  1647. public boolean supports(ConfigAttribute attribute);
  1648. public boolean supports(Class clazz);</programlisting></para>
  1649. <para>As can be seen from the first method, the
  1650. <literal>AccessDecisionManager</literal> is passed via method
  1651. parameters all information that is likely to be of value in assessing
  1652. an authorization decision. In particular, passing the secure
  1653. <literal>Object</literal> enables those arguments contained in the
  1654. actual secure object invocation to be inspected. For example, let's
  1655. assume the secure object was a <literal>MethodInvocation</literal>. It
  1656. would be easy to query the <literal>MethodInvocation</literal> for any
  1657. <literal>Customer</literal> argument, and then implement some sort of
  1658. security logic in the <literal>AccessDecisionManager</literal> to
  1659. ensure the principal is permitted to operate on that customer.
  1660. Implementations are expected to throw an
  1661. <literal>AccessDeniedException</literal> if access is denied.</para>
  1662. <para>The <literal>supports(ConfigAttribute)</literal> method is
  1663. called by the <literal>AbstractSecurityInterceptor</literal> at
  1664. startup time to determine if the
  1665. <literal>AccessDecisionManager</literal> can process the passed
  1666. <literal>ConfigAttribute</literal>. The
  1667. <literal>supports(Class)</literal> method is called by a security
  1668. interceptor implementation to ensure the configured
  1669. <literal>AccessDecisionManager</literal> supports the type of secure
  1670. object that the security interceptor will present.</para>
  1671. </sect2>
  1672. <sect2 id="security-authorization-voting-decision-manager">
  1673. <title>Voting Decision Manager</title>
  1674. <para>Whilst users can implement their own
  1675. <literal>AccessDecisionManager</literal> to control all aspects of
  1676. authorization, the Acegi Security System for Spring includes several
  1677. <literal>AccessDecisionManager</literal> implementations that are
  1678. based on voting. Figure 4 illustrates the relevant classes.</para>
  1679. <para><mediaobject>
  1680. <imageobject role="html">
  1681. <imagedata align="center"
  1682. fileref="images/AccessDecisionVoting.gif"
  1683. format="GIF" />
  1684. </imageobject>
  1685. <caption>
  1686. <para>Figure 4: Voting Decision Manager</para>
  1687. </caption>
  1688. </mediaobject></para>
  1689. <para>Using this approach, a series of
  1690. <literal>AccessDecisionVoter</literal> implementations are polled on
  1691. an authorization decision. The
  1692. <literal>AccessDecisionManager</literal> then decides whether or not
  1693. to throw an <literal>AccessDeniedException</literal> based on its
  1694. assessment of the votes.</para>
  1695. <para>The <literal>AccessDecisionVoter</literal> interface has three
  1696. methods:</para>
  1697. <para><programlisting>public int vote(Authentication authentication, Object object, ConfigAttributeDefinition config);
  1698. public boolean supports(ConfigAttribute attribute);
  1699. public boolean supports(Class clazz);</programlisting></para>
  1700. <para>Concrete implementations return an <literal>int</literal>, with
  1701. possible values being reflected in the
  1702. <literal>AccessDecisionVoter</literal> static fields
  1703. <literal>ACCESS_ABSTAIN</literal>, <literal>ACCESS_DENIED</literal>
  1704. and <literal>ACCESS_GRANTED</literal>. A voting implementation will
  1705. return <literal>ACCESS_ABSTAIN</literal> if it has no opinion on an
  1706. authorization decision. If it does have an opinion, it must return
  1707. either <literal>ACCESS_DENIED</literal> or
  1708. <literal>ACCESS_GRANTED</literal>.</para>
  1709. <para>There are three concrete
  1710. <literal>AccessDecisionManager</literal>s provided with the Acegi
  1711. Security System for Spring that tally the votes. The
  1712. <literal>ConsensusBased</literal> implementation will grant or deny
  1713. access based on the consensus of non-abstain votes. Properties are
  1714. provided to control behavior in the event of an equality of votes or
  1715. if all votes are abstain. The <literal>AffirmativeBased</literal>
  1716. implementation will grant access if one or more
  1717. <literal>ACCESS_GRANTED</literal> votes were received (ie a deny vote
  1718. will be ignored, provided there was at least one grant vote). Like the
  1719. <literal>ConsensusBased</literal> implementation, there is a parameter
  1720. that controls the behavior if all voters abstain. The
  1721. <literal>UnanimousBased</literal> provider expects unanimous
  1722. <literal>ACCESS_GRANTED</literal> votes in order to grant access,
  1723. ignoring abstains. It will deny access if there is any
  1724. <literal>ACCESS_DENIED</literal> vote. Like the other implementations,
  1725. there is a parameter that controls the behaviour if all voters
  1726. abstain.</para>
  1727. <para>It is possible to implement a custom
  1728. <literal>AccessDecisionManager</literal> that tallies votes
  1729. differently. For example, votes from a particular
  1730. <literal>AccessDecisionVoter</literal> might receive additional
  1731. weighting, whilst a deny vote from a particular voter may have a veto
  1732. effect.</para>
  1733. <para>There are two concrete <literal>AccessDecisionVoter</literal>
  1734. implementations provided with the Acegi Security System for Spring.
  1735. The <literal>RoleVoter</literal> class will vote if any
  1736. ConfigAttribute begins with <literal>ROLE_</literal>. It will vote to
  1737. grant access if there is a <literal>GrantedAuthority</literal> which
  1738. returns a <literal>String</literal> representation (via the
  1739. <literal>getAuthority()</literal> method) exactly equal to one or more
  1740. <literal>ConfigAttributes</literal> starting with
  1741. <literal>ROLE_</literal>. If there is no exact match of any
  1742. <literal>ConfigAttribute</literal> starting with
  1743. <literal>ROLE_</literal>, the <literal>RoleVoter</literal> will vote
  1744. to deny access. If no <literal>ConfigAttribute</literal> begins with
  1745. <literal>ROLE_</literal>, the voter will abstain.
  1746. <literal>RoleVoter</literal> is case sensitive on comparisons as well
  1747. as the <literal>ROLE_</literal> prefix.</para>
  1748. <para>BasicAclEntryVoter is the other concrete voter included with
  1749. Acegi Security. It integrates with Acegi Security's
  1750. <literal>AclManager</literal> (discussed later). This voter is
  1751. designed to have multiple instances in the same application context,
  1752. such as:</para>
  1753. <para><programlisting>&lt;bean id="aclContactReadVoter" class="org.acegisecurity.vote.BasicAclEntryVoter"&gt;
  1754. &lt;property name="processConfigAttribute"&gt;&lt;value&gt;ACL_CONTACT_READ&lt;/value&gt;&lt;/property&gt;
  1755. &lt;property name="processDomainObjectClass"&gt;&lt;value&gt;sample.contact.Contact&lt;/value&gt;&lt;/property&gt;
  1756. &lt;property name="aclManager"&gt;&lt;ref local="aclManager"/&gt;&lt;/property&gt;
  1757. &lt;property name="requirePermission"&gt;
  1758. &lt;list&gt;
  1759. &lt;ref local="org.acegisecurity.acl.basic.SimpleAclEntry.ADMINISTRATION"/&gt;
  1760. &lt;ref local="org.acegisecurity.acl.basic.SimpleAclEntry.READ"/&gt;
  1761. &lt;/list&gt;
  1762. &lt;/property&gt;
  1763. &lt;/bean&gt;
  1764. &lt;bean id="aclContactDeleteVoter" class="org.acegisecurity.vote.BasicAclEntryVoter"&gt;
  1765. &lt;property name="processConfigAttribute"&gt;&lt;value&gt;ACL_CONTACT_DELETE&lt;/value&gt;&lt;/property&gt;
  1766. &lt;property name="processDomainObjectClass"&gt;&lt;value&gt;sample.contact.Contact&lt;/value&gt;&lt;/property&gt;
  1767. &lt;property name="aclManager"&gt;&lt;ref local="aclManager"/&gt;&lt;/property&gt;
  1768. &lt;property name="requirePermission"&gt;
  1769. &lt;list&gt;
  1770. &lt;ref local="org.acegisecurity.acl.basic.SimpleAclEntry.ADMINISTRATION"/&gt;
  1771. &lt;ref local="org.acegisecurity.acl.basic.SimpleAclEntry.DELETE"/&gt;
  1772. &lt;/list&gt;
  1773. &lt;/property&gt;
  1774. &lt;/bean&gt;</programlisting></para>
  1775. <para>In the above example, you'd define
  1776. <literal>ACL_CONTACT_READ</literal> or
  1777. <literal>ACL_CONTACT_DELETE</literal> against some methods on a
  1778. <literal>MethodSecurityInterceptor</literal> or
  1779. <literal>AspectJSecurityInterceptor</literal>. When those methods are
  1780. invoked, the above applicable voter defined above would vote to grant
  1781. or deny access. The voter would look at the method invocation to
  1782. locate the first argument of type
  1783. <literal>sample.contact.Contact</literal>, and then pass that
  1784. <literal>Contact</literal> to the <literal>AclManager</literal>. The
  1785. <literal>AclManager</literal> will then return an access control list
  1786. (ACL) that applies to the current <literal>Authentication</literal>.
  1787. Assuming that ACL contains one of the listed
  1788. <literal>requirePermission</literal>s, the voter will vote to grant
  1789. access. If the ACL does not contain one of the permissions defined
  1790. against the voter, the voter will vote to deny access.
  1791. <literal>BasicAclEntryVoter</literal> is an important class as it
  1792. allows you to build truly complex applications with domain object
  1793. security entirely defined in the application context. If you're
  1794. interested in learning more about Acegi Security's ACL capabilities
  1795. and how best to apply them, please see the ACL and "After Invocation"
  1796. sections of this reference guide, and the Contacts sample
  1797. application.</para>
  1798. <para>It is also possible to implement a custom
  1799. <literal>AccessDecisionVoter</literal>. Several examples are provided
  1800. in the Acegi Security System for Spring unit tests, including
  1801. <literal>ContactSecurityVoter</literal> and
  1802. <literal>DenyVoter</literal>. The
  1803. <literal>ContactSecurityVoter</literal> abstains from voting decisions
  1804. where a <literal>CONTACT_OWNED_BY_CURRENT_USER</literal>
  1805. <literal>ConfigAttribute</literal> is not found. If voting, it queries
  1806. the <literal>MethodInvocation</literal> to extract the owner of the
  1807. <literal>Contact</literal> object that is subject of the method call.
  1808. It votes to grant access if the <literal>Contact</literal> owner
  1809. matches the principal presented in the
  1810. <literal>Authentication</literal> object. It could have just as easily
  1811. compared the <literal>Contact</literal> owner with some
  1812. <literal>GrantedAuthority</literal> the
  1813. <literal>Authentication</literal> object presented. All of this is
  1814. achieved with relatively few lines of code and demonstrates the
  1815. flexibility of the authorization model.</para>
  1816. </sect2>
  1817. <sect2 id="security-authorization-taglib">
  1818. <title>Authorization-Related Tag Libraries</title>
  1819. <para>The Acegi Security System for Spring comes bundled with several
  1820. JSP tag libraries that eases JSP writing. The tag libraries are known
  1821. as <literal>authz</literal> and provide a range of different
  1822. services.</para>
  1823. <para>All taglib classes are included in the core
  1824. <literal>acegi-security-xx.jar</literal> file, with the
  1825. <literal>authz.tld</literal> located in the JAR's
  1826. <literal>META-INF</literal> directory. This means for JSP 1.2+ web
  1827. containers you can simply include the JAR in the WAR's
  1828. <literal>WEB-INF/lib</literal> directory and it will be available. If
  1829. you're using a JSP 1.1 container, you'll need to declare the JSP
  1830. taglib in your <literal>web.xml file</literal>, and include
  1831. <literal>authz.tld</literal> in the <literal>WEB-INF/lib</literal>
  1832. directory. The following fragment is added to
  1833. <literal>web.xml</literal>:</para>
  1834. <para><programlisting>&lt;taglib&gt;
  1835. &lt;taglib-uri&gt;http://acegisecurity.sf.net/authz&lt;/taglib-uri&gt;
  1836. &lt;taglib-location&gt;/WEB-INF/authz.tld&lt;/taglib-location&gt;
  1837. &lt;/taglib&gt;</programlisting></para>
  1838. <sect3>
  1839. <title>AuthorizeTag</title>
  1840. <para><literal>AuthorizeTag</literal> is used to include content if
  1841. the current principal holds certain
  1842. <literal>GrantedAuthority</literal>s.</para>
  1843. <para>The following JSP fragment illustrates how to use the
  1844. <literal>AuthorizeTag</literal>:</para>
  1845. <para><programlisting>&lt;authz:authorize ifAllGranted="ROLE_SUPERVISOR"&gt;
  1846. &lt;td&gt;
  1847. &lt;A HREF="del.htm?id=&lt;c:out value="${contact.id}"/&gt;"&gt;Del&lt;/A&gt;
  1848. &lt;/td&gt;
  1849. &lt;/authz:authorize&gt;</programlisting></para>
  1850. <para>This tag would cause the tag's body to be output if the
  1851. principal has been granted ROLE_SUPERVISOR.</para>
  1852. <para>The <literal>authz:authorize</literal> tag declares the
  1853. following attributes:</para>
  1854. <para><itemizedlist spacing="compact">
  1855. <listitem>
  1856. <para><literal>ifAllGranted</literal>: All the listed roles
  1857. must be granted for the tag to output its body.</para>
  1858. </listitem>
  1859. <listitem>
  1860. <para><literal>ifAnyGranted</literal>: Any of the listed roles
  1861. must be granted for the tag to output its body.</para>
  1862. </listitem>
  1863. <listitem>
  1864. <para><literal>ifNotGranted</literal>: None of the listed
  1865. roles must be granted for the tag to output its body.</para>
  1866. </listitem>
  1867. </itemizedlist></para>
  1868. <para>You'll note that in each attribute you can list multiple
  1869. roles. Simply separate the roles using a comma. The
  1870. <literal>authorize</literal> tag ignores whitespace in
  1871. attributes.</para>
  1872. <para>The tag library logically ANDs all of it's parameters
  1873. together. This means that if you combine two or more attributes, all
  1874. attributes must be true for the tag to output it's body. Don't add
  1875. an <literal>ifAllGranted="ROLE_SUPERVISOR"</literal>, followed by an
  1876. <literal>ifNotGranted="ROLE_SUPERVISOR"</literal>, or you'll be
  1877. surprised to never see the tag's body.</para>
  1878. <para>By requiring all attributes to return true, the authorize tag
  1879. allows you to create more complex authorization scenarios. For
  1880. example, you could declare an
  1881. <literal>ifAllGranted="ROLE_SUPERVISOR"</literal> and an
  1882. <literal>ifNotGranted="ROLE_NEWBIE_SUPERVISOR"</literal> in the same
  1883. tag, in order to prevent new supervisors from seeing the tag body.
  1884. However it would no doubt be simpler to use
  1885. <literal>ifAllGranted="ROLE_EXPERIENCED_SUPERVISOR"</literal> rather
  1886. than inserting NOT conditions into your design.</para>
  1887. <para>One last item: the tag verifies the authorizations in a
  1888. specific order: first <literal>ifNotGranted</literal>, then
  1889. <literal>ifAllGranted</literal>, and finally,
  1890. <literal>ifAnyGranted</literal>.</para>
  1891. </sect3>
  1892. <sect3>
  1893. <title>AuthenticationTag</title>
  1894. <para><literal>AuthenticationTag</literal> is used to simply output
  1895. a property of the current principal's
  1896. <literal>Authentication.getPrincipal()</literal> object to the web
  1897. page.</para>
  1898. <para>The following JSP fragment illustrates how to use the
  1899. <literal>AuthenticationTag</literal>:</para>
  1900. <para><programlisting>&lt;authz:authentication operation="username"/&gt;</programlisting></para>
  1901. <para>This tag would cause the principal's name to be output. Here
  1902. we are assuming the <literal>Authentication.getPrincipal()</literal>
  1903. is a <literal>UserDetails</literal> object, which is generally the
  1904. case when using the typical
  1905. <literal>DaoAuthenticationProvider</literal>.</para>
  1906. </sect3>
  1907. <sect3>
  1908. <title>AclTag</title>
  1909. <para><literal>AclTag</literal> is used to include content if the
  1910. current principal has a ACL to the indicated domain object.</para>
  1911. <para>The following JSP fragment illustrates how to use the
  1912. <literal>AclTag</literal>:</para>
  1913. <para><programlisting>&lt;authz:acl domainObject="${contact}" hasPermission="16,1"&gt;
  1914. &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;
  1915. &lt;/authz:acl&gt;</programlisting></para>
  1916. <para>This tag would cause the tag's body to be output if the
  1917. principal holds either permission 16 or permission 1 for the
  1918. "contact" domain object. The numbers are actually integers that are
  1919. used with <literal>AbstractBasicAclEntry</literal> bit masking.
  1920. Please refer to the ACL section of this reference guide to
  1921. understand more about the ACL capabilities of Acegi Security.</para>
  1922. </sect3>
  1923. </sect2>
  1924. <sect2 id="security-authorization-recommendations">
  1925. <title>Authorization Recommendations</title>
  1926. <para>Given there are several ways to achieve similar authorization
  1927. outcomes in the Acegi Security System for Spring, the following
  1928. general recommendations are made:</para>
  1929. <itemizedlist>
  1930. <listitem>
  1931. <para>Grant authorities using
  1932. <literal>GrantedAuthorityImpl</literal> where possible. Because it
  1933. is already supported by the Acegi Security System for Spring, you
  1934. avoid the need to create custom
  1935. <literal>AuthenticationManager</literal> or
  1936. <literal>AuthenticationProvider</literal> implementations simply
  1937. to populate the <literal>Authentication</literal> object with a
  1938. custom <literal>GrantedAuthority</literal>.</para>
  1939. </listitem>
  1940. <listitem>
  1941. <para>Writing an <literal>AccessDecisionVoter</literal>
  1942. implementation and using either <literal>ConsensusBased</literal>,
  1943. <literal>AffirmativeBased</literal> or
  1944. <literal>UnanimousBased</literal> as the
  1945. <literal>AccessDecisionManager</literal> may be the best approach
  1946. to implementing your custom access decision rules.</para>
  1947. </listitem>
  1948. </itemizedlist>
  1949. </sect2>
  1950. </sect1>
  1951. <sect1 id="afterinvocation">
  1952. <title>After Invocation Handling</title>
  1953. <sect2 id="afterinvocation-overview">
  1954. <title>Overview</title>
  1955. <para>Whilst the <literal>AccessDecisionManager</literal> is called by
  1956. the <literal>AbstractSecurityInterceptor</literal> before proceeding
  1957. with the secure object invocation, some applications need a way of
  1958. modifying the object actually returned by the secure object
  1959. invocation. Whilst you could easily implement your own AOP concern to
  1960. achieve this, Acegi Security provides a convenient hook that has
  1961. several concrete implementations that integrate with its ACL
  1962. capabilities.</para>
  1963. <para>Figure 5 illustrates Acegi Security's
  1964. <literal>AfterInvocationManager</literal> and its concrete
  1965. implementations.</para>
  1966. <para><mediaobject>
  1967. <imageobject role="html">
  1968. <imagedata align="center" fileref="images/AfterInvocation.gif"
  1969. format="GIF" />
  1970. </imageobject>
  1971. <caption>
  1972. <para>Figure 5: After Invocation Implementation</para>
  1973. </caption>
  1974. </mediaobject></para>
  1975. <para>Like many other parts of Acegi Security,
  1976. <literal>AfterInvocationManager</literal> has a single concrete
  1977. implementation, <literal>AfterInvocationProvider</literal>, which
  1978. polls a list of <literal>AfterInvocationProvider</literal>s. Each
  1979. <literal>AfterInvocationProvider</literal> is allowed to modify the
  1980. return object or throw an <literal>AccessDeniedException</literal>.
  1981. Indeed multiple providers can modify the object, as the result of the
  1982. previous provider is passed to the next in the list. Let's now
  1983. consider our ACL-aware implementations of
  1984. <literal>AfterInvocationProvider</literal>.</para>
  1985. <para>Please be aware that if you're using
  1986. <literal>AfterInvocationManager</literal>, you will still need
  1987. configuration attributes that allow the
  1988. <literal>MethodSecurityInterceptor</literal>'s
  1989. <literal>AccessDecisionManager</literal> to allow an operation. If
  1990. you're using the typical Acegi Security included
  1991. <literal>AccessDecisionManager</literal> implementations, having no
  1992. configuration attributes defined for a particular secure method
  1993. invocation will cause each <literal>AccessDecisionVoter</literal> to
  1994. abstain from voting. In turn, if the
  1995. <literal>AccessDecisionManager</literal> property
  1996. "<literal>allowIfAllAbstainDecisions</literal>" is
  1997. <literal>false</literal>, an <literal>AccessDeniedException</literal>
  1998. will be thrown. You may avoid this potential issue by either (i)
  1999. setting "<literal>allowIfAllAbstainDecisions</literal>" to
  2000. <literal>true</literal> (although this is generally not recommended)
  2001. or (ii) simply ensure that there is at least one configuration
  2002. attribute that an <literal>AccessDecisionVoter</literal> will vote to
  2003. grant access for. This latter (recommended) approach is usually
  2004. achieved through a <literal>ROLE_USER</literal> or
  2005. <literal>ROLE_AUTHENTICATED</literal> configuration attribute.</para>
  2006. </sect2>
  2007. <sect2 id="afterinvocation-acl-aware">
  2008. <title>ACL-Aware AfterInvocationProviders</title>
  2009. <para>A common services layer method we've all written at one stage or
  2010. another looks like this:</para>
  2011. <para><programlisting>public Contact getById(Integer id);</programlisting></para>
  2012. <para>Quite often, only principals with permission to read the
  2013. <literal>Contact</literal> should be allowed to obtain it. In this
  2014. situation the <literal>AccessDecisionManager</literal> approach
  2015. provided by the <literal>AbstractSecurityInterceptor</literal> will
  2016. not suffice. This is because the identity of the
  2017. <literal>Contact</literal> is all that is available before the secure
  2018. object is invoked. The
  2019. <literal>BasicAclAfterInvocationProvider</literal> delivers a
  2020. solution, and is configured as follows:</para>
  2021. <para><programlisting>&lt;bean id="afterAclRead" class="org.acegisecurity.afterinvocation.BasicAclEntryAfterInvocationProvider"&gt;
  2022. &lt;property name="aclManager"&gt;&lt;ref local="aclManager"/&gt;&lt;/property&gt;
  2023. &lt;property name="requirePermission"&gt;
  2024. &lt;list&gt;
  2025. &lt;ref local="org.acegisecurity.acl.basic.SimpleAclEntry.ADMINISTRATION"/&gt;
  2026. &lt;ref local="org.acegisecurity.acl.basic.SimpleAclEntry.READ"/&gt;
  2027. &lt;/list&gt;
  2028. &lt;/property&gt;
  2029. &lt;/bean&gt;</programlisting></para>
  2030. <para>In the above example, the <literal>Contact</literal> will be
  2031. retrieved and passed to the
  2032. <literal>BasicAclEntryAfterInvocationProvider</literal>. The provider
  2033. will thrown an <literal>AccessDeniedException</literal> if one of the
  2034. listed <literal>requirePermission</literal>s is not held by the
  2035. <literal>Authentication</literal>. The
  2036. <literal>BasicAclEntryAfterInvocationProvider</literal> queries the
  2037. <literal>AclManager</literal> to determine the ACL that applies for
  2038. this domain object to this <literal>Authentication</literal>.</para>
  2039. <para>Similar to the
  2040. <literal>BasicAclEntryAfterInvocationProvider</literal> is
  2041. <literal>BasicAclEntryAfterInvocationCollectionFilteringProvider</literal>.
  2042. It is designed to remove <literal>Collection</literal> or array
  2043. elements for which a principal does not have access. It never thrown
  2044. an <literal>AccessDeniedException</literal> - simply silently removes
  2045. the offending elements. The provider is configured as follows:</para>
  2046. <para><programlisting>&lt;bean id="afterAclCollectionRead" class="org.acegisecurity.afterinvocation.BasicAclEntryAfterInvocationCollectionFilteringProvider"&gt;
  2047. &lt;property name="aclManager"&gt;&lt;ref local="aclManager"/&gt;&lt;/property&gt;
  2048. &lt;property name="requirePermission"&gt;
  2049. &lt;list&gt;
  2050. &lt;ref local="org.acegisecurity.acl.basic.SimpleAclEntry.ADMINISTRATION"/&gt;
  2051. &lt;ref local="org.acegisecurity.acl.basic.SimpleAclEntry.READ"/&gt;
  2052. &lt;/list&gt;
  2053. &lt;/property&gt;
  2054. &lt;/bean&gt;</programlisting></para>
  2055. <para>As you can imagine, the returned <literal>Object</literal> must
  2056. be a <literal>Collection</literal> or array for this provider to
  2057. operate. It will remove any element if the
  2058. <literal>AclManager</literal> indicates the
  2059. <literal>Authentication</literal> does not hold one of the listed
  2060. <literal>requirePermission</literal>s.</para>
  2061. <para>The Contacts sample application demonstrates these two
  2062. <literal>AfterInvocationProvider</literal>s.</para>
  2063. </sect2>
  2064. </sect1>
  2065. <sect1 id="security-run-as">
  2066. <title>Run-As Authentication Replacement</title>
  2067. <sect2 id="security-run-as-purpose">
  2068. <title>Purpose</title>
  2069. <para>The <literal>AbstractSecurityInterceptor</literal> is able to
  2070. temporarily replace the <literal>Authentication</literal> object in
  2071. the <literal>SecurityContext</literal> and
  2072. <literal>SecurityContextHolder</literal> during the
  2073. <literal>SecurityInterceptorCallback</literal>. This only occurs if
  2074. the original <literal>Authentication</literal> object was successfully
  2075. processed by the <literal>AuthenticationManager</literal> and
  2076. <literal>AccessDecisionManager</literal>. The
  2077. <literal>RunAsManager</literal> will indicate the replacement
  2078. <literal>Authentication</literal> object, if any, that should be used
  2079. during the <literal>SecurityInterceptorCallback</literal>.</para>
  2080. <para>By temporarily replacing the <literal>Authentication</literal>
  2081. object during a <literal>SecurityInterceptorCallback</literal>, the
  2082. secured invocation will be able to call other objects which require
  2083. different authentication and authorization credentials. It will also
  2084. be able to perform any internal security checks for specific
  2085. <literal>GrantedAuthority</literal> objects. Because Acegi Security
  2086. provides a number of helper classes that automatically configure
  2087. remoting protocols based on the contents of the
  2088. <literal>ContextHolder</literal>, these run-as replacements are
  2089. particularly useful when calling remote web services.</para>
  2090. </sect2>
  2091. <sect2 id="security-run-as-usage">
  2092. <title>Usage</title>
  2093. <para>A <literal>RunAsManager</literal> interface is provided by the
  2094. Acegi Security System for Spring:</para>
  2095. <para><programlisting>public Authentication buildRunAs(Authentication authentication, Object object, ConfigAttributeDefinition config);
  2096. public boolean supports(ConfigAttribute attribute);
  2097. public boolean supports(Class clazz);</programlisting></para>
  2098. <para>The first method returns the <literal>Authentication</literal>
  2099. object that should replace the existing
  2100. <literal>Authentication</literal> object for the duration of the
  2101. method invocation. If the method returns <literal>null</literal>, it
  2102. indicates no replacement should be made. The second method is used by
  2103. the <literal>AbstractSecurityInterceptor</literal> as part of its
  2104. startup validation of configuration attributes. The
  2105. <literal>supports(Class)</literal> method is called by a security
  2106. interceptor implementation to ensure the configured
  2107. <literal>RunAsManager</literal> supports the type of secure object
  2108. that the security interceptor will present.</para>
  2109. <para>One concrete implementation of a <literal>RunAsManager</literal>
  2110. is provided with the Acegi Security System for Spring. The
  2111. <literal>RunAsManagerImpl</literal> class returns a replacement
  2112. <literal>RunAsUserToken</literal> if any
  2113. <literal>ConfigAttribute</literal> starts with
  2114. <literal>RUN_AS_</literal>. If any such
  2115. <literal>ConfigAttribute</literal> is found, the replacement
  2116. <literal>RunAsUserToken</literal> will contain the same principal,
  2117. credentials and granted authorities as the original
  2118. <literal>Authentication</literal> object, along with a new
  2119. <literal>GrantedAuthorityImpl</literal> for each
  2120. <literal>RUN_AS_</literal> <literal>ConfigAttribute</literal>. Each
  2121. new <literal>GrantedAuthorityImpl</literal> will be prefixed with
  2122. <literal>ROLE_</literal>, followed by the <literal>RUN_AS</literal>
  2123. <literal>ConfigAttribute</literal>. For example, a
  2124. <literal>RUN_AS_SERVER</literal> will result in the replacement
  2125. <literal>RunAsUserToken</literal> containing a
  2126. <literal>ROLE_RUN_AS_SERVER</literal> granted authority.</para>
  2127. <para>The replacement <literal>RunAsUserToken</literal> is just like
  2128. any other <literal>Authentication</literal> object. It needs to be
  2129. authenticated by the <literal>AuthenticationManager</literal>,
  2130. probably via delegation to a suitable
  2131. <literal>AuthenticationProvider</literal>. The
  2132. <literal>RunAsImplAuthenticationProvider</literal> performs such
  2133. authentication. It simply accepts as valid any
  2134. <literal>RunAsUserToken</literal> presented.</para>
  2135. <para>To ensure malicious code does not create a
  2136. <literal>RunAsUserToken</literal> and present it for guaranteed
  2137. acceptance by the <literal>RunAsImplAuthenticationProvider</literal>,
  2138. the hash of a key is stored in all generated tokens. The
  2139. <literal>RunAsManagerImpl</literal> and
  2140. <literal>RunAsImplAuthenticationProvider</literal> is created in the
  2141. bean context with the same key:</para>
  2142. <para><programlisting>&lt;bean id="runAsManager" class="org.acegisecurity.runas.RunAsManagerImpl"&gt;
  2143. &lt;property name="key"&gt;&lt;value&gt;my_run_as_password&lt;/value&gt;&lt;/property&gt;
  2144. &lt;/bean&gt;</programlisting><programlisting>&lt;bean id="runAsAuthenticationProvider" class="org.acegisecurity.runas.RunAsImplAuthenticationProvider"&gt;
  2145. &lt;property name="key"&gt;&lt;value&gt;my_run_as_password&lt;/value&gt;&lt;/property&gt;
  2146. &lt;/bean&gt;</programlisting></para>
  2147. <para>By using the same key, each <literal>RunAsUserToken</literal>
  2148. can be validated it was created by an approved
  2149. <literal>RunAsManagerImpl</literal>. The
  2150. <literal>RunAsUserToken</literal> is immutable after creation for
  2151. security reasons.</para>
  2152. </sect2>
  2153. </sect1>
  2154. <sect1 id="security-ui">
  2155. <title>User Interfacing with the SecurityContextHolder</title>
  2156. <sect2 id="security-ui-purpose">
  2157. <title>Purpose</title>
  2158. <para>Everything presented so far assumes one thing: the
  2159. <literal>SecurityContextHolder</literal> is populated with a valid
  2160. <literal>SecurityContext</literal>, which in turn contains a valid
  2161. <literal>Authentication</literal> object. Developers are free to do
  2162. this in whichever way they like, such as directly calling the relevant
  2163. objects at runtime. However, several classes have been provided to
  2164. make this process transparent in many situations. We call these
  2165. classes "authentication mechanisms".</para>
  2166. <para>The <literal>org.acegisecurity.ui</literal> package provides
  2167. what we call "authentication processing mechanisms". An authentication
  2168. processing mechanism is solely concerned with received an
  2169. authentication request from the principal, testing if it seems valid,
  2170. and if so, placing the authentication request token onto the
  2171. <literal>SecurityContextHolder</literal>. Of course, if the
  2172. authentication request is invalid, the authentication processing
  2173. mechanism is responsible for informing the principal in whatever way
  2174. is appropriate to the protocol.</para>
  2175. <para>Recall the
  2176. <literal>HttpSessionContextIntegrationFilter</literal> (discussed in
  2177. the context section) is responsible for storing the
  2178. <literal>SecurityContextHolder</literal> contents between invocations.
  2179. This means no authentication processing mechanism need ever interact
  2180. directly with <literal>HttpSession</literal>. Indeed
  2181. <literal>HttpSessionContextIntegrationFilter</literal> has been
  2182. designed to minimise the unnecessary creation of
  2183. <literal>HttpSession</literal>s, as might occur when using Basic
  2184. authentication for example.</para>
  2185. <para>There are several authentication processing mechanisms included
  2186. with Acegi Security, which will be briefly discussed in this chapter.
  2187. The most popular (and almost always recommended) approach is HTTP Form
  2188. Authentication, which uses a login form to authenticate the user.
  2189. Another approach (commonly use with web services) is HTTP Basic
  2190. Authentication, which allows clients to use HTTP headers to present
  2191. authentication information to the Acegi Security System for Spring.
  2192. Alternatively, you can also use Yale Central Authentication Service
  2193. (CAS) for enterprise-wide single sign on. The final (and generally
  2194. unrecommended) approach is via Container Adapters, which allow
  2195. supported web containers to perform the authentication themselves.
  2196. HTTP Form Authentication and Basic Authentication is discussed below,
  2197. whilst CAS and Container Adapters are discussed in separate sections
  2198. of this document.</para>
  2199. </sect2>
  2200. <sect2 id="security-ui-http-form">
  2201. <title>HTTP Form Authentication</title>
  2202. <para>HTTP Form Authentication involves using the
  2203. <literal>AuthenticationProcessingFilter</literal> to process a login
  2204. form. The login form simply contains <literal>j_username</literal> and
  2205. <literal>j_password</literal> input fields, and posts to a URL that is
  2206. monitored by the filter (by default
  2207. <literal>j_acegi_security_check</literal>). The filter is defined in
  2208. <literal>web.xml</literal> behind a
  2209. <literal>FilterToBeanProxy</literal> as follows:</para>
  2210. <para><programlisting>&lt;filter&gt;
  2211. &lt;filter-name&gt;Acegi Authentication Processing Filter&lt;/filter-name&gt;
  2212. &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  2213. &lt;init-param&gt;
  2214. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  2215. &lt;param-value&gt;org.acegisecurity.ui.webapp.AuthenticationProcessingFilter&lt;/param-value&gt;
  2216. &lt;/init-param&gt;
  2217. &lt;/filter&gt;
  2218. &lt;filter-mapping&gt;
  2219. &lt;filter-name&gt;Acegi Authentication Processing Filter&lt;/filter-name&gt;
  2220. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  2221. &lt;/filter-mapping&gt;</programlisting></para>
  2222. <para>For a discussion of <literal>FilterToBeanProxy</literal>, please
  2223. refer to the Filters section. The application context will need to
  2224. define the <literal>AuthenticationProcessingFilter</literal>:</para>
  2225. <para><programlisting>&lt;bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter"&gt;
  2226. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  2227. &lt;property name="authenticationFailureUrl"&gt;&lt;value&gt;/acegilogin.jsp?login_error=1&lt;/value&gt;&lt;/property&gt;
  2228. &lt;property name="defaultTargetUrl"&gt;&lt;value&gt;/&lt;/value&gt;&lt;/property&gt;
  2229. &lt;property name="filterProcessesUrl"&gt;&lt;value&gt;/j_acegi_security_check&lt;/value&gt;&lt;/property&gt;
  2230. &lt;/bean&gt;</programlisting></para>
  2231. <para>The configured <literal>AuthenticationManager</literal>
  2232. processes each authentication request. If authentication fails, the
  2233. browser will be redirected to the
  2234. <literal>authenticationFailureUrl</literal>. The
  2235. <literal>AuthenticationException</literal> will be placed into the
  2236. <literal>HttpSession</literal> attribute indicated by
  2237. <literal>AbstractProcessingFilter.ACEGI_SECURITY_LAST_EXCEPTION_KEY</literal>,
  2238. enabling a reason to be provided to the user on the error page.</para>
  2239. <para>If authentication is successful, the resulting
  2240. <literal>Authentication</literal> object will be placed into the
  2241. <literal>SecurityContextHolder</literal>.</para>
  2242. <para>Once the <literal>SecurityContextHolder</literal> has been
  2243. updated, the browser will need to be redirected to the target URL. The
  2244. target URL is usually indicated by the <literal>HttpSession</literal>
  2245. attribute specified by
  2246. <literal>AbstractProcessingFilter.ACEGI_SECURITY_TARGET_URL_KEY</literal>.
  2247. This attribute is automatically set by the
  2248. <literal>SecurityEnforcementFilter</literal> when an
  2249. <literal>AuthenticationException</literal> occurs, so that after login
  2250. is completed the user can return to what they were trying to access.
  2251. If for some reason the <literal>HttpSession</literal> does not
  2252. indicate the target URL, the browser will be redirected to the
  2253. <literal>defaultTargetUrl</literal> property.</para>
  2254. <para>Because this authentication approach is fully contained within a
  2255. single web application, HTTP Form Authentication is recommended to be
  2256. used instead of Container Adapters.</para>
  2257. </sect2>
  2258. <sect2 id="security-ui-http-basic">
  2259. <title>HTTP Basic Authentication</title>
  2260. <para>The Acegi Security System for Spring provides a
  2261. <literal>BasicProcessingFilter</literal> which is capable of
  2262. processing basic authentication credentials presented in HTTP headers.
  2263. This can be used for authenticating calls made by Spring remoting
  2264. protocols (such as Hessian and Burlap), as well as normal user agents
  2265. (such as Internet Explorer and Navigator). The standard governing HTTP
  2266. Basic Authentication is defined by RFC 1945, Section 11, and the
  2267. <literal>BasicProcessingFilter</literal> conforms with this RFC. Basic
  2268. Authentication is an attractive approach to authentication, because it
  2269. is very widely deployed in user agents and implementation is extremely
  2270. simple (it's just a Base64 encoding of the username:password,
  2271. specified in a HTTP header).</para>
  2272. <para>To implement HTTP Basic Authentication, it is necessary to
  2273. define <literal>BasicProcessingFilter</literal> in the fitler chain.
  2274. The application context will need to define the
  2275. <literal>BasicProcessingFilter</literal> and its required
  2276. collaborator:</para>
  2277. <para><programlisting>&lt;bean id="basicProcessingFilter" class="org.acegisecurity.ui.basicauth.BasicProcessingFilter"&gt;
  2278. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  2279. &lt;property name="authenticationEntryPoint"&gt;&lt;ref bean="authenticationEntryPoint"/&gt;&lt;/property&gt;
  2280. &lt;/bean&gt;
  2281. &lt;bean id="authenticationEntryPoint" class="org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint"&gt;
  2282. &lt;property name="realmName"&gt;&lt;value&gt;Name Of Your Realm&lt;/value&gt;&lt;/property&gt;
  2283. &lt;/bean&gt;</programlisting></para>
  2284. <para>The configured <literal>AuthenticationManager</literal>
  2285. processes each authentication request. If authentication fails, the
  2286. configured <literal>AuthenticationEntryPoint</literal> will be used to
  2287. retry the authentication process. Usually you will use the
  2288. <literal>BasicProcessingFilterEntryPoint</literal>, which returns a
  2289. 401 response with a suitable header to retry HTTP Basic
  2290. authentication. If authentication is successful, the resulting
  2291. <literal>Authentication</literal> object will be placed into the
  2292. <literal>SecurityContextHolder</literal>.</para>
  2293. <para>If the authentication event was successful, or authentication
  2294. was not attempted because the HTTP header did not contain a supported
  2295. authentication request, the filter chain will continue as normal. The
  2296. only time the filter chain will be interrupted is if authentication
  2297. fails and the <literal>AuthenticationEntryPoint</literal> is called,
  2298. as discussed in the previous paragraph.</para>
  2299. </sect2>
  2300. <sect2 id="security-ui-http-digest">
  2301. <title>HTTP Digest Authentication</title>
  2302. <para>The Acegi Security System for Spring provides a
  2303. <literal>DigestProcessingFilter</literal> which is capable of
  2304. processing digest authentication credentials presented in HTTP
  2305. headers. Digest Authentication attempts to solve many of the
  2306. weakenesses of Basic authentication, specifically by ensuring
  2307. credentials are never sent in clear text across the wire. Many user
  2308. agents support Digest Authentication, including FireFox and Internet
  2309. Explorer. The standard governing HTTP Digest Authentication is defined
  2310. by RFC 2617, which updates an earlier version of the Digest
  2311. Authentication standard prescribed by RFC 2069. Most user agents
  2312. implement RFC 2617. The Acegi Security
  2313. <literal>DigestProcessingFilter</literal> is compatible with the
  2314. "<literal>auth</literal>" quality of protection
  2315. (<literal>qop</literal>) prescribed by RFC 2617, which also provides
  2316. backward compatibility with RFC 2069. Digest Authentication is a
  2317. highly attractive option if you need to use unencrypted HTTP (ie no
  2318. TLS/HTTPS) and wish to maximise security of the authentication
  2319. process. Indeed Digest Authentication is a mandatory requirement for
  2320. the WebDAV protocol, as noted by RFC 2518 Section 17.1, so we should
  2321. expect to see it increasingly deployed and replacing Basic
  2322. Authentication.</para>
  2323. <para>Digest Authentication is definitely the most secure choice
  2324. between Form Authentication, Basic Authentication and Digest
  2325. Authentication, although extra security also means more complex user
  2326. agent implementations. Central to Digest Authentication is a "nonce".
  2327. This is a value the server generates. Acegi Security's nonce adopts
  2328. the following format:</para>
  2329. <para><programlisting>base64(expirationTime + ":" + md5Hex(expirationTime + ":" + key))
  2330. expirationTime: The date and time when the nonce expires, expressed in milliseconds
  2331. key: A private key to prevent modification of the nonce token
  2332. </programlisting></para>
  2333. <para>The <literal>DigestProcessingFilterEntryPoint</literal> has a
  2334. property specifying the <literal>key</literal> used for generating the
  2335. nonce tokens, along with a <literal>nonceValiditySeconds</literal>
  2336. property for determining the expiration time (default 300, which
  2337. equals five minutes). Whilstever the nonce is valid, the digest is
  2338. computed by concatenating various strings including the username,
  2339. password, nonce, URI being requested, a client-generated nonce (merely
  2340. a random value which the user agent generates each request), the realm
  2341. name etc, then performing an MD5 hash. Both the server and user agent
  2342. perform this digest computation, resulting in different hash codes if
  2343. they disagree on an included value (eg password). In the Acegi
  2344. Security implementation, if the server-generated nonce has merely
  2345. expired (but the digest was otherwise valid), the
  2346. <literal>DigestProcessingFilterEntryPoint</literal> will send a
  2347. <literal>"stale=true"</literal> header. This tells the user agent
  2348. there is no need to disturb the user (as the password and username etc
  2349. is correct), but simply to try again using a new nonce.</para>
  2350. <para>An appropriate value for
  2351. <literal>DigestProcessingFilterEntryPoint</literal>'s
  2352. <literal>nonceValiditySeconds</literal> parameter will depend on your
  2353. application. Extremely secure applications should note that an
  2354. intercepted authentication header can be used to impersonate the
  2355. principal until the <literal>expirationTime</literal> contained in the
  2356. nonce is reached. This is the key principle when selecting an
  2357. appropriate setting, but it would be unusual for immensly secure
  2358. applications to not be running over TLS/HTTPS in the first
  2359. instance.</para>
  2360. <para>Because of the more complex implementation of Digest
  2361. Authentication, there are often user agent issues. For example,
  2362. Internet Explorer fails to present an "<literal>opaque</literal>"
  2363. token on subsequent requests in the same session. The Acegi Security
  2364. filters therefore encapsulate all state information into the
  2365. "<literal>nonce</literal>" token instead. In our testing, the Acegi
  2366. Security implementation works reliably with FireFox and Internet
  2367. Explorer, correctly handling nonce timeouts etc.</para>
  2368. <para>Now that we've reviewed the theory, let's see how to use it. To
  2369. implement HTTP Digest Authentication, it is necessary to define
  2370. <literal>DigestProcessingFilter</literal> in the fitler chain. The
  2371. application context will need to define the
  2372. <literal>DigestProcessingFilter</literal> and its required
  2373. collaborators:</para>
  2374. <para><programlisting>&lt;bean id="digestProcessingFilter" class="org.acegisecurity.ui.digestauth.DigestProcessingFilter"&gt;
  2375. &lt;property name="userDetailsService"&gt;&lt;ref local="jdbcDaoImpl"/&gt;&lt;/property&gt;
  2376. &lt;property name="authenticationEntryPoint"&gt;&lt;ref local="digestProcessingFilterEntryPoint"/&gt;&lt;/property&gt;
  2377. &lt;property name="userCache"&gt;&lt;ref local="userCache"/&gt;&lt;/property&gt;
  2378. &lt;/bean&gt;
  2379. &lt;bean id="digestProcessingFilterEntryPoint" class="org.acegisecurity.ui.digestauth.DigestProcessingFilterEntryPoint"&gt;
  2380. &lt;property name="realmName"&gt;&lt;value&gt;Contacts Realm via Digest Authentication&lt;/value&gt;&lt;/property&gt;
  2381. &lt;property name="key"&gt;&lt;value&gt;acegi&lt;/value&gt;&lt;/property&gt;
  2382. &lt;property name="nonceValiditySeconds"&gt;&lt;value&gt;10&lt;/value&gt;&lt;/property&gt;
  2383. &lt;/bean&gt;</programlisting></para>
  2384. <para>The configured <literal>UserDetailsService</literal> is needed
  2385. because <literal>DigestProcessingFilter</literal> must have direct
  2386. access to the clear text password of a user. Digest Authentication
  2387. will NOT work if you are using encoded passwords in your DAO. The DAO
  2388. collaborator, along with the <literal>UserCache</literal>, are
  2389. typically shared directly with a
  2390. <literal>DaoAuthenticationProvider</literal>. The
  2391. <literal>authenticationEntryPoint</literal> property must be
  2392. <literal>DigestProcessingFilterEntryPoint</literal>, so that
  2393. <literal>DigestProcessingFilter</literal> can obtain the correct
  2394. <literal>realmName</literal> and <literal>key</literal> for digest
  2395. calculations.</para>
  2396. <para>Like <literal>BasicAuthenticationFilter</literal>, if
  2397. authentication is successful an <literal>Authentication</literal>
  2398. request token will be placed into the
  2399. <literal>SecurityContextHolder</literal>. If the authentication event
  2400. was successful, or authentication was not attempted because the HTTP
  2401. header did not contain a Digest Authentication request, the filter
  2402. chain will continue as normal. The only time the filter chain will be
  2403. interrupted is if authentication fails and the
  2404. <literal>AuthenticationEntryPoint</literal> is called, as discussed in
  2405. the previous paragraph.</para>
  2406. <para>Digest Authentication's RFC offers a range of additional
  2407. features to further increase security. For example, the nonce can be
  2408. changed on every request. Despite this, the Acegi Security
  2409. implementation was designed to minimise the complexity of the
  2410. implementation (and the doubtless user agent incompatibilities that
  2411. would emerge), and avoid needing to store server-side state. You are
  2412. invited to review RFC 2617 if you wish to explore these features in
  2413. more detail. As far as we are aware, the Acegi Security implementation
  2414. does comply with the minimum standards of this RFC.</para>
  2415. </sect2>
  2416. <sect2 id="security-ui-anonymous">
  2417. <title>Anonymous Authentication</title>
  2418. <para>Particularly in the case of web request URI security, sometimes
  2419. it is more convenient to assign configuration attributes against every
  2420. possible secure object invocation. Put differently, sometimes it is
  2421. nice to say <literal>ROLE_SOMETHING</literal> is required by default
  2422. and only allow certain exceptions to this rule, such as for login,
  2423. logout and home pages of an application. There are also other
  2424. situations where anonymous authentication would be desired, such as
  2425. when an auditing interceptor queries the
  2426. <literal>SecurityContextHolder</literal> to identify which principal
  2427. was responsible for a given operation. Such classes can be authored
  2428. with more robustness if they know the
  2429. <literal>SecurityContextHolder</literal> always contains an
  2430. <literal>Authentication</literal> object, and never
  2431. <literal>null</literal>.</para>
  2432. <para>Acegi Security provides three classes that together provide an
  2433. anoymous authentication feature.
  2434. <literal>AnonymousAuthenticationToken</literal> is an implementation
  2435. of <literal>Authentication</literal>, and stores the
  2436. <literal>GrantedAuthority</literal>[]s which apply to the anonymous
  2437. principal. There is a corresponding
  2438. <literal>AnonymousAuthenticationProvider</literal>, which is chained
  2439. into the <literal>ProviderManager</literal> so that
  2440. <literal>AnonymousAuthenticationTokens</literal> are accepted.
  2441. Finally, there is an AnonymousProcessingFilter, which is chained after
  2442. the normal authentication mechanisms and automatically add an
  2443. <literal>AnonymousAuthenticationToken</literal> to the
  2444. <literal>SecurityContextHolder</literal> if there is no existing
  2445. <literal>Authentication</literal> held there. The definition of the
  2446. filter and authentication provider appears as follows:</para>
  2447. <para><programlisting>&lt;bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter"&gt;
  2448. &lt;property name="key"&gt;&lt;value&gt;foobar&lt;/value&gt;&lt;/property&gt;
  2449. &lt;property name="userAttribute"&gt;&lt;value&gt;anonymousUser,ROLE_ANONYMOUS&lt;/value&gt;&lt;/property&gt;
  2450. &lt;/bean&gt;
  2451. &lt;bean id="anonymousAuthenticationProvider" class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider"&gt;
  2452. &lt;property name="key"&gt;&lt;value&gt;foobar&lt;/value&gt;&lt;/property&gt;
  2453. &lt;/bean&gt;</programlisting></para>
  2454. <para>The <literal>key</literal> is shared between the filter and
  2455. authentication provider, so that tokens created by the former are
  2456. accepted by the latter. The <literal>userAttribute</literal> is
  2457. expressed in the form of
  2458. <literal>usernameInTheAuthenticationToken,grantedAuthority[,grantedAuthority]</literal>.
  2459. This is the same syntax as used after the equals sign for
  2460. <literal>InMemoryDaoImpl</literal>'s <literal>userMap</literal>
  2461. property.</para>
  2462. <para>As explained earlier, the benefit of anonymous authentication is
  2463. that all URI patterns can have security applied to them. For
  2464. example:</para>
  2465. <para><programlisting>&lt;bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor"&gt;
  2466. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  2467. &lt;property name="accessDecisionManager"&gt;&lt;ref local="httpRequestAccessDecisionManager"/&gt;&lt;/property&gt;
  2468. &lt;property name="objectDefinitionSource"&gt;
  2469. &lt;value&gt;
  2470. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  2471. PATTERN_TYPE_APACHE_ANT
  2472. /index.jsp=ROLE_ANONYMOUS,ROLE_USER
  2473. /hello.htm=ROLE_ANONYMOUS,ROLE_USER
  2474. /logoff.jsp=ROLE_ANONYMOUS,ROLE_USER
  2475. /acegilogin.jsp*=ROLE_ANONYMOUS,ROLE_USER
  2476. /**=ROLE_USER
  2477. &lt;/value&gt;
  2478. &lt;/property&gt;
  2479. &lt;/bean&gt;</programlisting>Rounding out the anonymous authentication
  2480. discussion is the <literal>AuthenticationTrustResolver</literal>
  2481. interface, with its corresponding
  2482. <literal>AuthenticationTrustResolverImpl</literal> implementation.
  2483. This interface provides an
  2484. <literal>isAnonymous(Authentication)</literal> method, which allows
  2485. interested classes to take into account this special type of
  2486. authentication status. The
  2487. <literal>SecurityEnforcementFilter</literal> uses this interface in
  2488. processing <literal>AccessDeniedException</literal>s. If an
  2489. <literal>AccessDeniedException</literal> is thrown, and the
  2490. authentication is of an anonymous type, instead of throwing a 403
  2491. (forbidden) response, the filter will instead commence the
  2492. <literal>AuthenticationEntryPoint</literal> so the principal can
  2493. authenticate properly. This is a necessary distinction, otherwise
  2494. principals would always be deemed "authenticated" and never be given
  2495. an opportunity to login via form, basic, digest or some other normal
  2496. authentication mechanism.</para>
  2497. </sect2>
  2498. <sect2 id="security-ui-remember-me">
  2499. <title>Remember-Me Authentication</title>
  2500. <para>Remember-me authentication refers to web sites being able to
  2501. remember the identity of a principal between sessions. This is
  2502. typically accomplished by sending a cookie to the browser, with the
  2503. cookie being detected during future sessions and causing automated
  2504. login to take place. Acegi Security provides the necessary hooks so
  2505. that such operations can take place, along with providing a concrete
  2506. implementation that uses hashing to preserve the security of
  2507. cookie-based tokens.</para>
  2508. <para>Remember-me authentication is not used with digest or basic
  2509. authentication, given they are often not used with
  2510. <literal>HttpSession</literal>s. Remember-me is used with
  2511. <literal>AuthenticationProcessingFilter</literal>, and is implemented
  2512. via hooks in the <literal>AbstractProcessingFilter</literal>
  2513. superclass. The hooks will invoke a concrete
  2514. <literal>RememberMeServices</literal> at the appropriate times. The
  2515. interface looks like this:</para>
  2516. <para><programlisting>public Authentication autoLogin(HttpServletRequest request, HttpServletResponse response);
  2517. public void loginFail(HttpServletRequest request, HttpServletResponse response);
  2518. public void loginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication);</programlisting></para>
  2519. <para>Please refer to JavaDocs for a fuller discussion on what the
  2520. methods do, although note at this stage
  2521. <literal>AbstractProcessingFilter</literal> only calls the
  2522. <literal>loginFail()</literal> and <literal>loginSuccess()</literal>
  2523. methods. The <literal>autoLogin()</literal> method is called by
  2524. <literal>RememberMeProcessingFilter</literal> whenever the
  2525. <literal>SecurityContextHolder</literal> does not contain an
  2526. <literal>Authentication</literal>. This interface therefore provides
  2527. the underlaying remember-me implementation with sufficient
  2528. notification of authentication-related events, and delegates to the
  2529. implementation whenever a candidate web request might contain a cookie
  2530. and wish to be remembered.</para>
  2531. <para>This design allows any number of remember-me implementation
  2532. strategies. In the interests of simplicity and avoiding the need for
  2533. DAO implementations that specify write and create methods, Acegi
  2534. Security's only concrete implementation,
  2535. <literal>TokenBasedRememberMeServices</literal>, uses hashing to
  2536. achieve a useful remember-me strategy. In essence a cookie is sent to
  2537. the browser upon successful interactive authentication, with that
  2538. cookie being composed as follows:</para>
  2539. <para><programlisting>base64(username + ":" + expirationTime + ":" + md5Hex(username + ":" + expirationTime + ":" password + ":" + key))
  2540. username: As identifiable to TokenBasedRememberMeServices.getUserDetailsService()
  2541. password: That matches the relevant UserDetails retrieved from TokenBasedRememberMeServices.getUserDetailsService()
  2542. expirationTime: The date and time when the remember-me token expires, expressed in milliseconds
  2543. key: A private key to prevent modification of the remember-me token
  2544. </programlisting></para>
  2545. <para>As such the remember-me token is valid only for the period
  2546. specified, and provided that the username, password and key does not
  2547. change. Notably, this has a potential security issue issue in that a
  2548. captured remember-me token will be usable from any user agent until
  2549. such time as the token expires. This is the same issue as with digest
  2550. authentication. If a principal is aware a token has been captured,
  2551. they can easily change their password and immediately invalidate all
  2552. remember-me tokens on issue. However, if more significant security is
  2553. needed a rolling token approach should be used (this would require a
  2554. database) or remember-me services should simply not be used.</para>
  2555. <para><literal>TokenBasedRememberMeServices</literal> generates a
  2556. <literal>RememberMeAuthenticationToken</literal>, which is processed
  2557. by <literal>RememberMeAuthenticationProvider</literal>. A
  2558. <literal>key</literal> is shared between this authentication provider
  2559. and the <literal>TokenBasedRememberMeServices</literal>. In addition,
  2560. <literal>TokenBasedRememberMeServices</literal> requires A
  2561. UserDetailsService from which it can retrieve the username and
  2562. password for signature comparison purposes, and generate the
  2563. <literal>RememberMeAuthenticationToken</literal> to contain the
  2564. correct <literal>GrantedAuthority</literal>[]s. Some sort of logout
  2565. command should be provided by the application (typically via a JSP)
  2566. that invalidates the cookie upon user request. See the Contacts Sample
  2567. application's <literal>logout.jsp</literal> for an example.</para>
  2568. <para>The beans required in an application context to enable
  2569. remember-me services are as follows:</para>
  2570. <para><programlisting>&lt;bean id="rememberMeProcessingFilter" class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter"&gt;
  2571. &lt;property name="rememberMeServices"&gt;&lt;ref local="rememberMeServices"/&gt;&lt;/property&gt;
  2572. &lt;/bean&gt;
  2573. &lt;bean id="rememberMeServices" class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices"&gt;
  2574. &lt;property name="userDetailsService"&gt;&lt;ref local="jdbcDaoImpl"/&gt;&lt;/property&gt;
  2575. &lt;property name="key"&gt;&lt;value&gt;springRocks&lt;/value&gt;&lt;/property&gt;
  2576. &lt;/bean&gt;
  2577. &lt;bean id="rememberMeAuthenticationProvider" class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider"&gt;
  2578. &lt;property name="key"&gt;&lt;value&gt;springRocks&lt;/value&gt;&lt;/property&gt;
  2579. &lt;/bean&gt;</programlisting>Don't forget to add your
  2580. <literal>RememberMeServices</literal> implementation to your
  2581. <literal>AuthenticationProcessingFilter.setRememberMeServices()</literal>
  2582. property, include the
  2583. <literal>RememberMeAuthenticationProvider</literal> in your
  2584. <literal>AuthenticationManager.setProviders()</literal> list, and add
  2585. a call to <literal>RememberMeProcessingFilter</literal> into your
  2586. <literal>FilterChainProxy</literal> (typically immediately after your
  2587. <literal>AuthenticationProcessingFilter</literal>).</para>
  2588. </sect2>
  2589. <sect2 id="security-ui-well-known">
  2590. <title>Well-Known Locations</title>
  2591. <para>Prior to release 0.8.0, Acegi Security referred to "well-known
  2592. locations" in discussions about storing the
  2593. <literal>Authentication</literal>. This approach did not explicitly
  2594. separate the function of <literal>HttpSession</literal> storage of
  2595. <literal>SecurityContextHolder</literal> contents from the processing
  2596. of authentication requests received through various protocols. In
  2597. addition, the previous approach did not facilitate storage of
  2598. non-<literal>Authentication</literal> objects between requests, which
  2599. was limiting usefulness of the
  2600. <literal>SecurityContextHolder</literal> system to member of the
  2601. community. For these reasons, the notion of well-known locations was
  2602. abandoned, the <literal>HttpSessionContextIntegrationFilter</literal>
  2603. was established, and the purpose of authentication processing
  2604. mechanisms was explicitly defined and limited to interaction with the
  2605. <literal>SecurityContextHolder</literal> only. There is no need to
  2606. refer to well-known locations any more and we hope this clearer
  2607. separation of responsibilities enhances understanding of the
  2608. design.</para>
  2609. </sect2>
  2610. </sect1>
  2611. <sect1 id="security-container-adapters">
  2612. <title>Container Adapters</title>
  2613. <sect2 id="security-container-adapters-overview">
  2614. <title>Overview</title>
  2615. <para>Very early versions of the Acegi Security System for Spring
  2616. exclusively used Container Adapters for interfacing authentication
  2617. with end users. Whilst this worked well, it required considerable time
  2618. to support multiple container versions and the configuration itself
  2619. was relatively time-consuming for developers. For this reason the HTTP
  2620. Form Authentication and HTTP Basic Authentication approaches were
  2621. developed, and are today recommended for almost all
  2622. applications.</para>
  2623. <para>Container Adapters enable the Acegi Security System for Spring
  2624. to integrate directly with the containers used to host end user
  2625. applications. This integration means that applications can continue to
  2626. leverage the authentication and authorization capabilities built into
  2627. containers (such as <literal>isUserInRole()</literal> and form-based
  2628. or basic authentication), whilst benefiting from the enhanced security
  2629. interception capabilities provided by the Acegi Security System for
  2630. Spring (it should be noted that Acegi Security also offers
  2631. <literal>ContextHolderAwareRequestWrapper</literal> to deliver
  2632. <literal>isUserInRole()</literal> and similar Servlet Specification
  2633. compatibility methods).</para>
  2634. <para>The integration between a container and the Acegi Security
  2635. System for Spring is achieved through an adapter. The adapter provides
  2636. a container-compatible user authentication provider, and needs to
  2637. return a container-compatible user object.</para>
  2638. <para>The adapter is instantiated by the container and is defined in a
  2639. container-specific configuration file. The adapter then loads a Spring
  2640. application context which defines the normal authentication manager
  2641. settings, such as the authentication providers that can be used to
  2642. authenticate the request. The application context is usually named
  2643. <literal>acegisecurity.xml</literal> and is placed in a
  2644. container-specific location.</para>
  2645. <para>The Acegi Security System for Spring currently supports Jetty,
  2646. Catalina (Tomcat), JBoss and Resin. Additional container adapters can
  2647. easily be written.</para>
  2648. </sect2>
  2649. <sect2 id="security-container-adapters-adapter-provider">
  2650. <title>Adapter Authentication Provider</title>
  2651. <para>As is always the case, the container adapter generated
  2652. <literal>Authentication</literal> object still needs to be
  2653. authenticated by an <literal>AuthenticationManager</literal> when
  2654. requested to do so by the
  2655. <literal>AbstractSecurityInterceptor</literal>. The
  2656. <literal>AuthenticationManager</literal> needs to be certain the
  2657. adapter-provided <literal>Authentication</literal> object is valid and
  2658. was actually authenticated by a trusted adapter.</para>
  2659. <para>Adapters create <literal>Authentication</literal> objects which
  2660. are immutable and implement the <literal>AuthByAdapter</literal>
  2661. interface. These objects store the hash of a key that is defined by
  2662. the adapter. This allows the <literal>Authentication</literal> object
  2663. to be validated by the <literal>AuthByAdapterProvider</literal>. This
  2664. authentication provider is defined as follows:</para>
  2665. <para><programlisting>&lt;bean id="authByAdapterProvider" class="org.acegisecurity.adapters.AuthByAdapterProvider"&gt;
  2666. &lt;property name="key"&gt;&lt;value&gt;my_password&lt;/value&gt;&lt;/property&gt;
  2667. &lt;/bean&gt;</programlisting></para>
  2668. <para>The key must match the key that is defined in the
  2669. container-specific configuration file that starts the adapter. The
  2670. <literal>AuthByAdapterProvider</literal> automatically accepts as
  2671. valid any <literal>AuthByAdapter</literal> implementation that returns
  2672. the expected hash of the key.</para>
  2673. <para>To reiterate, this means the adapter will perform the initial
  2674. authentication using providers such as
  2675. <literal>DaoAuthenticationProvider</literal>, returning an
  2676. <literal>AuthByAdapter</literal> instance that contains a hash code of
  2677. the key. Later, when an application calls a security interceptor
  2678. managed resource, the <literal>AuthByAdapter</literal> instance in the
  2679. <literal>SecurityContext</literal> in the
  2680. <literal>SecurityContextHolder</literal> will be tested by the
  2681. application's <literal>AuthByAdapterProvider</literal>. There is no
  2682. requirement for additional authentication providers such as
  2683. <literal>DaoAuthenticationProvider</literal> within the
  2684. application-specific application context, as the only type of
  2685. <literal>Authentication</literal> instance that will be presented by
  2686. the application is from the container adapter.</para>
  2687. <para>Classloader issues are frequent with containers and the use of
  2688. container adapters illustrates this further. Each container requires a
  2689. very specific configuration. The installation instructions are
  2690. provided below. Once installed, please take the time to try the sample
  2691. application to ensure your container adapter is properly
  2692. configured.</para>
  2693. <para>When using container adapters with the
  2694. <literal>DaoAuthenticationProvider</literal>, ensure you set its
  2695. <literal>forcePrincipalAsString</literal> property to
  2696. <literal>true</literal>.</para>
  2697. </sect2>
  2698. <sect2 id="security-container-adapters-catalina">
  2699. <title>Catalina (Tomcat) Installation</title>
  2700. <para>The following was tested with Jakarta Tomcat 4.1.30 and
  2701. 5.0.19.</para>
  2702. <para><literal>$CATALINA_HOME</literal> refers to the root of your
  2703. Catalina (Tomcat) installation.</para>
  2704. <para>Edit your <literal>$CATALINA_HOME/conf/server.xml</literal> file
  2705. so the <literal>&lt;Engine&gt;</literal> section contains only one
  2706. active <literal>&lt;Realm&gt;</literal> entry. An example realm
  2707. entry:</para>
  2708. <para><programlisting> &lt;Realm className="org.acegisecurity.adapters.catalina.CatalinaAcegiUserRealm"
  2709. appContextLocation="conf/acegisecurity.xml"
  2710. key="my_password" /&gt;</programlisting></para>
  2711. <para>Be sure to remove any other <literal>&lt;Realm&gt;</literal>
  2712. entry from your <literal>&lt;Engine&gt;</literal> section.</para>
  2713. <para>Copy <literal>acegisecurity.xml</literal> into
  2714. <literal>$CATALINA_HOME/conf</literal>.</para>
  2715. <para>Copy <literal>acegi-security-catalina-XX.jar</literal> into
  2716. <literal>$CATALINA_HOME/server/lib</literal>.</para>
  2717. <para>Copy the following files into
  2718. <literal>$CATALINA_HOME/common/lib</literal>:</para>
  2719. <itemizedlist>
  2720. <listitem>
  2721. <para><literal>aopalliance.jar</literal></para>
  2722. </listitem>
  2723. <listitem>
  2724. <para><literal>spring.jar</literal></para>
  2725. </listitem>
  2726. <listitem>
  2727. <para><literal>commons-codec.jar</literal></para>
  2728. </listitem>
  2729. <listitem>
  2730. <para><literal>burlap.jar</literal></para>
  2731. </listitem>
  2732. <listitem>
  2733. <para><literal>hessian.jar</literal></para>
  2734. </listitem>
  2735. </itemizedlist>
  2736. <para>None of the above JAR files (or
  2737. <literal>acegi-security-XX.jar</literal>) should be in your
  2738. application's <literal>WEB-INF/lib</literal>. The realm name indicated
  2739. in your <literal>web.xml</literal> does not matter with
  2740. Catalina.</para>
  2741. <para>We have received reports of problems using this Container
  2742. Adapter with Mac OS X. A work-around is to use a script such as
  2743. follows:</para>
  2744. <para><programlisting>#!/bin/sh
  2745. export CATALINA_HOME="/Library/Tomcat"
  2746. export JAVA_HOME="/Library/Java/Home"
  2747. cd /
  2748. $CATALINA_HOME/bin/startup.sh</programlisting></para>
  2749. </sect2>
  2750. <sect2 id="security-container-adapters-jetty">
  2751. <title>Jetty Installation</title>
  2752. <para>The following was tested with Jetty 4.2.18.</para>
  2753. <para><literal>$JETTY_HOME</literal> refers to the root of your Jetty
  2754. installation.</para>
  2755. <para>Edit your <literal>$JETTY_HOME/etc/jetty.xml</literal> file so
  2756. the <literal>&lt;Configure class&gt;</literal> section has a new
  2757. addRealm call:</para>
  2758. <para><programlisting> &lt;Call name="addRealm"&gt;
  2759. &lt;Arg&gt;
  2760. &lt;New class="org.acegisecurity.adapters.jetty.JettyAcegiUserRealm"&gt;
  2761. &lt;Arg&gt;Spring Powered Realm&lt;/Arg&gt;
  2762. &lt;Arg&gt;my_password&lt;/Arg&gt;
  2763. &lt;Arg&gt;etc/acegisecurity.xml&lt;/Arg&gt;
  2764. &lt;/New&gt;
  2765. &lt;/Arg&gt;
  2766. &lt;/Call&gt;</programlisting></para>
  2767. <para>Copy <literal>acegisecurity.xml</literal> into
  2768. <literal>$JETTY_HOME/etc</literal>.</para>
  2769. <para>Copy the following files into
  2770. <literal>$JETTY_HOME/ext</literal>:<itemizedlist>
  2771. <listitem>
  2772. <para><literal>aopalliance.jar</literal></para>
  2773. </listitem>
  2774. <listitem>
  2775. <para><literal>commons-logging.jar</literal></para>
  2776. </listitem>
  2777. <listitem>
  2778. <para><literal>spring.jar</literal></para>
  2779. </listitem>
  2780. <listitem>
  2781. <para><literal>acegi-security-jetty-XX.jar</literal></para>
  2782. </listitem>
  2783. <listitem>
  2784. <para><literal>commons-codec.jar</literal></para>
  2785. </listitem>
  2786. <listitem>
  2787. <para><literal>burlap.jar</literal></para>
  2788. </listitem>
  2789. <listitem>
  2790. <para><literal>hessian.jar</literal></para>
  2791. </listitem>
  2792. </itemizedlist></para>
  2793. <para>None of the above JAR files (or
  2794. <literal>acegi-security-XX.jar</literal>) should be in your
  2795. application's <literal>WEB-INF/lib</literal>. The realm name indicated
  2796. in your <literal>web.xml</literal> does matter with Jetty. The
  2797. <literal>web.xml</literal> must express the same
  2798. <literal>&lt;realm-name&gt;</literal> as your
  2799. <literal>jetty.xml</literal> (in the example above, "Spring Powered
  2800. Realm").</para>
  2801. </sect2>
  2802. <sect2 id="security-container-adapters-joss">
  2803. <title>JBoss Installation</title>
  2804. <para>The following was tested with JBoss 3.2.6.</para>
  2805. <para><literal>$JBOSS_HOME</literal> refers to the root of your JBoss
  2806. installation.</para>
  2807. <para>There are two different ways of making spring context available
  2808. to the Jboss integration classes.</para>
  2809. <para>The first approach is by editing your
  2810. <literal>$JBOSS_HOME/server/your_config/conf/login-config.xml</literal>
  2811. file so that it contains a new entry under the
  2812. <literal>&lt;Policy&gt;</literal> section:</para>
  2813. <para><programlisting> &lt;application-policy name = "SpringPoweredRealm"&gt;
  2814. &lt;authentication&gt;
  2815. &lt;login-module code = "org.acegisecurity.adapters.jboss.JbossSpringLoginModule"
  2816. flag = "required"&gt;
  2817. &lt;module-option name = "appContextLocation"&gt;acegisecurity.xml&lt;/module-option&gt;
  2818. &lt;module-option name = "key"&gt;my_password&lt;/module-option&gt;
  2819. &lt;/login-module&gt;
  2820. &lt;/authentication&gt;
  2821. &lt;/application-policy&gt;</programlisting></para>
  2822. <para>Copy <literal>acegisecurity.xml</literal> into
  2823. <literal>$JBOSS_HOME/server/your_config/conf</literal>.</para>
  2824. <para>In this configuration <literal>acegisecurity.xml</literal>
  2825. contains the spring context definition including all the
  2826. authentication manager beans. You have to bear in mind though, that
  2827. <literal>SecurityContext</literal> is created and destroyed on each
  2828. login request, so the login operation might become costly.
  2829. Alternatively, the second approach is to use Spring singleton
  2830. capabilities through
  2831. <literal>org.springframework.beans.factory.access.SingletonBeanFactoryLocator</literal>.
  2832. The required configuration for this approach is:</para>
  2833. <para><programlisting> &lt;application-policy name = "SpringPoweredRealm"&gt;
  2834. &lt;authentication&gt;
  2835. &lt;login-module code = "org.acegisecurity.adapters.jboss.JbossSpringLoginModule"
  2836. flag = "required"&gt;
  2837. &lt;module-option name = "singletonId"&gt;springRealm&lt;/module-option&gt;
  2838. &lt;module-option name = "key"&gt;my_password&lt;/module-option&gt;
  2839. &lt;module-option name = "authenticationManager"&gt;authenticationManager&lt;/module-option&gt;
  2840. &lt;/login-module&gt;
  2841. &lt;/authentication&gt;
  2842. &lt;/application-policy&gt;</programlisting></para>
  2843. <para>In the above code fragment,
  2844. <literal>authenticationManager</literal> is a helper property that
  2845. defines the expected name of the
  2846. <literal>AuthenticationManager</literal> in case you have several
  2847. defined in the IoC container. The <literal>singletonId</literal>
  2848. property references a bean defined in a
  2849. <literal>beanRefFactory.xml</literal> file. This file needs to be
  2850. available from anywhere on the JBoss classpath, including
  2851. <literal>$JBOSS_HOME/server/your_config/conf</literal>. The
  2852. <literal>beanRefFactory.xml</literal> contains the following
  2853. declaration:</para>
  2854. <para><programlisting>&lt;beans&gt;
  2855. &lt;bean id="springRealm" singleton="true" lazy-init="true" class="org.springframework.context.support.ClassPathXmlApplicationContext"&gt;
  2856. &lt;constructor-arg&gt;
  2857. &lt;list&gt;
  2858. &lt;value&gt;acegisecurity.xml&lt;/value&gt;
  2859. &lt;/list&gt;
  2860. &lt;/constructor-arg&gt;
  2861. &lt;/bean&gt;
  2862. &lt;/beans&gt;</programlisting></para>
  2863. <para>Finally, irrespective of the configuration approach you need to
  2864. copy the following files into
  2865. <literal>$JBOSS_HOME/server/your_config/lib</literal>:<itemizedlist>
  2866. <listitem>
  2867. <para><literal>aopalliance.jar</literal></para>
  2868. </listitem>
  2869. <listitem>
  2870. <para><literal>spring.jar</literal></para>
  2871. </listitem>
  2872. <listitem>
  2873. <para><literal>acegi-security-jboss-XX.jar</literal></para>
  2874. </listitem>
  2875. <listitem>
  2876. <para><literal>commons-codec.jar</literal></para>
  2877. </listitem>
  2878. <listitem>
  2879. <para><literal>burlap.jar</literal></para>
  2880. </listitem>
  2881. <listitem>
  2882. <para><literal>hessian.jar</literal></para>
  2883. </listitem>
  2884. </itemizedlist></para>
  2885. <para>None of the above JAR files (or
  2886. <literal>acegi-security-XX.jar</literal>) should be in your
  2887. application's <literal>WEB-INF/lib</literal>. The realm name indicated
  2888. in your <literal>web.xml</literal> does not matter with JBoss.
  2889. However, your web application's
  2890. <literal>WEB-INF/jboss-web.xml</literal> must express the same
  2891. <literal>&lt;security-domain&gt;</literal> as your
  2892. <literal>login-config.xml</literal>. For example, to match the above
  2893. example, your <literal>jboss-web.xml</literal> would look like
  2894. this:</para>
  2895. <para><programlisting>&lt;jboss-web&gt;
  2896. &lt;security-domain&gt;java:/jaas/SpringPoweredRealm&lt;/security-domain&gt;
  2897. &lt;/jboss-web&gt;</programlisting></para>
  2898. </sect2>
  2899. <sect2 id="security-container-adapters-resin">
  2900. <title>Resin Installation</title>
  2901. <para>The following was tested with Resin 3.0.6.</para>
  2902. <para><literal>$RESIN_HOME</literal> refers to the root of your Resin
  2903. installation.</para>
  2904. <para>Resin provides several ways to support the container adapter. In
  2905. the instructions below we have elected to maximise consistency with
  2906. other container adapter configurations. This will allow Resin users to
  2907. simply deploy the sample application and confirm correct
  2908. configuration. Developers comfortable with Resin are naturally able to
  2909. use its capabilities to package the JARs with the web application
  2910. itself, and/or support single sign-on.</para>
  2911. <para>Copy the following files into
  2912. <literal>$RESIN_HOME/lib</literal>:<itemizedlist>
  2913. <listitem>
  2914. <para><literal>aopalliance.jar</literal></para>
  2915. </listitem>
  2916. <listitem>
  2917. <para><literal>commons-logging.jar</literal></para>
  2918. </listitem>
  2919. <listitem>
  2920. <para><literal>spring.jar</literal></para>
  2921. </listitem>
  2922. <listitem>
  2923. <para><literal>acegi-security-resin-XX.jar</literal></para>
  2924. </listitem>
  2925. <listitem>
  2926. <para><literal>commons-codec.jar</literal></para>
  2927. </listitem>
  2928. <listitem>
  2929. <para><literal>burlap.jar</literal></para>
  2930. </listitem>
  2931. <listitem>
  2932. <para><literal>hessian.jar</literal></para>
  2933. </listitem>
  2934. </itemizedlist></para>
  2935. <para>Unlike the container-wide <literal>acegisecurity.xml</literal>
  2936. files used by other container adapters, each Resin web application
  2937. will contain its own
  2938. <literal>WEB-INF/resin-acegisecurity.xml</literal> file. Each web
  2939. application will also contain a <literal>resin-web.xml</literal> file
  2940. which Resin uses to start the container adapter:</para>
  2941. <para><programlisting>&lt;web-app&gt;
  2942. &lt;authenticator&gt;
  2943. &lt;type&gt;org.acegisecurity.adapters.resin.ResinAcegiAuthenticator&lt;/type&gt;
  2944. &lt;init&gt;
  2945. &lt;app-context-location&gt;WEB-INF/resin-acegisecurity.xml&lt;/app-context-location&gt;
  2946. &lt;key&gt;my_password&lt;/key&gt;
  2947. &lt;/init&gt;
  2948. &lt;/authenticator&gt;
  2949. &lt;/web-app&gt;</programlisting></para>
  2950. <para>With the basic configuration provided above, none of the JAR
  2951. files listed (or <literal>acegi-security-XX.jar</literal>) should be
  2952. in your application's <literal>WEB-INF/lib</literal>. The realm name
  2953. indicated in your <literal>web.xml</literal> does not matter with
  2954. Resin, as the relevant authentication class is indicated by the
  2955. <literal>&lt;authenticator&gt;</literal> setting.</para>
  2956. </sect2>
  2957. </sect1>
  2958. <sect1 id="security-cas">
  2959. <title>Yale Central Authentication Service (CAS) Single Sign On</title>
  2960. <sect2 id="security-cas-overview">
  2961. <title>Overview</title>
  2962. <para>Yale University produces an enterprise-wide single sign on
  2963. system known as CAS. Unlike other initiatives, Yale's Central
  2964. Authentication Service is open source, widely used, simple to
  2965. understand, platform independent, and supports proxy capabilities. The
  2966. Acegi Security System for Spring fully supports CAS, and provides an
  2967. easy migration path from single-application deployments of Acegi
  2968. Security through to multiple-application deployments secured by an
  2969. enterprise-wide CAS server.</para>
  2970. <para>You can learn more about CAS at
  2971. <literal>http://www.yale.edu/tp/auth/</literal>. You will need to
  2972. visit this URL to download the CAS Server files. Whilst the Acegi
  2973. Security System for Spring includes two CAS libraries in the
  2974. "-with-dependencies" ZIP file, you will still need the CAS Java Server
  2975. Pages and <literal>web.xml</literal> to customise and deploy your CAS
  2976. server.</para>
  2977. </sect2>
  2978. <sect2 id="security-cas-how-cas-works">
  2979. <title>How CAS Works</title>
  2980. <para>Whilst the CAS web site above contains two documents that detail
  2981. the architecture of CAS, we present the general overview again here
  2982. within the context of the Acegi Security System for Spring. The
  2983. following refers to CAS 2.0, being the version of CAS that Acegi
  2984. Security System for Spring supports.</para>
  2985. <para>Somewhere in your enterprise you will need to setup a CAS
  2986. server. The CAS server is simply a standard WAR file, so there isn't
  2987. anything difficult about setting up your server. Inside the WAR file
  2988. you will customise the login and other single sign on pages displayed
  2989. to users. You will also need to specify in the web.xml a
  2990. <literal>PasswordHandler</literal>. The
  2991. <literal>PasswordHandler</literal> has a simple method that returns a
  2992. boolean as to whether a given username and password is valid. Your
  2993. <literal>PasswordHandler</literal> implementation will need to link
  2994. into some type of backend authentication repository, such as an LDAP
  2995. server or database.</para>
  2996. <para>If you are already running an existing CAS server instance, you
  2997. will have already established a <literal>PasswordHandler</literal>. If
  2998. you do not already have a <literal>PasswordHandler</literal>, you
  2999. might prefer to use the Acegi Security System for Spring
  3000. <literal>CasPasswordHandler</literal> class. This class delegates
  3001. through to the standard Acegi Security
  3002. <literal>AuthenticationManager</literal>, enabling you to use a
  3003. security configuration you might already have in place. You do not
  3004. need to use the <literal>CasPasswordHandler</literal> class on your
  3005. CAS server if you do not wish. The Acegi Security System for Spring
  3006. will function as a CAS client successfully irrespective of the
  3007. <literal>PasswordHandler</literal> you've chosen for your CAS
  3008. server.</para>
  3009. <para>Apart from the CAS server itself, the other key player is of
  3010. course the secure web applications deployed throughout your
  3011. enterprise. These web applications are known as "services". There are
  3012. two types of services: standard services and proxy services. A proxy
  3013. service is able to request resources from other services on behalf of
  3014. the user. This will be explained more fully later.</para>
  3015. <para>Services can be developed in a large variety of languages, due
  3016. to CAS 2.0's very light XML-based protocol. The Yale CAS home page
  3017. contains a clients archive which demonstrates CAS clients in Java,
  3018. Active Server Pages, Perl, Python and others. Naturally, Java support
  3019. is very strong given the CAS server is written in Java. You do not
  3020. need to use any of CAS' client classes in applications secured by the
  3021. Acegi Security System for Spring. This is handled transparently for
  3022. you.</para>
  3023. <para>The basic interaction between a web browser, CAS server and an
  3024. Acegi Security for System Spring secured service is as follows:</para>
  3025. <orderedlist>
  3026. <listitem>
  3027. <para>The web user is browsing the service's public pages. CAS or
  3028. Acegi Security is not involved.</para>
  3029. </listitem>
  3030. <listitem>
  3031. <para>The user eventually requests a page that is either secure or
  3032. one of the beans it uses is secure. Acegi Security's
  3033. <literal>SecurityEnforcementFilter</literal> will detect the
  3034. <literal>AuthenticationException</literal>.</para>
  3035. </listitem>
  3036. <listitem>
  3037. <para>Because the user's <literal>Authentication</literal> object
  3038. (or lack thereof) caused an
  3039. <literal>AuthenticationException</literal>, the
  3040. <literal>SecurityEnforcementFilter</literal> will call the
  3041. configured <literal>AuthenticationEntryPoint</literal>. If using
  3042. CAS, this will be the
  3043. <literal>CasProcessingFilterEntryPoint</literal> class.</para>
  3044. </listitem>
  3045. <listitem>
  3046. <para>The <literal>CasProcessingFilterEntry</literal> point will
  3047. redirect the user's browser to the CAS server. It will also
  3048. indicate a <literal>service</literal> parameter, which is the
  3049. callback URL for the Acegi Security service. For example, the URL
  3050. to which the browser is redirected might be
  3051. <literal>https://my.company.com/cas/login?service=https%3A%2F%2Fserver3.company.com%2Fwebapp%2Fj_acegi_cas_security_check</literal>.</para>
  3052. </listitem>
  3053. <listitem>
  3054. <para>After the user's browser redirects to CAS, they will be
  3055. prompted for their username and password. If the user presents a
  3056. session cookie which indicates they've previously logged on, they
  3057. will not be prompted to login again (there is an exception to this
  3058. procedure, which we'll cover later). CAS will use the
  3059. <literal>PasswordHandler</literal> discussed above to decide
  3060. whether the username and password is valid.</para>
  3061. </listitem>
  3062. <listitem>
  3063. <para>Upon successful login, CAS will redirect the user's browser
  3064. back to the original service. It will also include a
  3065. <literal>ticket</literal> parameter, which is an opaque string
  3066. representing the "service ticket". Continuing our earlier example,
  3067. the URL the browser is redirected to might be
  3068. <literal>https://server3.company.com/webapp/j_acegi_cas_security_check?ticket=ST-0-ER94xMJmn6pha35CQRoZ</literal>.</para>
  3069. </listitem>
  3070. <listitem>
  3071. <para>Back in the service web application, the
  3072. <literal>CasProcessingFilter</literal> is always listening for
  3073. requests to <literal>/j_acegi_cas_security_check</literal> (this
  3074. is configurable, but we'll use the defaults in this introduction).
  3075. The processing filter will construct a
  3076. <literal>UsernamePasswordAuthenticationToken</literal>
  3077. representing the service ticket. The principal will be equal to
  3078. <literal>CasProcessingFilter.CAS_STATEFUL_IDENTIFIER</literal>,
  3079. whilst the credentials will be the service ticket opaque value.
  3080. This authentication request will then be handed to the configured
  3081. <literal>AuthenticationManager</literal>.</para>
  3082. </listitem>
  3083. <listitem>
  3084. <para>The <literal>AuthenticationManager</literal> implementation
  3085. will be the <literal>ProviderManager</literal>, which is in turn
  3086. configured with the <literal>CasAuthenticationProvider</literal>.
  3087. The <literal>CasAuthenticationProvider</literal> only responds to
  3088. <literal>UsernamePasswordAuthenticationToken</literal>s containing
  3089. the CAS-specific principal (such as
  3090. <literal>CasProcessingFilter.CAS_STATEFUL_IDENTIFIER</literal>)
  3091. and <literal>CasAuthenticationToken</literal>s (discussed
  3092. later).</para>
  3093. </listitem>
  3094. <listitem>
  3095. <para><literal>CasAuthenticationProvider</literal> will validate
  3096. the service ticket using a <literal>TicketValidator</literal>
  3097. implementation. Acegi Security includes one implementation, the
  3098. <literal>CasProxyTicketValidator</literal>. This implementation a
  3099. ticket validation class included in the CAS client library. The
  3100. <literal>CasProxyTicketValidator</literal> makes a HTTPS request
  3101. to the CAS server in order to validate the service ticket. The
  3102. <literal>CasProxyTicketValidator</literal> may also include a
  3103. proxy callback URL, which is included in this example:
  3104. <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>
  3105. </listitem>
  3106. <listitem>
  3107. <para>Back on the CAS server, the proxy validation request will be
  3108. received. If the presented service ticket matches the service URL
  3109. the ticket was issued to, CAS will provide an affirmative response
  3110. in XML indicating the username. If any proxy was involved in the
  3111. authentication (discussed below), the list of proxies is also
  3112. included in the XML response.</para>
  3113. </listitem>
  3114. <listitem>
  3115. <para>[OPTIONAL] If the request to the CAS validation service
  3116. included the proxy callback URL (in the <literal>pgtUrl</literal>
  3117. parameter), CAS will include a <literal>pgtIou</literal> string in
  3118. the XML response. This <literal>pgtIou</literal> represents a
  3119. proxy-granting ticket IOU. The CAS server will then create its own
  3120. HTTPS connection back to the <literal>pgtUrl</literal>. This is to
  3121. mutually authenticate the CAS server and the claimed service URL.
  3122. The HTTPS connection will be used to send a proxy granting ticket
  3123. to the original web application. For example,
  3124. <literal>https://server3.company.com/webapp/casProxy/receptor?pgtIou=PGTIOU-0-R0zlgrl4pdAQwBvJWO3vnNpevwqStbSGcq3vKB2SqSFFRnjPHt&amp;pgtId=PGT-1-si9YkkHLrtACBo64rmsi3v2nf7cpCResXg5MpESZFArbaZiOKH</literal>.
  3125. We suggest you use CAS' <literal>ProxyTicketReceptor</literal>
  3126. servlet to receive these proxy-granting tickets, if they are
  3127. required.</para>
  3128. </listitem>
  3129. <listitem>
  3130. <para>The <literal>CasProxyTicketValidator</literal> will parse
  3131. the XML received from the CAS server. It will return to the
  3132. <literal>CasAuthenticationProvider</literal> a
  3133. <literal>TicketResponse</literal>, which includes the username
  3134. (mandatory), proxy list (if any were involved), and proxy-granting
  3135. ticket IOU (if the proxy callback was requested).</para>
  3136. </listitem>
  3137. <listitem>
  3138. <para>Next <literal>CasAuthenticationProvider</literal> will call
  3139. a configured <literal>CasProxyDecider</literal>. The
  3140. <literal>CasProxyDecider</literal> indicates whether the proxy
  3141. list in the <literal>TicketResponse</literal> is acceptable to the
  3142. service. Several implementations are provided with the Acegi
  3143. Security System: <literal>RejectProxyTickets</literal>,
  3144. <literal>AcceptAnyCasProxy</literal> and
  3145. <literal>NamedCasProxyDecider</literal>. These names are largely
  3146. self-explanatory, except <literal>NamedCasProxyDecider</literal>
  3147. which allows a <literal>List</literal> of trusted proxies to be
  3148. provided.</para>
  3149. </listitem>
  3150. <listitem>
  3151. <para><literal>CasAuthenticationProvider</literal> will next
  3152. request a <literal>CasAuthoritiesPopulator</literal> to advise the
  3153. <literal>GrantedAuthority</literal> objects that apply to the user
  3154. contained in the <literal>TicketResponse</literal>. Acegi Security
  3155. includes a <literal>DaoCasAuthoritiesPopulator</literal> which
  3156. simply uses the <literal>UserDetailsService</literal>
  3157. infrastructure to find the <literal>UserDetails</literal> and
  3158. their associated <literal>GrantedAuthority</literal>s. Note that
  3159. the password and enabled/disabled status of
  3160. <literal>UserDetails</literal> returned by the
  3161. <literal>UserDetailsService</literal> are ignored, as the CAS
  3162. server is responsible for authentication decisions.
  3163. <literal>DaoCasAuthoritiesPopulator</literal> is only concerned
  3164. with retrieving the <literal>GrantedAuthority</literal>s.</para>
  3165. </listitem>
  3166. <listitem>
  3167. <para>If there were no problems,
  3168. <literal>CasAuthenticationProvider</literal> constructs a
  3169. <literal>CasAuthenticationToken</literal> including the details
  3170. contained in the <literal>TicketResponse</literal> and the
  3171. <literal>GrantedAuthority</literal>s. The
  3172. <literal>CasAuthenticationToken</literal> contains the hash of a
  3173. key, so that the <literal>CasAuthenticationProvider</literal>
  3174. knows it created it.</para>
  3175. </listitem>
  3176. <listitem>
  3177. <para>Control then returns to
  3178. <literal>CasProcessingFilter</literal>, which places the created
  3179. <literal>CasAuthenticationToken</literal> into the
  3180. <literal>HttpSession</literal> attribute named
  3181. <literal>HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY</literal>.</para>
  3182. </listitem>
  3183. <listitem>
  3184. <para>The user's browser is redirected to the original page that
  3185. caused the <literal>AuthenticationException</literal>.</para>
  3186. </listitem>
  3187. <listitem>
  3188. <para>As the <literal>Authentication</literal> object is now in
  3189. the well-known location, it is handled like any other
  3190. authentication approach. Usually the
  3191. <literal>HttpSessionIntegrationFilter</literal> will be used to
  3192. associate the <literal>Authentication</literal> object with the
  3193. <literal>SecurityContextHolder</literal> for the duration of each
  3194. request.</para>
  3195. </listitem>
  3196. </orderedlist>
  3197. <para>It's good that you're still here! It might sound involved, but
  3198. you can relax as the Acegi Security System for Spring classes hide
  3199. much of the complexity. Let's now look at how this is
  3200. configured.</para>
  3201. </sect2>
  3202. <sect2 id="security-cas-install-server">
  3203. <title>CAS Server Installation (Optional)</title>
  3204. <para>As mentioned above, the Acegi Security System for Spring
  3205. includes a <literal>PasswordHandler</literal> that bridges your
  3206. existing <literal>AuthenticationManager</literal> into CAS. You do not
  3207. need to use this <literal>PasswordHandler</literal> to use Acegi
  3208. Security on the client side (any CAS
  3209. <literal>PasswordHandler</literal> will do).</para>
  3210. <para>To install, you will need to download and extract the CAS server
  3211. archive. We used version 2.0.12. There will be a
  3212. <literal>/web</literal> directory in the root of the deployment. Copy
  3213. an <literal>applicationContext.xml</literal> containing your
  3214. <literal>AuthenticationManager</literal> as well as the
  3215. <literal>CasPasswordHandler</literal> into the
  3216. <literal>/web/WEB-INF</literal> directory. A sample
  3217. <literal>applicationContext.xml</literal> is included below:</para>
  3218. <programlisting>&lt;bean id="inMemoryDaoImpl" class="org.acegisecurity.userdetails.memory.InMemoryDaoImpl"&gt;
  3219. &lt;property name="userMap"&gt;
  3220. &lt;value&gt;
  3221. marissa=koala,ROLES_IGNORED_BY_CAS
  3222. dianne=emu,ROLES_IGNORED_BY_CAS
  3223. scott=wombat,ROLES_IGNORED_BY_CAS
  3224. peter=opal,disabled,ROLES_IGNORED_BY_CAS
  3225. &lt;/value&gt;
  3226. &lt;/property&gt;
  3227. &lt;/bean&gt;
  3228. &lt;bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider"&gt;
  3229. &lt;property name="userDetailsService"&gt;&lt;ref bean="inMemoryDaoImpl"/&gt;&lt;/property&gt;
  3230. &lt;/bean&gt;
  3231. &lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt;
  3232. &lt;property name="providers"&gt;
  3233. &lt;list&gt;
  3234. &lt;ref bean="daoAuthenticationProvider"/&gt;
  3235. &lt;/list&gt;
  3236. &lt;/property&gt;
  3237. &lt;/bean&gt;
  3238. &lt;bean id="casPasswordHandler" class="org.acegisecurity.adapters.cas.CasPasswordHandler"&gt;
  3239. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  3240. &lt;/bean&gt;</programlisting>
  3241. <para>Note the granted authorities are ignored by CAS because it has
  3242. no way of communicating the granted authorities to calling
  3243. applications. CAS is only concerned with username and passwords (and
  3244. the enabled/disabled status).</para>
  3245. <para>Next you will need to edit the existing
  3246. <literal>/web/WEB-INF/web.xml</literal> file. Add (or edit in the case
  3247. of the <literal>authHandler</literal> property) the following
  3248. lines:</para>
  3249. <para><programlisting>&lt;context-param&gt;
  3250. &lt;param-name&gt;edu.yale.its.tp.cas.authHandler&lt;/param-name&gt;
  3251. &lt;param-value&gt;org.acegisecurity.adapters.cas.CasPasswordHandlerProxy&lt;/param-value&gt;
  3252. &lt;/context-param&gt;
  3253. &lt;context-param&gt;
  3254. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
  3255. &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;
  3256. &lt;/context-param&gt;
  3257. &lt;listener&gt;
  3258. &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  3259. &lt;/listener&gt;</programlisting></para>
  3260. <para>Copy the <literal>spring.jar</literal> and
  3261. <literal>acegi-security.jar</literal> files into
  3262. <literal>/web/WEB-INF/lib</literal>. Now use the <literal>ant
  3263. dist</literal> task in the <literal>build.xml</literal> in the root of
  3264. the directory structure. This will create
  3265. <literal>/lib/cas.war</literal>, which is ready for deployment to your
  3266. servlet container.</para>
  3267. <para>Note CAS heavily relies on HTTPS. You can't even test the system
  3268. without a HTTPS certificate. Whilst you should refer to your web
  3269. container's documentation on setting up HTTPS, if you need some
  3270. additional help or a test certificate you might like to check the
  3271. <literal>samples/contacts/etc/ssl</literal> directory.</para>
  3272. </sect2>
  3273. <sect2 id="security-cas-install-client">
  3274. <title>CAS Acegi Security System Client Installation</title>
  3275. <para>The web application side of CAS is made easy due to the Acegi
  3276. Security System for Spring. It is assumed you already know the basics
  3277. of using the Acegi Security System for Spring, so these are not
  3278. covered again below. Only the CAS-specific beans are mentioned.</para>
  3279. <para>You will need to add a <literal>ServiceProperties</literal> bean
  3280. to your application context. This represents your service:</para>
  3281. <para><programlisting>&lt;bean id="serviceProperties" class="org.acegisecurity.ui.cas.ServiceProperties"&gt;
  3282. &lt;property name="service"&gt;&lt;value&gt;https://localhost:8443/contacts-cas/j_acegi_cas_security_check&lt;/value&gt;&lt;/property&gt;
  3283. &lt;property name="sendRenew"&gt;&lt;value&gt;false&lt;/value&gt;&lt;/property&gt;
  3284. &lt;/bean&gt;</programlisting></para>
  3285. <para>The <literal>service</literal> must equal a URL that will be
  3286. monitored by the <literal>CasProcessingFilter</literal>. The
  3287. <literal>sendRenew</literal> defaults to false, but should be set to
  3288. true if your application is particularly sensitive. What this
  3289. parameter does is tell the CAS login service that a single sign on
  3290. login is unacceptable. Instead, the user will need to re-enter their
  3291. username and password in order to gain access to the service.</para>
  3292. <para>The following beans should be configured to commence the CAS
  3293. authentication process:</para>
  3294. <para><programlisting>&lt;bean id="casProcessingFilter" class="org.acegisecurity.ui.cas.CasProcessingFilter"&gt;
  3295. &lt;property name="authenticationManager"&gt;&lt;ref bean="authenticationManager"/&gt;&lt;/property&gt;
  3296. &lt;property name="authenticationFailureUrl"&gt;&lt;value&gt;/casfailed.jsp&lt;/value&gt;&lt;/property&gt;
  3297. &lt;property name="defaultTargetUrl"&gt;&lt;value&gt;/&lt;/value&gt;&lt;/property&gt;
  3298. &lt;property name="filterProcessesUrl"&gt;&lt;value&gt;/j_acegi_cas_security_check&lt;/value&gt;&lt;/property&gt;
  3299. &lt;/bean&gt;
  3300. &lt;bean id="securityEnforcementFilter" class="org.acegisecurity.intercept.web.SecurityEnforcementFilter"&gt;
  3301. &lt;property name="filterSecurityInterceptor"&gt;&lt;ref bean="filterInvocationInterceptor"/&gt;&lt;/property&gt;
  3302. &lt;property name="authenticationEntryPoint"&gt;&lt;ref bean="casProcessingFilterEntryPoint"/&gt;&lt;/property&gt;
  3303. &lt;/bean&gt;
  3304. &lt;bean id="casProcessingFilterEntryPoint" class="org.acegisecurity.ui.cas.CasProcessingFilterEntryPoint"&gt;
  3305. &lt;property name="loginUrl"&gt;&lt;value&gt;https://localhost:8443/cas/login&lt;/value&gt;&lt;/property&gt;
  3306. &lt;property name="serviceProperties"&gt;&lt;ref bean="serviceProperties"/&gt;&lt;/property&gt;
  3307. &lt;/bean&gt;</programlisting></para>
  3308. <para>You will also need to add the
  3309. <literal>CasProcessingFilter</literal> to web.xml:</para>
  3310. <para><programlisting>&lt;filter&gt;
  3311. &lt;filter-name&gt;Acegi CAS Processing Filter&lt;/filter-name&gt;
  3312. &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  3313. &lt;init-param&gt;
  3314. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  3315. &lt;param-value&gt;org.acegisecurity.ui.cas.CasProcessingFilter&lt;/param-value&gt;
  3316. &lt;/init-param&gt;
  3317. &lt;/filter&gt;
  3318. &lt;filter-mapping&gt;
  3319. &lt;filter-name&gt;Acegi CAS Processing Filter&lt;/filter-name&gt;
  3320. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  3321. &lt;/filter-mapping&gt;</programlisting></para>
  3322. <para>The <literal>CasProcessingFilter</literal> has very similar
  3323. properties to the <literal>AuthenticationProcessingFilter</literal>
  3324. (used for form-based logins). Each property is
  3325. self-explanatory.</para>
  3326. <para>For CAS to operate, the
  3327. <literal>SecurityEnforcementFilter</literal> must have its
  3328. <literal>authenticationEntryPoint</literal> property set to the
  3329. <literal>CasProcessingFilterEntryPoint</literal> bean.</para>
  3330. <para>The <literal>CasProcessingFilterEntryPoint</literal> must refer
  3331. to the <literal>ServiceProperties</literal> bean (discussed above),
  3332. which provides the URL to the enterprise's CAS login server. This is
  3333. where the user's browser will be redirected.</para>
  3334. <para>Next you need to add an <literal>AuthenticationManager</literal>
  3335. that uses <literal>CasAuthenticationProvider</literal> and its
  3336. collaborators:</para>
  3337. <para><programlisting>&lt;bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager"&gt;
  3338. &lt;property name="providers"&gt;
  3339. &lt;list&gt;
  3340. &lt;ref bean="casAuthenticationProvider"/&gt;
  3341. &lt;/list&gt;
  3342. &lt;/property&gt;
  3343. &lt;/bean&gt;
  3344. &lt;bean id="casAuthenticationProvider" class="org.acegisecurity.providers.cas.CasAuthenticationProvider"&gt;
  3345. &lt;property name="casAuthoritiesPopulator"&gt;&lt;ref bean="casAuthoritiesPopulator"/&gt;&lt;/property&gt;
  3346. &lt;property name="casProxyDecider"&gt;&lt;ref bean="casProxyDecider"/&gt;&lt;/property&gt;
  3347. &lt;property name="ticketValidator"&gt;&lt;ref bean="casProxyTicketValidator"/&gt;&lt;/property&gt;
  3348. &lt;property name="statelessTicketCache"&gt;&lt;ref bean="statelessTicketCache"/&gt;&lt;/property&gt;
  3349. &lt;property name="key"&gt;&lt;value&gt;my_password_for_this_auth_provider_only&lt;/value&gt;&lt;/property&gt;
  3350. &lt;/bean&gt;
  3351. &lt;bean id="casProxyTicketValidator" class="org.acegisecurity.providers.cas.ticketvalidator.CasProxyTicketValidator"&gt;
  3352. &lt;property name="casValidate"&gt;&lt;value&gt;https://localhost:8443/cas/proxyValidate&lt;/value&gt;&lt;/property&gt;
  3353. &lt;property name="proxyCallbackUrl"&gt;&lt;value&gt;https://localhost:8443/contacts-cas/casProxy/receptor&lt;/value&gt;&lt;/property&gt;
  3354. &lt;property name="serviceProperties"&gt;&lt;ref bean="serviceProperties"/&gt;&lt;/property&gt;
  3355. &lt;!-- &lt;property name="trustStore"&gt;&lt;value&gt;/some/path/to/your/lib/security/cacerts&lt;/value&gt;&lt;/property&gt; --&gt;
  3356. &lt;/bean&gt;
  3357. &lt;bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&gt;
  3358. &lt;property name="configLocation"&gt;
  3359. &lt;value&gt;classpath:/ehcache-failsafe.xml&lt;/value&gt;
  3360. &lt;/property&gt;
  3361. &lt;/bean&gt;
  3362. &lt;bean id="ticketCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean"&gt;
  3363. &lt;property name="cacheManager"&gt;
  3364. &lt;ref local="cacheManager"/&gt;
  3365. &lt;/property&gt;
  3366. &lt;property name="cacheName"&gt;
  3367. &lt;value&gt;ticketCache&lt;/value&gt;
  3368. &lt;/property&gt;
  3369. &lt;/bean&gt;
  3370. &lt;bean id="statelessTicketCache" class="org.acegisecurity.providers.cas.cache.EhCacheBasedTicketCache"&gt;
  3371. &lt;property name="cache"&gt;&lt;ref local="ticketCacheBackend"/&gt;&lt;/property&gt;
  3372. &lt;/bean&gt;
  3373. &lt;bean id="casAuthoritiesPopulator" class="org.acegisecurity.providers.cas.populator.DaoCasAuthoritiesPopulator"&gt;
  3374. &lt;property name="userDetailsService"&gt;&lt;ref bean="inMemoryDaoImpl"/&gt;&lt;/property&gt;
  3375. &lt;/bean&gt;
  3376. &lt;bean id="casProxyDecider" class="org.acegisecurity.providers.cas.proxy.RejectProxyTickets"/&gt;</programlisting></para>
  3377. <para>The beans are all reasonable self-explanatory if you refer back
  3378. to the "How CAS Works" section. Careful readers might notice one
  3379. surprise: the <literal>statelessTicketCache</literal> property of the
  3380. <literal>CasAuthenticationProvider</literal>. This is discussed in
  3381. detail in the "Advanced CAS Usage" section.</para>
  3382. <para>Note the <literal>CasProxyTicketValidator</literal> has a
  3383. remarked out <literal>trustStore</literal> property. This property
  3384. might be helpful if you experience HTTPS certificate issues. Also note
  3385. the <literal>proxyCallbackUrl</literal> is set so the service can
  3386. receive a proxy-granting ticket. As mentioned above, this is optional
  3387. and unnecessary if you do not require proxy-granting tickets. If you
  3388. do use this feature, you will need to configure a suitable servlet to
  3389. receive the proxy-granting tickets. We suggest you use CAS'
  3390. <literal>ProxyTicketReceptor</literal> by adding the following to your
  3391. web application's <literal>web.xml</literal>:</para>
  3392. <para><programlisting>&lt;servlet&gt;
  3393. &lt;servlet-name&gt;casproxy&lt;/servlet-name&gt;
  3394. &lt;servlet-class&gt;edu.yale.its.tp.cas.proxy.ProxyTicketReceptor&lt;/servlet-class&gt;
  3395. &lt;/servlet&gt;
  3396. &lt;servlet-mapping&gt;
  3397. &lt;servlet-name&gt;casproxy&lt;/servlet-name&gt;
  3398. &lt;url-pattern&gt;/casProxy/*&lt;/url-pattern&gt;
  3399. &lt;/servlet-mapping&gt;</programlisting></para>
  3400. <para>This completes the configuration of CAS. If you haven't made any
  3401. mistakes, your web application should happily work within the
  3402. framework of CAS single sign on. No other parts of the Acegi Security
  3403. System for Spring need to be concerned about the fact CAS handled
  3404. authentication.</para>
  3405. <para>There is also a <literal>contacts-cas.war</literal> file in the
  3406. sample applications directory. This sample application uses the above
  3407. settings and can be deployed to see CAS in operation.</para>
  3408. </sect2>
  3409. <sect2 id="security-cas-advanced-usage">
  3410. <title>Advanced CAS Usage</title>
  3411. <para>The <literal>CasAuthenticationProvider</literal> distinguishes
  3412. between stateful and stateless clients. A stateful client is
  3413. considered any that originates via the
  3414. <literal>CasProcessingFilter</literal>. A stateless client is any that
  3415. presents an authentication request via the
  3416. <literal>UsernamePasswordAuthenticationToken</literal> with a
  3417. principal equal to
  3418. <literal>CasProcessingFilter.CAS_STATELESS_IDENTIFIER</literal>.</para>
  3419. <para>Stateless clients are likely to be via remoting protocols such
  3420. as Hessian and Burlap. The <literal>BasicProcessingFilter</literal> is
  3421. still used in this case, but the remoting protocol client is expected
  3422. to present a username equal to the static string above, and a password
  3423. equal to a CAS service ticket. Clients should acquire a CAS service
  3424. ticket directly from the CAS server.</para>
  3425. <para>Because remoting protocols have no way of presenting themselves
  3426. within the context of a <literal>HttpSession</literal>, it isn't
  3427. possible to rely on the <literal>HttpSession</literal>'s
  3428. <literal>HttpSessionIntegrationFilter.ACEGI_SECURITY_AUTHENTICATION_KEY</literal>
  3429. attribute to locate the <literal>CasAuthenticationToken</literal>.
  3430. Furthermore, because the CAS server invalidates a service ticket after
  3431. it has been validated by the <literal>TicketValidator</literal>,
  3432. presenting the same service ticket on subsequent requests will not
  3433. work. It is similarly very difficult to obtain a proxy-granting ticket
  3434. for a remoting protocol client, as they are often deployed on client
  3435. machines which rarely have HTTPS URLs that would be accessible to the
  3436. CAS server.</para>
  3437. <para>One obvious option is to not use CAS at all for remoting
  3438. protocol clients. However, this would eliminate many of the desirable
  3439. features of CAS.</para>
  3440. <para>As a middle-ground, the
  3441. <literal>CasAuthenticationProvider</literal> uses a
  3442. <literal>StatelessTicketCache</literal>. This is used solely for
  3443. requests with a principal equal to
  3444. <literal>CasProcessingFilter.CAS_STATELESS_IDENTIFIER</literal>. What
  3445. happens is the <literal>CasAuthenticationProvider</literal> will store
  3446. the resulting <literal>CasAuthenticationToken</literal> in the
  3447. <literal>StatelessTicketCache</literal>, keyed on the service ticket.
  3448. Accordingly, remoting protocol clients can present the same service
  3449. ticket and the <literal>CasAuthenticationProvider</literal> will not
  3450. need to contact the CAS server for validation (aside from the first
  3451. request).</para>
  3452. <para>The other aspect of advanced CAS usage involves creating proxy
  3453. tickets from the proxy-granting ticket. As indicated above, we
  3454. recommend you use CAS' <literal>ProxyTicketReceptor</literal> to
  3455. receive these tickets. The <literal>ProxyTicketReceptor</literal>
  3456. provides a static method that enables you to obtain a proxy ticket by
  3457. presenting the proxy-granting IOU ticket. You can obtain the
  3458. proxy-granting IOU ticket by calling
  3459. <literal>CasAuthenticationToken.getProxyGrantingTicketIou()</literal>.</para>
  3460. <para>It is hoped you find CAS integration easy and useful with the
  3461. Acegi Security System for Spring classes. Welcome to enterprise-wide
  3462. single sign on!</para>
  3463. </sect2>
  3464. </sect1>
  3465. <sect1 id="security-x509">
  3466. <title>X509 Authentication</title>
  3467. <sect2 id="security-x509-overview">
  3468. <title>Overview</title>
  3469. <para>The most common use of X509 certificate authentication is in
  3470. verifying the identity of a server when using SSL, most commonly when
  3471. using HTTPS from a browser. The browser will automatically check that
  3472. the certificate presented by a server has been issued (ie digitally
  3473. signed) by one of a list of trusted certificate authorities which it
  3474. maintains.</para>
  3475. <para>You can also use SSL with <quote>mutual authentication</quote>;
  3476. the server will then request a valid certificate from the client as
  3477. part of the SSL handshake. The server will authenticate the client by
  3478. checking that it's certificate is signed by an acceptable authority.
  3479. If a valid certificate has been provided, it can be obtained through
  3480. the servlet API in an application. The Acegi Security X509 module
  3481. extracts the certificate using a filter and passes it to the
  3482. configured X509 authentication provider to allow any additional
  3483. application-specific checks to be applied. It also maps the
  3484. certificate to an application user and loads that user's set of
  3485. granted authorities for use with the standard Acegi Security
  3486. infrastructure.</para>
  3487. <para>You should be familiar with using certificates and setting up
  3488. client authentication for your servlet container before attempting to
  3489. use it with Acegi Security. Most of the work is in creating and
  3490. installing suitable certificates and keys. For example, if you're
  3491. using Tomcat then read the instructions here <ulink
  3492. url="http://jakarta.apache.org/tomcat/tomcat-5.0-doc/ssl-howto.html"></ulink>.
  3493. It's important that you get this working before trying it out with
  3494. Acegi Security.</para>
  3495. </sect2>
  3496. <sect2 id="security-x509-details">
  3497. <title>X509 with Acegi Security</title>
  3498. <para>With X509 authentication, there is no explicit login procedure
  3499. so the implementation is relatively simple; there is no need to
  3500. redirect requests in order to interact with the user. As a result,
  3501. some of the classes behave slightly differently from their equivalents
  3502. in other packages. For example, the default <quote>entry point</quote>
  3503. class, which is normally responsible for starting the authentication
  3504. process, is only invoked if the certificate is rejected and it always
  3505. returns an error to the user. With a suitable bean configuration, the
  3506. normal sequence of events is as follows <orderedlist>
  3507. <listitem>
  3508. <para>The <classname>X509ProcessingFilter</classname> extracts
  3509. the certificate from the request and uses it as the credentials
  3510. for an authentication request. The generated authentication
  3511. request is an <classname>X509AuthenticationToken</classname>.
  3512. The request is passed to the authentication manager.</para>
  3513. </listitem>
  3514. <listitem>
  3515. <para>The <classname>X509AuthenticationProvider</classname>
  3516. receives the token. Its main concern is to obtain the user
  3517. information (in particular the user's granted authorities) that
  3518. matches the certificate. It delegates this responsibility to an
  3519. <interfacename>X509AuthoritiesPopulator</interfacename>.</para>
  3520. </listitem>
  3521. <listitem>
  3522. <para>The populator's single method,
  3523. <methodname>getUserDetails(X509Certificate
  3524. userCertificate)</methodname> is invoked. Implementations should
  3525. return a <classname>UserDetails</classname> instance containing
  3526. the array of <classname>GrantedAuthority</classname> objects for
  3527. the user. This method can also choose to reject the certificate
  3528. (for example if it doesn't contain a matching user name). In
  3529. such cases it should throw a
  3530. <exceptionname>BadCredentialsException</exceptionname>. A
  3531. DAO-based implementation,
  3532. <classname>DaoX509AuthoritiesPopulator</classname>, is provided
  3533. which extracts the user's name from the subject <quote>common
  3534. name</quote> (CN) in the certificate. It also allows you to set
  3535. your own regular expression to match a different part of the
  3536. subject's distinguished name. A UserDetailsService is used to
  3537. load the user information.<!-- TODO: Give email matching as an example --></para>
  3538. </listitem>
  3539. <listitem>
  3540. <para>If everything has gone smoothly then there should be a
  3541. valid <classname>Authentication</classname> object in the secure
  3542. context and the invocation will procede as normal. If no
  3543. certificate was found, or the certificate was rejected, then the
  3544. <classname>SecurityEnforcementFilter</classname> will invoke the
  3545. <classname>X509ProcessingFilterEntryPoint</classname> which
  3546. returns a 403 error (forbidden) to the user.</para>
  3547. </listitem>
  3548. </orderedlist></para>
  3549. </sect2>
  3550. <sect2 id="security-x509-config">
  3551. <title>Configuring the X509 Provider</title>
  3552. <para>There is a version of the <link
  3553. linkend="security-sample">Contacts Sample Application</link> which
  3554. uses X509. Copy the beans and filter setup from this as a starting
  3555. point for configuring your own application. A set of example
  3556. certificates is also included which you can use to configure your
  3557. server. These are <itemizedlist>
  3558. <listitem>
  3559. <para><filename>marissa.p12</filename>: A PKCS12 format file
  3560. containing the client key and certificate. These should be
  3561. installed in your browser. It maps to the user
  3562. <quote>marissa</quote> in the application.</para>
  3563. </listitem>
  3564. <listitem>
  3565. <para><filename>server.p12</filename>: The server certificate
  3566. and key for HTTPS connections.</para>
  3567. </listitem>
  3568. <listitem>
  3569. <para><filename>ca.jks</filename>: A Java keystore containing
  3570. the certificate for the authority which issued marissa's
  3571. certificate. This will be used by the container to validate
  3572. client certificates.</para>
  3573. </listitem>
  3574. </itemizedlist> For JBoss 3.2.7 (with Tomcat 5.0), the SSL
  3575. configuration in the <filename>server.xml</filename> file looks like
  3576. this <programlisting>&lt;!-- SSL/TLS Connector configuration --&gt;
  3577. &lt;Connector port="8443" address="${jboss.bind.address}"
  3578. maxThreads="100" minSpareThreads="5" maxSpareThreads="15"
  3579. scheme="https" secure="true"
  3580. sslProtocol = "TLS"
  3581. clientAuth="true" keystoreFile="${jboss.server.home.dir}/conf/server.p12"
  3582. keystoreType="PKCS12" keystorePass="password"
  3583. truststoreFile="${jboss.server.home.dir}/conf/ca.jks"
  3584. truststoreType="JKS" truststorePass="password"
  3585. /&gt;</programlisting><parameter>clientAuth</parameter> can also be set to
  3586. <parameter>want</parameter> if you still want SSL connections to
  3587. succeed even if the client doesn't provide a certificate. Obviously
  3588. these clients won't be able to access any objects secured by Acegi
  3589. Security (unless you use a non-X509 authentication mechanism, such as
  3590. BASIC authentication, to authenticate the user).</para>
  3591. </sect2>
  3592. </sect1>
  3593. <sect1 id="security-ldap">
  3594. <title>LDAP Authentication Provider</title>
  3595. <sect2 id="security-ldap-overview">
  3596. <title>Overview</title>
  3597. <para>LDAP is often used by organizations as a central repository for user information and
  3598. as an authentication service. It can also be used to store the role information for
  3599. application users. </para>
  3600. <para>There are many different scenarios for how an LDAP server may be configured so the
  3601. Acegi LDAP provider is fully configurable. It uses separate strategy interfaces for
  3602. authentication and role retrieval and provides default implementations which can be
  3603. configured to handle a wide range of situations. </para>
  3604. <para>You should be familiar with LDAP before trying to use it with Acegi. The following
  3605. link provides a good introduction to the concepts involved and a guide to setting up a
  3606. directory using the free LDAP server OpenLDAP: <ulink
  3607. url="http://www.zytrax.com/books/ldap/"/>. Some familiarity with the JNDI APIs used to
  3608. access LDAP from Java may also be useful. We don't use any third-party LDAP libraries
  3609. (Mozilla/Netscape, JLDAP etc.) in the LDAP provider. </para>
  3610. <sect3 id="security-ldap-details">
  3611. <title>LDAP with Acegi Security</title>
  3612. <para>The main LDAP provider class is
  3613. <classname>org.acegisecurity.providers.ldap.LdapAuthenticationProvider</classname>. This
  3614. bean doesn't actually do much itself other than implement the
  3615. <methodname>retrieveUser</methodname> method required by its base class,
  3616. <classname>AbstractUserDetailsAuthenticationProvider</classname>. It delegates the work
  3617. to two other beans, an <interfacename>LdapAuthenticator</interfacename> and an
  3618. <interfacename>LdapAuthoritiesPopulator</interfacename> which are responsible for
  3619. authenticating the user and retrieving the user's set of
  3620. <interfacename>GrantedAuthority</interfacename>s respectively.
  3621. </para>
  3622. </sect3>
  3623. </sect2>
  3624. <sect2 id="security-ldap-authenticators">
  3625. <title>LdapAuthenticator Implementations</title>
  3626. <para> The authenticator is also responsible for retrieving any required user attributes.
  3627. This is because the permissions on the attributes may depend on the type of
  3628. authentication being used. For example, if binding as the user, it may be necessary to
  3629. read them with the user's own permissions. </para>
  3630. <para> There are currently two authentication strategies supplied with Acegi Security:
  3631. <itemizedlist>
  3632. <listitem>
  3633. <para>Authentication directly to the LDAP server ("bind" authentication).</para>
  3634. </listitem>
  3635. <listitem>
  3636. <para>Password comparison, where the password supplied by the user is compared with
  3637. the one stored in the repository. This can either be done by retrieving the value
  3638. of the password attribute and checking it locally or by performing an LDAP
  3639. "compare" operation, where the supplied password is passed to the server for
  3640. comparison and the real password value is never retrieved.</para>
  3641. </listitem>
  3642. </itemizedlist>
  3643. </para>
  3644. <sect3>
  3645. <title>Common Functionality</title>
  3646. <para>Before it is possible to authenticate a user (by either strategy), the
  3647. distinguished name (DN) has to be obtained from the login name supplied to the
  3648. application. This can be done either by simple pattern-matching (by setting the
  3649. <property>setUserDnPatterns</property> array property) or by setting the
  3650. <property>userSearch</property> property. For the DN pattern-matching approach, a
  3651. standard Java pattern format is used, and the login name will be substituted for the
  3652. parameter <parameter>{0}</parameter>. The pattern should be relative to the DN that
  3653. the configured <interfacename>InitialDirContextFactory</interfacename> will bind to
  3654. (see the section on <link linkend="security-ldap-dircontextfactory">connecting to the
  3655. LDAP server</link> for more information on this). For example, if you are using an
  3656. LDAP server specified by the URL
  3657. <literal>ldap://monkeymachine.co.uk/dc=acegisecurity,dc=org</literal>, and have a
  3658. pattern <literal>uid={0},ou=greatapes</literal>, then a login name of "gorilla" will
  3659. map to a DN <literal>uid=gorilla,ou=greatapes,dc=acegisecurity,dc=org</literal>. Each
  3660. configured DN pattern will be tried in turn until a match is found. For information on
  3661. using a search, see the section on <link linkend="security-ldap-searchobjects">search
  3662. objects</link> below. A combination of the two approaches can also be used - the
  3663. patterns will be checked first and if no matching DN is found, the search will be
  3664. used. </para>
  3665. </sect3>
  3666. <sect3>
  3667. <title>BindAuthenticator</title>
  3668. <para>The class
  3669. <classname>org.acegisecurity.providers.ldap.authenticator.BindAuthenticator</classname>
  3670. implements the bind authentication strategy. It simply attempts to bind as the user.
  3671. </para>
  3672. </sect3>
  3673. <sect3>
  3674. <title>PasswordComparisonAuthenticator</title>
  3675. <para>The class
  3676. <classname>org.acegisecurity.providers.ldap.authenticator.PasswordComparisonAuthenticator</classname>
  3677. implements the password comparison authentication strategy.</para>
  3678. </sect3>
  3679. <sect3 id="security-ldap-authenticators-adauth">
  3680. <title>Active Directory Authentication</title>
  3681. <para>In addition to standard LDAP authentication (binding with a DN), Active Directory
  3682. has its own non-standard syntax for user authentication.
  3683. </para>
  3684. </sect3>
  3685. </sect2>
  3686. <sect2 id="security-ldap-dircontextfactory">
  3687. <title>Connecting to the LDAP Server</title>
  3688. <para>The beans discussed above have to be able to connect to the server. They both have
  3689. to be supplied with an <interfacename>InitialDirContextFactory</interfacename> instance.
  3690. Unless you have special requirements, this will usually be a
  3691. <classname>DefaultInitialDirContextFactory</classname> bean, which can be configured
  3692. with the URL of your LDAP server and optionally with the username and password of a
  3693. "manager" user which will be used by default when binding to the server (instead of
  3694. binding anonymously). It currently supports "simple" LDAP authentication.</para>
  3695. <para><classname>DefaultInitialDirContextFactory</classname> uses Sun's JNDI LDAP
  3696. implementation by default (the one that comes with the JDK). It also supports the
  3697. built in connection pooling offered by Sun's provider. Connections which are obtained
  3698. either anonymously or with the "manager" user's identity will be pooled automatically.
  3699. Connections obtained with a specific user's identity will not be pooled. Connection
  3700. pooling can be disabled completely by setting the <property>useConnectionPool</property>
  3701. property to false.
  3702. </para>
  3703. <para> See the <ulink
  3704. url="http://acegisecurity.org/multiproject/acegi-security/xref/org/acegisecurity/providers/ldap/DefaultInitialDirContextFactory.html"
  3705. >class Javadoc and source</ulink> for more information on this bean and its properties.
  3706. </para>
  3707. </sect2>
  3708. <sect2 id="security-ldap-searchobjects">
  3709. <title>LDAP Search Objects</title>
  3710. <para>Often more a more complicated strategy than simple DN-matching is required to locate
  3711. a user entry in the directory. This can be encapsulated in an
  3712. <interfacename>LdapUserSearch</interfacename> instance which can be supplied to the
  3713. authenticator implementations, for example, to allow them to locate a user. The supplied
  3714. implementation is <classname>FilterBasedLdapUserSearch</classname>.
  3715. </para>
  3716. <sect3>
  3717. <title><classname>FilterBasedLdapUserSearch</classname></title>
  3718. <para>This bean uses an LDAP filter to match the user object in the directory. The
  3719. process is explained in the Javadoc for the corresponding search method on the
  3720. <ulink
  3721. url="http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/directory/DirContext.html#search(javax.naming.Name,%20java.lang.String,%20java.lang.Object[],%20javax.naming.directory.SearchControls)">JDK
  3722. DirContext class</ulink>.
  3723. As explained there, the search filter can be supplied with parameters. For this class,
  3724. the only valid parameter is <parameter>{0}</parameter> which will be replaced with
  3725. the user's login name.
  3726. </para>
  3727. </sect3>
  3728. </sect2>
  3729. <sect2 id="security-ldap-config">
  3730. <title>Configuring the LDAP Provider</title>
  3731. <para>There is a version of the
  3732. <link linkend="security-sample">Contacts Sample Application</link> which
  3733. uses LDAP. You can copy the beans and filter setup from this as a starting
  3734. point for configuring your own application.
  3735. </para>
  3736. <para>
  3737. A typical configuration, using some of the beans we've discussed above, might look like this:
  3738. <programlisting>
  3739. &lt;bean id=&quot;initialDirContextFactory&quot; class=&quot;org.acegisecurity.providers.ldap.DefaultInitialDirContextFactory&quot;&gt;
  3740. &lt;constructor-arg value=&quot;ldap://monkeymachine:389/dc=acegisecurity,dc=org&quot;/&gt;
  3741. &lt;property name=&quot;managerDn&quot;&gt;&lt;value&gt;cn=manager,dc=acegisecurity,dc=org&lt;/value&gt;&lt;/property&gt;
  3742. &lt;property name=&quot;managerPassword&quot;&gt;&lt;value&gt;password&lt;/value&gt;&lt;/property&gt;
  3743. &lt;/bean&gt;
  3744. &lt;bean
  3745. id=&quot;userSearch&quot;
  3746. class=&quot;org.acegisecurity.providers.ldap.search.FilterBasedLdapUserSearch&quot;&gt;
  3747. &lt;property name=&quot;searchSubtree&quot;&gt;
  3748. &lt;value&gt;true&lt;/value&gt;
  3749. &lt;/property&gt;
  3750. &lt;property name=&quot;initialDirContextFactory&quot;&gt;
  3751. &lt;ref local=&quot;initialDirContextFactory&quot; /&gt;
  3752. &lt;/property&gt;
  3753. &lt;property name=&quot;searchFilter&quot;&gt;
  3754. &lt;value&gt;(uid={0})&lt;/value&gt;
  3755. &lt;/property&gt;
  3756. &lt;/bean&gt;
  3757. &lt;bean
  3758. id=&quot;ldapAuthProvider&quot; class=&quot;org.acegisecurity.providers.ldap.LdapAuthenticationProvider&quot;&gt;
  3759. &lt;constructor-arg&gt;
  3760. &lt;bean class=&quot;org.acegisecurity.providers.ldap.authenticator.BindAuthenticator&quot;&gt;
  3761. &lt;constructor-arg&gt;&lt;ref local=&quot;initialDirContextFactory&quot;/&gt;&lt;/constructor-arg&gt;
  3762. &lt;property name=&quot;userDnPatterns&quot;&gt;&lt;list&gt;&lt;value&gt;uid={0},ou=people&lt;/value&gt;&lt;/list&gt;&lt;/property&gt;
  3763. &lt;/bean&gt;
  3764. &lt;/constructor-arg&gt;
  3765. &lt;constructor-arg&gt;
  3766. &lt;bean class=&quot;org.acegisecurity.providers.ldap.populator.DefaultLdapAuthoritiesPopulator&quot;&gt;
  3767. &lt;constructor-arg&gt;&lt;ref local=&quot;initialDirContextFactory&quot;/&gt;&lt;/constructor-arg&gt;
  3768. &lt;constructor-arg&gt;&lt;value&gt;ou=groups&lt;/value&gt;&lt;/constructor-arg&gt;
  3769. &lt;property name=&quot;groupRoleAttribute&quot;&gt;&lt;value&gt;ou&lt;/value&gt;&lt;/property&gt;
  3770. &lt;/bean&gt;
  3771. &lt;/constructor-arg&gt;
  3772. &lt;/bean&gt;
  3773. </programlisting>
  3774. This would set up the provider to access an LDAP server with URL
  3775. <literal>ldap://monkeymachine:389/dc=acegisecurity,dc=org</literal>. Authentication will be performed by
  3776. attempting to bind with the DN <literal>uid=&lt;user-login-name&gt;,ou=people,dc=acegisecurity,dc=org</literal>.
  3777. After successful authentication, roles will be assigned to the user by searching under the DN
  3778. <literal>ou=groups,dc=acegisecurity,dc=org</literal> with the default filter <literal>(member=&lt;user's-DN&gt;)</literal>.
  3779. The role name will be taken from the <quote>ou</quote> attribute of each match.
  3780. </para>
  3781. <para>
  3782. We've also included the configuration for a user search object, which uses the filter
  3783. <literal>(uid=&lt;user-login-name&gt;)</literal>. This could be used
  3784. instead of the DN-pattern (or in addition to it), by setting the authenticator's
  3785. <property>userSearch</property> property. The autheticator would then call the search
  3786. object to obtain the correct user's DN before attempting to bind as this user.
  3787. </para>
  3788. </sect2>
  3789. </sect1>
  3790. <sect1 id="security-channels">
  3791. <title>Channel Security</title>
  3792. <sect2 id="security-channels-overview">
  3793. <title>Overview</title>
  3794. <para>In addition to coordinating the authentication and authorization
  3795. requirements of your application, the Acegi Security System for Spring
  3796. is also able to ensure unauthenticated web requests have certain
  3797. properties. These properties may include being of a particular
  3798. transport type, having a particular <literal>HttpSession</literal>
  3799. attribute set and so on. The most common requirement is for your web
  3800. requests to be received using a particular transport protocol, such as
  3801. HTTPS.</para>
  3802. <para>An important issue in considering transport security is that of
  3803. session hijacking. Your web container manages a
  3804. <literal>HttpSession</literal> by reference to a
  3805. <literal>jsessionid</literal> that is sent to user agents either via a
  3806. cookie or URL rewriting. If the <literal>jsessionid</literal> is ever
  3807. sent over HTTP, there is a possibility that session identifier can be
  3808. intercepted and used to impersonate the user after they complete the
  3809. authentication process. This is because most web containers maintain
  3810. the same session identifier for a given user, even after they switch
  3811. from HTTP to HTTPS pages.</para>
  3812. <para>If session hijacking is considered too significant a risk for
  3813. your particular application, the only option is to use HTTPS for every
  3814. request. This means the <literal>jsessionid</literal> is never sent
  3815. across an insecure channel. You will need to ensure your
  3816. <literal>web.xml</literal>-defined
  3817. <literal>&lt;welcome-file&gt;</literal> points to a HTTPS location,
  3818. and the application never directs the user to a HTTP location. The
  3819. Acegi Security System for Spring provides a solution to assist with
  3820. the latter.</para>
  3821. </sect2>
  3822. <sect2 id="security-channels-installation">
  3823. <title>Configuration</title>
  3824. <para>To utilise Acegi Security's channel security services, add the
  3825. following lines to <literal>web.xml</literal>:</para>
  3826. <para><programlisting>&lt;filter&gt;
  3827. &lt;filter-name&gt;Acegi Channel Processing Filter&lt;/filter-name&gt;
  3828. &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  3829. &lt;init-param&gt;
  3830. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  3831. &lt;param-value&gt;org.acegisecurity.securechannel.ChannelProcessingFilter&lt;/param-value&gt;
  3832. &lt;/init-param&gt;
  3833. &lt;/filter&gt;
  3834. &lt;filter-mapping&gt;
  3835. &lt;filter-name&gt;Acegi Channel Processing Filter&lt;/filter-name&gt;
  3836. &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  3837. &lt;/filter-mapping&gt;</programlisting></para>
  3838. <para>As usual when running <literal>FilterToBeanProxy</literal>, you
  3839. will also need to configure the filter in your application
  3840. context:</para>
  3841. <para><programlisting>&lt;bean id="channelProcessingFilter" class="org.acegisecurity.securechannel.ChannelProcessingFilter"&gt;
  3842. &lt;property name="channelDecisionManager"&gt;&lt;ref bean="channelDecisionManager"/&gt;&lt;/property&gt;
  3843. &lt;property name="filterInvocationDefinitionSource"&gt;
  3844. &lt;value&gt;
  3845. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  3846. \A/secure/.*\Z=REQUIRES_SECURE_CHANNEL
  3847. \A/acegilogin.jsp.*\Z=REQUIRES_SECURE_CHANNEL
  3848. \A/j_acegi_security_check.*\Z=REQUIRES_SECURE_CHANNEL
  3849. \A.*\Z=REQUIRES_INSECURE_CHANNEL
  3850. &lt;/value&gt;
  3851. &lt;/property&gt;
  3852. &lt;/bean&gt;
  3853. &lt;bean id="channelDecisionManager" class="org.acegisecurity.securechannel.ChannelDecisionManagerImpl"&gt;
  3854. &lt;property name="channelProcessors"&gt;
  3855. &lt;list&gt;
  3856. &lt;ref bean="secureChannelProcessor"/&gt;
  3857. &lt;ref bean="insecureChannelProcessor"/&gt;
  3858. &lt;/list&gt;
  3859. &lt;/property&gt;
  3860. &lt;/bean&gt;
  3861. &lt;bean id="secureChannelProcessor" class="org.acegisecurity.securechannel.SecureChannelProcessor"/&gt;
  3862. &lt;bean id="insecureChannelProcessor" class="org.acegisecurity.securechannel.InsecureChannelProcessor"/&gt;</programlisting></para>
  3863. <para>Like <literal>FilterSecurityInterceptor</literal>, Apache Ant
  3864. style paths are also supported by the
  3865. <literal>ChannelProcessingFilter</literal>.</para>
  3866. <para>The <literal>ChannelProcessingFilter</literal> operates by
  3867. filtering all web requests and determining the configuration
  3868. attributes that apply. It then delegates to the
  3869. <literal>ChannelDecisionManager</literal>. The default implementation,
  3870. <literal>ChannelDecisionManagerImpl</literal>, should suffice in most
  3871. cases. It simply delegates through the list of configured
  3872. <literal>ChannelProcessor</literal> instances. A
  3873. <literal>ChannelProcessor</literal> will review the request, and if it
  3874. is unhappy with the request (eg it was received across the incorrect
  3875. transport protocol), it will perform a redirect, throw an exception or
  3876. take whatever other action is appropriate.</para>
  3877. <para>Included with the Acegi Security System for Spring are two
  3878. concrete <literal>ChannelProcessor</literal> implementations:
  3879. <literal>SecureChannelProcessor</literal> ensures requests with a
  3880. configuration attribute of <literal>REQUIRES_SECURE_CHANNEL</literal>
  3881. are received over HTTPS, whilst
  3882. <literal>InsecureChannelProcessor</literal> ensures requests with a
  3883. configuration attribute of
  3884. <literal>REQUIRES_INSECURE_CHANNEL</literal> are received over HTTP.
  3885. Both implementations delegate to a
  3886. <literal>ChannelEntryPoint</literal> if the required transport
  3887. protocol is not used. The two <literal>ChannelEntryPoint</literal>
  3888. implementations included with Acegi Security simply redirect the
  3889. request to HTTP and HTTPS as appropriate. Appropriate defaults are
  3890. assigned to the <literal>ChannelProcessor</literal> implementations
  3891. for the configuration attribute keywords they respond to and the
  3892. <literal>ChannelEntryPoint</literal> they delegate to, although you
  3893. have the ability to override these using the application
  3894. context.</para>
  3895. <para>Note that the redirections are absolute (eg
  3896. <literal>http://www.company.com:8080/app/page</literal>), not relative
  3897. (eg <literal>/app/page</literal>). During testing it was discovered
  3898. that Internet Explorer 6 Service Pack 1 has a bug whereby it does not
  3899. respond correctly to a redirection instruction which also changes the
  3900. port to use. Accordingly, absolute URLs are used in conjunction with
  3901. bug detection logic in the <literal>PortResolverImpl</literal> that is
  3902. wired up by default to many Acegi Security beans. Please refer to the
  3903. JavaDocs for <literal>PortResolverImpl</literal> for further
  3904. details.</para>
  3905. </sect2>
  3906. <sect2 id="security-channels-usage">
  3907. <title>Usage</title>
  3908. <para>Once configured, using the channel security filter is very easy.
  3909. Simply request pages without regard to the protocol (ie HTTP or HTTPS)
  3910. or port (eg 80, 8080, 443, 8443 etc). Obviously you'll still need a
  3911. way of making the initial request (probably via the
  3912. <literal>web.xml</literal> <literal>&lt;welcome-file&gt;</literal> or
  3913. a well-known home page URL), but once this is done the filter will
  3914. perform redirects as defined by your application context.</para>
  3915. <para>You can also add your own <literal>ChannelProcessor</literal>
  3916. implementations to the <literal>ChannelDecisionManagerImpl</literal>.
  3917. For example, you might set a <literal>HttpSession</literal> attribute
  3918. when a human user is detected via a "enter the contents of this
  3919. graphic" procedure. Your <literal>ChannelProcessor</literal> would
  3920. respond to say <literal>REQUIRES_HUMAN_USER</literal> configuration
  3921. attributes and redirect to an appropriate entry point to start the
  3922. human user validation process if the <literal>HttpSession</literal>
  3923. attribute is not currently set.</para>
  3924. <para>To decide whether a security check belongs in a
  3925. <literal>ChannelProcessor</literal> or an
  3926. <literal>AccessDecisionVoter</literal>, remember that the former is
  3927. designed to handle unauthenticated requests, whilst the latter is
  3928. designed to handle authenticated requests. The latter therefore has
  3929. access to the granted authorities of the authenticated principal. In
  3930. addition, problems detected by a <literal>ChannelProcessor</literal>
  3931. will generally cause a HTTP/HTTPS redirection so its requirements can
  3932. be met, whilst problems detected by an
  3933. <literal>AccessDecisionVoter</literal> will ultimately result in an
  3934. <literal>AccessDeniedException</literal> (depending on the governing
  3935. <literal>AccessDecisionManager</literal>).</para>
  3936. </sect2>
  3937. </sect1>
  3938. <sect1 id="acls">
  3939. <title>Instance-Based Access Control List (ACL) Security</title>
  3940. <sect2 id="acls-overview">
  3941. <title>Overview</title>
  3942. <para>Complex applications often will find the need to define access
  3943. permissions not simply at a web request or method invocation level.
  3944. Instead, security decisions need to comprise both who
  3945. (<literal>Authentication</literal>), where
  3946. (<literal>MethodInvocation</literal>) and what
  3947. (<literal>SomeDomainObject</literal>). In other words, authorization
  3948. decisions also need to consider the actual domain object instance
  3949. subject of a method invocation.</para>
  3950. <para>Imagine you're designing an application for a pet clinic. There
  3951. will be two main groups of users of your Spring-based application:
  3952. staff of the pet clinic, as well as the pet clinic's customers. The
  3953. staff will have access to all of the data, whilst your customers will
  3954. only be able to see their own customer records. To make it a little
  3955. more interesting, your customers can allow other users to see their
  3956. customer records, such as their "puppy preschool "mentor or president
  3957. of their local "Pony Club". Using Acegi Security System for Spring as
  3958. the foundation, you have several approaches that can be
  3959. used:<orderedlist>
  3960. <listitem>
  3961. <para>Write your business methods to enforce the security. You
  3962. could consult a collection within the
  3963. <literal>Customer</literal> domain object instance to determine
  3964. which users have access. By using the
  3965. <literal>SecurityContextHolder.getContext().getAuthentication()</literal>,
  3966. you'll be able to access the <literal>Authentication</literal>
  3967. object.</para>
  3968. </listitem>
  3969. <listitem>
  3970. <para>Write an <literal>AccessDecisionVoter</literal> to enforce
  3971. the security from the <literal>GrantedAuthority[]</literal>s
  3972. stored in the <literal>Authentication</literal> object. This
  3973. would mean your <literal>AuthenticationManager</literal> would
  3974. need to populate the <literal>Authentication</literal> with
  3975. custom <literal>GrantedAuthority</literal>[]s representing each
  3976. of the <literal>Customer</literal> domain object instances the
  3977. principal has access to.</para>
  3978. </listitem>
  3979. <listitem>
  3980. <para>Write an <literal>AccessDecisionVoter</literal> to enforce
  3981. the security and open the target <literal>Customer</literal>
  3982. domain object directly. This would mean your voter needs access
  3983. to a DAO that allows it to retrieve the
  3984. <literal>Customer</literal> object. It would then access the
  3985. <literal>Customer</literal> object's collection of approved
  3986. users and make the appropriate decision.</para>
  3987. </listitem>
  3988. </orderedlist></para>
  3989. <para>Each one of these approaches is perfectly legitimate. However,
  3990. the first couples your authorization checking to your business code.
  3991. The main problems with this include the enhanced difficulty of unit
  3992. testing and the fact it would be more difficult to reuse the
  3993. <literal>Customer</literal> authorization logic elsewhere. Obtaining
  3994. the <literal>GrantedAuthority[]</literal>s from the
  3995. <literal>Authentication</literal> object is also fine, but will not
  3996. scale to large numbers of <literal>Customer</literal>s. If a user
  3997. might be able to access 5,000 <literal>Customer</literal>s (unlikely
  3998. in this case, but imagine if it were a popular vet for a large Pony
  3999. Club!) the amount of memory consumed and time required to construct
  4000. the <literal>Authentication</literal> object would be undesirable. The
  4001. final method, opening the <literal>Customer</literal> directly from
  4002. external code, is probably the best of the three. It achieves
  4003. separation of concerns, and doesn't misuse memory or CPU cycles, but
  4004. it is still inefficient in that both the
  4005. <literal>AccessDecisionVoter</literal> and the eventual business
  4006. method itself will perform a call to the DAO responsible for
  4007. retrieving the <literal>Customer</literal> object. Two accesses per
  4008. method invocation is clearly undesirable. In addition, with every
  4009. approach listed you'll need to write your own access control list
  4010. (ACL) persistence and business logic from scratch.</para>
  4011. <para>Fortunately, there is another alternative, which we'll talk
  4012. about below.</para>
  4013. </sect2>
  4014. <sect2 id="acls-acl-package">
  4015. <title>The org.acegisecurity.acl Package</title>
  4016. <para>The <literal>org.acegisecurity.acl</literal> package is very
  4017. simple, comprising only a handful of interfaces and a single class, as
  4018. shown in Figure 6. It provides the basic foundation for access control
  4019. list (ACL) lookups.</para>
  4020. <para><mediaobject>
  4021. <imageobject role="html">
  4022. <imagedata align="center" fileref="images/ACLSecurity.gif"
  4023. format="GIF" />
  4024. </imageobject>
  4025. <caption>
  4026. <para>Figure 6: Access Control List Manager</para>
  4027. </caption>
  4028. </mediaobject></para>
  4029. <para>The central interface is <literal>AclManager</literal>, which is
  4030. defined by two methods:</para>
  4031. <para><programlisting>public AclEntry[] getAcls(java.lang.Object domainInstance);
  4032. public AclEntry[] getAcls(java.lang.Object domainInstance, Authentication authentication);</programlisting></para>
  4033. <para><literal>AclManager</literal> is intended to be used as a
  4034. collaborator against your business objects, or, more desirably,
  4035. <literal>AccessDecisionVoter</literal>s. This means you use Spring's
  4036. normal <literal>ApplicationContext</literal> features to wire up your
  4037. <literal>AccessDecisionVoter</literal> (or business method) with an
  4038. <literal>AclManager</literal>. Consideration was given to placing the
  4039. ACL information in the <literal>ContextHolder</literal>, but it was
  4040. felt this would be inefficient both in terms of memory usage as well
  4041. as the time spent loading potentially unused ACL information. The
  4042. trade-off of needing to wire up a collaborator for those objects
  4043. requiring ACL information is rather minor, particularly in a
  4044. Spring-managed application.</para>
  4045. <para>The first method of the <literal>AclManager</literal> will
  4046. return all ACLs applying to the domain object instance passed to it.
  4047. The second method does the same, but only returns those ACLs which
  4048. apply to the passed <literal>Authentication</literal> object.</para>
  4049. <para>The <literal>AclEntry</literal> interface returned by
  4050. <literal>AclManager</literal> is merely a marker interface. You will
  4051. need to provide an implementation that reflects that ACL permissions
  4052. for your application.</para>
  4053. <para>Rounding out the <literal>org.acegisecurity.acl</literal>
  4054. package is an <literal>AclProviderManager</literal> class, with a
  4055. corresponding <literal>AclProvider</literal> interface.
  4056. <literal>AclProviderManager</literal> is a concrete implementation of
  4057. <literal>AclManager</literal>, which iterates through registered
  4058. <literal>AclProvider</literal>s. The first
  4059. <literal>AclProvider</literal> that indicates it can authoritatively
  4060. provide ACL information for the presented domain object instance will
  4061. be used. This is very similar to the
  4062. <literal>AuthenticationProvider</literal> interface used for
  4063. authentication.</para>
  4064. <para>With this background, let's now look at a usable ACL
  4065. implementation.</para>
  4066. </sect2>
  4067. <sect2 id="acls-masking">
  4068. <title>Integer Masked ACLs</title>
  4069. <para>Acegi Security System for Spring includes a production-quality
  4070. ACL provider implementation, which is shown in Figure 7.</para>
  4071. <para><mediaobject>
  4072. <imageobject role="html">
  4073. <imagedata align="center" fileref="images/BasicAclProvider.gif"
  4074. format="GIF" />
  4075. </imageobject>
  4076. <caption>
  4077. <para>Figure 7: Basic ACL Manager</para>
  4078. </caption>
  4079. </mediaobject></para>
  4080. <para>The implementation is based on integer masking, which is
  4081. commonly used for ACL permissions given its flexibility and speed.
  4082. Anyone who has used Unix's <literal>chmod</literal> command will know
  4083. all about this type of permission masking (eg <literal>chmod
  4084. 777</literal>). You'll find the classes and interfaces for the integer
  4085. masking ACL package under
  4086. <literal>org.acegisecurity.acl.basic</literal>.</para>
  4087. <para>Extending the <literal>AclEntry</literal> interface is a
  4088. <literal>BasicAclEntry</literal> interface, with the main methods
  4089. shown below:</para>
  4090. <para><programlisting>public AclObjectIdentity getAclObjectIdentity();
  4091. public AclObjectIdentity getAclObjectParentIdentity();
  4092. public int getMask();
  4093. public java.lang.Object getRecipient();</programlisting></para>
  4094. <para>As shown, each <literal>BasicAclEntry</literal> has four main
  4095. properties. The <literal>mask</literal> is the integer that represents
  4096. the permissions granted to the <literal>recipient</literal>. The
  4097. <literal>aclObjectIdentity</literal> is able to identify the domain
  4098. object instance for which the ACL applies, and the
  4099. <literal>aclObjectParentIdentity</literal> optionally specifies the
  4100. parent of the domain object instance. Multiple
  4101. <literal>BasicAclEntry</literal>s usually exist against a single
  4102. domain object instance, and as suggested by the parent identity
  4103. property, permissions granted higher in the object hierarchy will
  4104. trickle down and be inherited (unless blocked by integer zero).</para>
  4105. <para><literal>BasicAclEntry</literal> implementations typically
  4106. provide convenience methods, such as
  4107. <literal>isReadAllowed()</literal>, to avoid application classes
  4108. needing to perform bit masking themselves. The
  4109. <literal>SimpleAclEntry</literal> and
  4110. <literal>AbstractBasicAclEntry</literal> demonstrate and provide much
  4111. of this bit masking logic.</para>
  4112. <para>The <literal>AclObjectIdentity</literal> itself is merely a
  4113. marker interface, so you need to provide implementations for your
  4114. domain objects. However, the package does include a
  4115. <literal>NamedEntityObjectIdentity</literal> implementation which will
  4116. suit many needs. The <literal>NamedEntityObjectIdentity</literal>
  4117. identifies a given domain object instance by the classname of the
  4118. instance and the identity of the instance. A
  4119. <literal>NamedEntityObjectIdentity</literal> can be constructed
  4120. manually (by calling the constructor and providing the classname and
  4121. identity <literal>String</literal>s), or by passing in any domain
  4122. object that contains a <literal>getId()</literal> method.</para>
  4123. <para>The actual <literal>AclProvider</literal> implementation is
  4124. named <literal>BasicAclProvider</literal>. It has adopted a similar
  4125. design to that used by the authentication-related
  4126. <literal>DaoAuthenticationProvder</literal>. Specifically, you define
  4127. a <literal>BasicAclDao</literal> against the provider, so different
  4128. ACL repository types can be accessed in a pluggable manner. The
  4129. <literal>BasicAclProvider</literal> also supports pluggable cache
  4130. providers (with Acegi Security System for Spring including an
  4131. implementation that fronts EH-CACHE).</para>
  4132. <para>The <literal>BasicAclDao</literal> interface is very simple to
  4133. implement:</para>
  4134. <para><programlisting>public BasicAclEntry[] getAcls(AclObjectIdentity aclObjectIdentity);</programlisting></para>
  4135. <para>A <literal>BasicAclDao</literal> implementation needs to
  4136. understand the presented <literal>AclObjectIdentity</literal> and how
  4137. it maps to a storage repository, find the relevant records, and create
  4138. appropriate <literal>BasicAclEntry</literal> objects and return
  4139. them.</para>
  4140. <para>Acegi Security includes a single <literal>BasicAclDao</literal>
  4141. implementation called <literal>JdbcDaoImpl</literal>. As implied by
  4142. the name, <literal>JdbcDaoImpl</literal> accesses ACL information from
  4143. a JDBC database. There is also an extended version of this DAO,
  4144. <literal>JdbcExtendedDaoImpl</literal>, which provides CRUD operations
  4145. on the JDBC database, although we won't discuss these features here.
  4146. The default database schema and some sample data will aid in
  4147. understanding its function:</para>
  4148. <para><programlisting>CREATE TABLE acl_object_identity (
  4149. id IDENTITY NOT NULL,
  4150. object_identity VARCHAR_IGNORECASE(250) NOT NULL,
  4151. parent_object INTEGER,
  4152. acl_class VARCHAR_IGNORECASE(250) NOT NULL,
  4153. CONSTRAINT unique_object_identity UNIQUE(object_identity),
  4154. FOREIGN KEY (parent_object) REFERENCES acl_object_identity(id)
  4155. );
  4156. CREATE TABLE acl_permission (
  4157. id IDENTITY NOT NULL,
  4158. acl_object_identity INTEGER NOT NULL,
  4159. recipient VARCHAR_IGNORECASE(100) NOT NULL,
  4160. mask INTEGER NOT NULL,
  4161. CONSTRAINT unique_recipient UNIQUE(acl_object_identity, recipient),
  4162. FOREIGN KEY (acl_object_identity) REFERENCES acl_object_identity(id)
  4163. );
  4164. INSERT INTO acl_object_identity VALUES (1, 'corp.DomainObject:1', null, 'org.acegisecurity.acl.basic.SimpleAclEntry');
  4165. INSERT INTO acl_object_identity VALUES (2, 'corp.DomainObject:2', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');
  4166. INSERT INTO acl_object_identity VALUES (3, 'corp.DomainObject:3', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');
  4167. INSERT INTO acl_object_identity VALUES (4, 'corp.DomainObject:4', 1, 'org.acegisecurity.acl.basic.SimpleAclEntry');
  4168. INSERT INTO acl_object_identity VALUES (5, 'corp.DomainObject:5', 3, 'org.acegisecurity.acl.basic.SimpleAclEntry');
  4169. INSERT INTO acl_object_identity VALUES (6, 'corp.DomainObject:6', 3, 'org.acegisecurity.acl.basic.SimpleAclEntry');
  4170. INSERT INTO acl_permission VALUES (null, 1, 'ROLE_SUPERVISOR', 1);
  4171. INSERT INTO acl_permission VALUES (null, 2, 'ROLE_SUPERVISOR', 0);
  4172. INSERT INTO acl_permission VALUES (null, 2, 'marissa', 2);
  4173. INSERT INTO acl_permission VALUES (null, 3, 'scott', 14);
  4174. INSERT INTO acl_permission VALUES (null, 6, 'scott', 1);</programlisting></para>
  4175. <para>As can be seen, database-specific constraints are used
  4176. extensively to ensure the integrity of the ACL information. If you
  4177. need to use a different database (Hypersonic SQL statements are shown
  4178. above), you should try to implement equivalent constraints.</para>
  4179. <para>The <literal>JdbcDaoImpl</literal> will only respond to requests
  4180. for <literal>NamedEntityObjectIdentity</literal>s. It converts such
  4181. identities into a single <literal>String</literal>, comprising
  4182. the<literal> NamedEntityObjectIdentity.getClassname()</literal> +
  4183. <literal>":"</literal> +
  4184. <literal>NamedEntityObjectIdentity.getId()</literal>. This yields the
  4185. type of <literal>object_identity</literal> values shown above. As
  4186. indicated by the sample data, each database row corresponds to a
  4187. single <literal>BasicAclEntry</literal>. As stated earlier and
  4188. demonstrated by <literal>corp.DomainObject:2</literal> in the above
  4189. sample data, each domain object instance will often have multiple
  4190. <literal>BasicAclEntry</literal>[]s.</para>
  4191. <para>As <literal>JdbcDaoImpl</literal> is required to return concrete
  4192. <literal>BasicAclEntry</literal> classes, it needs to know which
  4193. <literal>BasicAclEntry</literal> implementation it is to create and
  4194. populate. This is the role of the <literal>acl_class</literal> column.
  4195. <literal>JdbcDaoImpl</literal> will create the indicated class and set
  4196. its <literal>mask</literal>, <literal>recipient</literal>,
  4197. <literal>aclObjectIdentity</literal> and
  4198. <literal>aclObjectParentIdentity</literal> properties.</para>
  4199. <para>As you can probably tell from the sample data, the
  4200. <literal>parent_object_identity</literal> value can either be null or
  4201. in the same format as the <literal>object_identity</literal>. If
  4202. non-null, <literal>JdbcDaoImpl</literal> will create a
  4203. <literal>NamedEntityObjectIdentity</literal> to place inside the
  4204. returned <literal>BasicAclEntry</literal> class.</para>
  4205. <para>Returning to the <literal>BasicAclProvider</literal>, before it
  4206. can poll the <literal>BasicAclDao</literal> implementation it needs to
  4207. convert the domain object instance it was passed into an
  4208. <literal>AclObjectIdentity</literal>.
  4209. <literal>BasicAclProvider</literal> has a <literal>protected
  4210. AclObjectIdentity obtainIdentity(Object domainInstance)</literal>
  4211. method that is responsible for this. As a protected method, it enables
  4212. subclasses to easily override. The normal implementation checks
  4213. whether the passed domain object instance implements the
  4214. <literal>AclObjectIdentityAware</literal> interface, which is merely a
  4215. getter for an <literal>AclObjectIdentity</literal>. If the domain
  4216. object does implement this interface, that is the identity returned.
  4217. If the domain object does not implement this interface, the method
  4218. will attempt to create an <literal>AclObjectIdentity</literal> by
  4219. passing the domain object instance to the constructor of a class
  4220. defined by the
  4221. <literal>BasicAclProvider.getDefaultAclObjectIdentity()</literal>
  4222. method. By default the defined class is
  4223. <literal>NamedEntityObjectIdentity</literal>, which was described in
  4224. more detail above. Therefore, you will need to either (i) provide a
  4225. <literal>getId()</literal> method on your domain objects, (ii)
  4226. implement <literal>AclObjectIdentityAware</literal> on your domain
  4227. objects, (iii) provide an alternative
  4228. <literal>AclObjectIdentity</literal> implementation that will accept
  4229. your domain object in its constructor, or (iv) override the
  4230. <literal>obtainIdentity(Object)</literal> method.</para>
  4231. <para>Once the <literal>AclObjectIdentity</literal> of the domain
  4232. object instance is determined, the <literal>BasicAclProvider</literal>
  4233. will poll the DAO to obtain its <literal>BasicAclEntry</literal>[]s.
  4234. If any of the entries returned by the DAO indicate there is a parent,
  4235. that parent will be polled, and the process will repeat until there is
  4236. no further parent. The permissions assigned to a
  4237. <literal>recipient</literal> closest to the domain object instance
  4238. will always take priority and override any inherited permissions. From
  4239. the sample data above, the following inherited permissions would
  4240. apply:</para>
  4241. <para><programlisting>--- Mask integer 0 = no permissions
  4242. --- Mask integer 1 = administer
  4243. --- Mask integer 2 = read
  4244. --- Mask integer 6 = read and write permissions
  4245. --- Mask integer 14 = read and write and create permissions
  4246. ---------------------------------------------------------------------
  4247. --- *** INHERITED RIGHTS FOR DIFFERENT INSTANCES AND RECIPIENTS ***
  4248. --- INSTANCE RECIPIENT PERMISSION(S) (COMMENT #INSTANCE)
  4249. ---------------------------------------------------------------------
  4250. --- 1 ROLE_SUPERVISOR Administer
  4251. --- 2 ROLE_SUPERVISOR None (overrides parent #1)
  4252. --- marissa Read
  4253. --- 3 ROLE_SUPERVISOR Administer (from parent #1)
  4254. --- scott Read, Write, Create
  4255. --- 4 ROLE_SUPERVISOR Administer (from parent #1)
  4256. --- 5 ROLE_SUPERVISOR Administer (from parent #3)
  4257. --- scott Read, Write, Create (from parent #3)
  4258. --- 6 ROLE_SUPERVISOR Administer (from parent #3)
  4259. --- scott Administer (overrides parent #3)</programlisting></para>
  4260. <para>So the above explains how a domain object instance has its
  4261. <literal>AclObjectIdentity</literal> discovered, and the
  4262. <literal>BasicAclDao</literal> will be polled successively until an
  4263. array of inherited permissions is constructed for the domain object
  4264. instance. The final step is to determine the
  4265. <literal>BasicAclEntry</literal>[]s that are actually applicable to a
  4266. given <literal>Authentication</literal> object.</para>
  4267. <para>As you would recall, the <literal>AclManager</literal> (and all
  4268. delegates, up to and including <literal>BasicAclProvider</literal>)
  4269. provides a method which returns only those
  4270. <literal>BasicAclEntry</literal>[]s applying to a passed
  4271. <literal>Authentication</literal> object.
  4272. <literal>BasicAclProvider</literal> delivers this functionality by
  4273. delegating the filtering operation to an
  4274. <literal>EffectiveAclsResolver</literal> implementation. The default
  4275. implementation,
  4276. <literal>GrantedAuthorityEffectiveAclsResolver</literal>, will iterate
  4277. through the <literal>BasicAclEntry</literal>[]s and include only those
  4278. where the <literal>recipient</literal> is equal to either the
  4279. <literal>Authentication</literal>'s <literal>principal</literal> or
  4280. any of the <literal>Authentication</literal>'s
  4281. <literal>GrantedAuthority</literal>[]s. Please refer to the JavaDocs
  4282. for more information.</para>
  4283. <mediaobject>
  4284. <imageobject role="html">
  4285. <imagedata align="center" fileref="images/Permissions.gif"
  4286. format="GIF" />
  4287. </imageobject>
  4288. <caption>
  4289. <para>Figure 8: ACL Instantiation Approach</para>
  4290. </caption>
  4291. </mediaobject>
  4292. </sect2>
  4293. <sect2 id="acls-conclusion">
  4294. <title>Conclusion</title>
  4295. <para>Acegi Security's instance-specific ACL packages shield you from
  4296. much of the complexity of developing your own ACL approach. The
  4297. interfaces and classes detailed above provide a scalable, customisable
  4298. ACL solution that is decoupled from your application code. Whilst the
  4299. reference documentation may suggest complexity, the basic
  4300. implementation is able to support most typical applications
  4301. out-of-the-box.</para>
  4302. </sect2>
  4303. </sect1>
  4304. <sect1 id="security-filters">
  4305. <title>Filters</title>
  4306. <sect2 id="security-filters-overview">
  4307. <title>Overview</title>
  4308. <para>The Acegi Security System for Spring uses filters extensively.
  4309. Each filter is covered in detail in a respective section of this
  4310. document. This section includes information that applies to all
  4311. filters.</para>
  4312. </sect2>
  4313. <sect2 id="security-filters-filtertobeanproxy">
  4314. <title>FilterToBeanProxy</title>
  4315. <para>Most filters are configured using the
  4316. <literal>FilterToBeanProxy</literal>. An example configuration from
  4317. <literal>web.xml</literal> follows:</para>
  4318. <para><programlisting>&lt;filter&gt;
  4319. &lt;filter-name&gt;Acegi HTTP Request Security Filter&lt;/filter-name&gt;
  4320. &lt;filter-class&gt;org.acegisecurity.util.FilterToBeanProxy&lt;/filter-class&gt;
  4321. &lt;init-param&gt;
  4322. &lt;param-name&gt;targetClass&lt;/param-name&gt;
  4323. &lt;param-value&gt;org.acegisecurity.ClassThatImplementsFilter&lt;/param-value&gt;
  4324. &lt;/init-param&gt;
  4325. &lt;/filter&gt;</programlisting></para>
  4326. <para>Notice that the filter in <literal>web.xml</literal> is actually
  4327. a <literal>FilterToBeanProxy</literal>, and not the filter that will
  4328. actually implements the logic of the filter. What
  4329. <literal>FilterToBeanProxy</literal> does is delegate the
  4330. <literal>Filter</literal>'s methods through to a bean which is
  4331. obtained from the Spring application context. This enables the bean to
  4332. benefit from the Spring application context lifecycle support and
  4333. configuration flexibility. The bean must implement
  4334. <literal>javax.servlet.Filter</literal>.</para>
  4335. <para>The <literal>FilterToBeanProxy</literal> only requires a single
  4336. initialization parameter, <literal>targetClass</literal> or
  4337. <literal>targetBean</literal>. The <literal>targetClass</literal>
  4338. parameter locates the first object in the application context of the
  4339. specified class, whilst <literal>targetBean</literal> locates the
  4340. object by bean name. Like standard Spring web applications, the
  4341. <literal>FilterToBeanProxy</literal> accesses the application context
  4342. via<literal>
  4343. WebApplicationContextUtils.getWebApplicationContext(ServletContext)</literal>,
  4344. so you should configure a <literal>ContextLoaderListener</literal> in
  4345. <literal>web.xml</literal>.</para>
  4346. <para>There is a lifecycle issue to consider when hosting
  4347. <literal>Filter</literal>s in an IoC container instead of a servlet
  4348. container. Specifically, which container should be responsible for
  4349. calling the <literal>Filter</literal>'s "startup" and "shutdown"
  4350. methods? It is noted that the order of initialization and destruction
  4351. of a <literal>Filter</literal> can vary by servlet container, and this
  4352. can cause problems if one <literal>Filter</literal> depends on
  4353. configuration settings established by an earlier initialized
  4354. <literal>Filter</literal>. The Spring IoC container on the other hand
  4355. has more comprehensive lifecycle/IoC interfaces (such as
  4356. <literal>InitializingBean</literal>,
  4357. <literal>DisposableBean</literal>, <literal>BeanNameAware</literal>,
  4358. <literal>ApplicationContextAware</literal> and many others) as well as
  4359. a well-understood interface contract, predictable method invocation
  4360. ordering, autowiring support, and even options to avoid implementing
  4361. Spring interfaces (eg the <literal>destroy-method</literal> attribute
  4362. in Spring XML). For this reason we recommend the use of Spring
  4363. lifecycle services instead of servlet container lifecycle services
  4364. wherever possible. By default <literal>FilterToBeanProxy</literal>
  4365. will not delegate <literal>init(FilterConfig)</literal> and
  4366. <literal>destroy()</literal> methods through to the proxied bean. If
  4367. you do require such invocations to be delegated, set the
  4368. <literal>lifecycle</literal> initialization parameter to
  4369. <literal>servlet-container-managed</literal>.</para>
  4370. </sect2>
  4371. <sect2 id="security-filters-filterchainproxy">
  4372. <title>FilterChainProxy</title>
  4373. <para>We strongly recommend to use <literal>FilterChainProxy</literal>
  4374. instead of adding multiple filters to
  4375. <literal>web.xml</literal>.</para>
  4376. <para>Whilst <literal>FilterToBeanProxy</literal> is a very useful
  4377. class, the problem is that the lines of code required for
  4378. <literal>&lt;filter&gt;</literal> and
  4379. <literal>&lt;filter-mapping&gt;</literal> entries in
  4380. <literal>web.xml</literal> explodes when using more than a few
  4381. filters. To overcome this issue, Acegi Security provides a
  4382. <literal>FilterChainProxy</literal> class. It is wired using a
  4383. <literal>FilterToBeanProxy</literal> (just like in the example above),
  4384. but the target class is
  4385. <literal>org.acegisecurity.util.FilterChainProxy</literal>. The filter
  4386. chain is then declared in the application context, using code such as
  4387. this:</para>
  4388. <para><programlisting>&lt;bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy"&gt;
  4389. &lt;property name="filterInvocationDefinitionSource"&gt;
  4390. &lt;value&gt;
  4391. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
  4392. PATTERN_TYPE_APACHE_ANT
  4393. /webServices/**=httpSessionContextIntegrationFilterWithASCFalse,basicProcessingFilter,securityEnforcementFilter
  4394. /**=httpSessionContextIntegrationFilterWithASCTrue,authenticationProcessingFilter,securityEnforcementFilter
  4395. &lt;/value&gt;
  4396. &lt;/property&gt;
  4397. &lt;/bean&gt;</programlisting></para>
  4398. <para>You may notice similarities with the way
  4399. <literal>SecurityEnforcementFilter</literal> is declared. Both regular
  4400. expressions and Ant Paths are supported, and the most specific URIs
  4401. appear first. At runtime the <literal>FilterChainProxy</literal> will
  4402. locate the first URI pattern that matches the current web request.
  4403. Each of the corresponding configuration attributes represent the name
  4404. of a bean defined in the application context. The filters will then be
  4405. invoked in the order they are specified, with standard
  4406. <literal>FilterChain</literal> behaviour being respected (a
  4407. <literal>Filter</literal> can elect not to proceed with the chain if
  4408. it wishes to end processing).</para>
  4409. <para>As you can see, <literal>FitlerChainProxy</literal> requires the
  4410. duplication of filter names for different request patterns (in the
  4411. above example, <literal>httpSessionContextIntegrationFilter</literal>
  4412. and <literal>securityEnforcementFilter</literal> are duplicated). This
  4413. design decision was made to enable <literal>FilterChainProxy</literal>
  4414. to specify different <literal>Filter</literal> invocation orders for
  4415. different URI patterns, and also to improve both the expressiveness
  4416. (in terms of regular expressions, Ant Paths, and any custom
  4417. <literal>FilterInvocationDefinitionSource</literal> implementations)
  4418. and clarity of which <literal>Filter</literal>s should be
  4419. invoked.</para>
  4420. <para>You may have noticed we have declared two
  4421. <literal>HttpSessionContextIntegrationFilter</literal>s in the filter
  4422. chain (<literal>ASC</literal> is short for
  4423. <literal>allowSessionCreation</literal>, a property of
  4424. <literal>HttpSessionContextIntegrationFilter</literal>). As web
  4425. services will never present a <literal>jsessionid</literal> on future
  4426. requests, creating <literal>HttpSession</literal>s for such user
  4427. agents would be wasteful. If you had a high-volume application which
  4428. required maximum scalability, we recommend you use the approach shown
  4429. above. For smaller applications, using a single
  4430. <literal>HttpSessionContextIntegrationFilter</literal> (with its
  4431. default <literal>allowSessionCreation</literal> as
  4432. <literal>true</literal>) would likely be sufficient.</para>
  4433. <para>In relation to lifecycle issues, the
  4434. <literal>FilterChainProxy</literal> will always delegate
  4435. <literal>init(FilterConfig)</literal> and <literal>destroy()</literal>
  4436. methods through to the underlaying <literal>Filter</literal>s if such
  4437. methods are called against <literal>FilterChainProxy</literal> itself.
  4438. In this case, <literal>FilterChainProxy</literal> guarantees to only
  4439. initialize and destroy each <literal>Filter</literal> once,
  4440. irrespective of how many times it is declared by the
  4441. <literal>FilterInvocationDefinitionSource</literal>. You control the
  4442. overall choice as to whether these methods are called or not via the
  4443. <literal>lifecycle</literal> initialization parameter of the
  4444. <literal>FilterToBeanProxy</literal> that proxies
  4445. <literal>FilterChainProxy</literal>. As discussed above, by default
  4446. any servlet container lifecycle invocations are not delegated through
  4447. to <literal>FilterChainProxy</literal>.</para>
  4448. </sect2>
  4449. <sect2 id="security-filters-order">
  4450. <title>Filter Ordering</title>
  4451. <para>The order that filters are defined in <literal>web.xml</literal>
  4452. is important. NB: THE FILTER ORDER CHANGED FROM VERSION 0.8.0.</para>
  4453. <para>Irrespective of which filters you are actually using, the order
  4454. of the <literal>&lt;filter-mapping&gt;</literal>s should be as
  4455. follows:</para>
  4456. <orderedlist>
  4457. <listitem>
  4458. <para><literal>ChannelProcessingFilter</literal>, because it might
  4459. need to redirect to a different protocol</para>
  4460. </listitem>
  4461. <listitem>
  4462. <para><literal>ConcurrentSessionFilter</literal>, because it
  4463. doesn't use any <literal>SecurityContextHolder</literal>
  4464. functionality but needs to update the
  4465. <literal>SessionRegistry</literal> to reflect ongoing requests
  4466. from the principal</para>
  4467. </listitem>
  4468. <listitem>
  4469. <para><literal>HttpSessionContextIntegrationFilter</literal>, so a
  4470. <literal>Context</literal> can be setup in the
  4471. <literal>SecurityContextHolder</literal> at the beginning of a web
  4472. request, and any changes to the Context can be copied to the
  4473. <literal>HttpSession</literal> when the web request ends (ready
  4474. for use with the next web request)</para>
  4475. </listitem>
  4476. <listitem>
  4477. <para>Authentication processing mechanisms -
  4478. <literal>AuthenticationProcessingFilter</literal>,
  4479. <literal>CasProcessingFilter</literal>,
  4480. <literal>BasicProcessingFilter, HttpRequestIntegrationFilter,
  4481. JbossIntegrationFilter</literal> etc - so that the
  4482. <literal>SecurityContextHolder</literal> can be modified to
  4483. contain a valid <literal>Authentication</literal> request
  4484. token</para>
  4485. </listitem>
  4486. <listitem>
  4487. <para>The <literal>ContextHolderAwareRequestFilter</literal>, if
  4488. you are using it to install an Acegi Security aware
  4489. <literal>HttpServletRequestWrapper</literal> into your servlet
  4490. container</para>
  4491. </listitem>
  4492. <listitem>
  4493. <para><literal>RememberMeProcessingFilter</literal>, so that if no
  4494. earlier authentication processing mechanism updated the
  4495. <literal>SecurityContextHolder</literal>, and the request presents
  4496. a cookie that enables remember-me services to take place, a
  4497. suitable remembered
  4498. <literal><literal>Authentication</literal></literal> object will
  4499. be put there</para>
  4500. </listitem>
  4501. <listitem>
  4502. <para><literal>AnonymousProcessingFilter</literal>, so that if no
  4503. earlier authentication processing mechanism updated the
  4504. <literal>SecurityContextHolder</literal>, an anonymous
  4505. <literal>Authentication</literal> object will be put there</para>
  4506. </listitem>
  4507. <listitem>
  4508. <para><literal>SecurityEnforcementFilter</literal>, to protect web
  4509. URIs and catch any Acegi Security exceptions so that an
  4510. appropriate <literal>AuthenticationEntryPoint</literal> can be
  4511. launched</para>
  4512. </listitem>
  4513. </orderedlist>
  4514. <para>All of the above filters use
  4515. <literal>FilterToBeanProxy</literal> or
  4516. <literal>FilterChainProxy</literal>, which is discussed in the
  4517. previous sections. It is recommended that a single
  4518. <literal>FilterToBeanProxy</literal> proxy through to a single
  4519. <literal>FilterChainProxy</literal> for each application, with that
  4520. <literal>FilterChainProxy</literal> defining all of the Acegi Security
  4521. <literal>Filter</literal>s.</para>
  4522. <para>If you're using SiteMesh, ensure the Acegi Security filters
  4523. execute before the SiteMesh filters are called. This enables the
  4524. <literal>SecurityContextHolder</literal> to be populated in time for
  4525. use by SiteMesh decorators.</para>
  4526. </sect2>
  4527. </sect1>
  4528. <sect1 id="security-sample">
  4529. <title>Contacts Sample Application</title>
  4530. <para>Included with the Acegi Security System for Spring is a very
  4531. simple application that can demonstrate the basic security facilities
  4532. provided by the system (and confirm your Container Adapter is properly
  4533. configured if you're using one).</para>
  4534. <para>If you build from CVS, the Contacts sample application includes
  4535. three deployable versions:
  4536. <literal>acegi-security-sample-contacts-filter.war</literal> is
  4537. configured with the HTTP Session Authentication approach. The
  4538. <literal><literal>acegi-security-sample-contacts-ca.war</literal></literal>
  4539. is configured to use a Container Adapter. Finally,
  4540. <literal>acegi-security-sample-contacts-cas.war</literal> is designed to
  4541. work with a Yale CAS server. If you're just wanting to see how the
  4542. sample application works, please use
  4543. <literal><literal>acegi-security-sample-contacts-filter.war</literal></literal>
  4544. as it does not require special configuration of your container. This is
  4545. also the artifact included in ofiical release ZIPs.</para>
  4546. <para>To deploy, simply copy the relevant WAR file from the Acegi
  4547. Security System for Spring distribution into your container’s
  4548. <literal>webapps</literal> directory.</para>
  4549. <para>After starting your container, check the application can load.
  4550. Visit
  4551. <literal>http://localhost:</literal><literal><literal>8080/</literal>acegi-security-sample-contacts-filter</literal>
  4552. (or whichever URL is appropriate for your web container and the WAR you
  4553. deployed). A random contact should be displayed. Click "Refresh" several
  4554. times and you will see different contacts. The business method that
  4555. provides this random contact is not secured.</para>
  4556. <para>Next, click "Debug". You will be prompted to authenticate, and a
  4557. series of usernames and passwords are suggested on that page. Simply
  4558. authenticate with any of these and view the resulting page. It should
  4559. contain a success message similar to the following:</para>
  4560. <blockquote>
  4561. <para>Context on SecurityContextHolder is of type:
  4562. org.acegisecurity.context.SecurityContextImpl</para>
  4563. <para>The Context implements SecurityContext.</para>
  4564. <para>Authentication object is of type:
  4565. org.acegisecurity.adapters.PrincipalAcegiUserToken</para>
  4566. <para>Authentication object as a String:
  4567. org.acegisecurity.adapters.PrincipalAcegiUserToken@e9a7c2: Username:
  4568. marissa; Password: [PROTECTED]; Authenticated: true; Granted
  4569. Authorities: ROLE_TELLER, ROLE_SUPERVISOR</para>
  4570. <para>Authentication object holds the following granted
  4571. authorities:</para>
  4572. <para>ROLE_TELLER (getAuthority(): ROLE_TELLER)</para>
  4573. <para>ROLE_SUPERVISOR (getAuthority(): ROLE_SUPERVISOR)</para>
  4574. <para>SUCCESS! Your [container adapter|web filter] appears to be
  4575. properly configured!</para>
  4576. </blockquote>
  4577. <para>If you receive a different message, and deployed
  4578. <literal>acegi-security-sample-contacts-ca.war</literal>, check you have
  4579. properly configured your Container Adapter as described elsewhere in
  4580. this reference guide.</para>
  4581. <para>Once you successfully receive the above message, return to the
  4582. sample application's home page and click "Manage". You can then try out
  4583. the application. Notice that only the contacts available to the
  4584. currently logged on user are displayed, and only users with
  4585. <literal>ROLE_SUPERVISOR</literal> are granted access to delete their
  4586. contacts. Behind the scenes, the
  4587. <literal>MethodSecurityInterceptor</literal> is securing the business
  4588. objects. If you're using
  4589. <literal><literal>acegi-security-sample-contacts-filter.war</literal></literal>
  4590. or <literal>acegi-security-sample-contacts-cas.war</literal>, the
  4591. <literal>FilterSecurityInterceptor</literal> is also securing the HTTP
  4592. requests. If using either of these WARs, be sure to try visiting
  4593. <literal>http://localhost:8080/contacts/secure/super</literal>, which
  4594. will demonstrate access being denied by the
  4595. <literal>SecurityEnforcementFilter</literal>. Note the sample
  4596. application enables you to modify the access control lists associated
  4597. with different contacts. Be sure to give this a try and understand how
  4598. it works by reviewing the sample application's application context XML
  4599. files.</para>
  4600. <para>The Contacts sample application also include a
  4601. <literal>client</literal> directory. Inside you will find a small
  4602. application that queries the backend business objects using several web
  4603. services protocols. This demonstrates how to use the Acegi Security
  4604. System for Spring for authentication with Spring remoting protocols. To
  4605. try this client, ensure your servlet container is still running the
  4606. Contacts sample application, and then execute <literal>client marissa
  4607. koala</literal>. The command-line parameters respectively represent the
  4608. username to use, and the password to use. Note that you may need to edit
  4609. <literal>client.properties</literal> to use a different target
  4610. URL.</para>
  4611. <para>Please note the sample application's <literal>client</literal>
  4612. does not currently support CAS. You can still give it a try, though, if
  4613. you're ambitious: try <literal>client _cas_stateless_
  4614. YOUR-SERVICE-TICKET-ID</literal>.</para>
  4615. </sect1>
  4616. <sect1 id="security-become-involved">
  4617. <title>Become Involved</title>
  4618. <para>We welcome you to become involved in the Acegi Security System for
  4619. Spring project. There are many ways of contributing, including reading
  4620. the mailing list and responding to questions from other people, writing
  4621. new code, improving existing code, assisting with documentation, or
  4622. simply making suggestions. Please read our project policies web page
  4623. that is available on the Acegi Security home page. This explains the
  4624. path to become a committer, and the administration approaches we use
  4625. with the project.</para>
  4626. <para>SourceForge provides CVS services for the project, allowing
  4627. anybody to access the latest code. If you wish to contribute new code,
  4628. please observe the following requirements. These exist to maintain the
  4629. quality and consistency of the project:</para>
  4630. <itemizedlist>
  4631. <listitem>
  4632. <para>Use a suitable IDE Jalopy plug-in to convert your code into
  4633. the project's consistent style</para>
  4634. </listitem>
  4635. <listitem>
  4636. <para>Ensure your code does not break any unit tests (run the Maven
  4637. <literal>test:test</literal> goal)</para>
  4638. </listitem>
  4639. <listitem>
  4640. <para>If you have added new code, please provide suitable unit tests
  4641. (use the Maven <literal>clover:html-report</literal> to view
  4642. coverage)</para>
  4643. </listitem>
  4644. <listitem>
  4645. <para>Join the acegisecurity-developer and acegisecurity-cvs mailing
  4646. lists so you're in the loop</para>
  4647. </listitem>
  4648. <listitem>
  4649. <para>Use CamelCase</para>
  4650. </listitem>
  4651. <listitem>
  4652. <para>Add code contributions to JIRA</para>
  4653. </listitem>
  4654. <listitem>
  4655. <para>Add a CVS <literal>$Id: index.xml,v 1.3 2004/04/02 21:12:25
  4656. fbos Exp $</literal> tag to the JavaDocs for any new class you
  4657. create</para>
  4658. </listitem>
  4659. </itemizedlist>
  4660. </sect1>
  4661. <sect1 id="security-further">
  4662. <title>Further Information</title>
  4663. <para>Questions and comments on the Acegi Security System for Spring are
  4664. welcome. Please use the Spring Community Forum web site at
  4665. <ulink url="http://forum.springframework.org"></ulink>. You're also welcome
  4666. to join the acegisecurity-developer mailing list. Our project home page
  4667. (where you can obtain the latest release of the project and access to
  4668. CVS, mailing lists, forums etc) is at
  4669. <ulink url="http://acegisecurity.org"></ulink>.</para>
  4670. </sect1>
  4671. </chapter>
  4672. </book>