acegi.xml 328 KB

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