acegi.xml 283 KB

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