acegi.xml 328 KB

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