聽聽聽聽聽聽聽聽聽聽 Acegi瀹夊叏緋葷粺錛屾槸涓涓敤浜嶴pring Framework鐨勫畨鍏ㄦ鏋訛紝鑳藉鍜岀洰鍓嶆祦琛岀殑Web瀹瑰櫒鏃犵紳闆嗘垚銆傚畠浣跨敤浜哠pring鐨勬柟寮忔彁渚涗簡瀹夊叏鍜岃璇佸畨鍏ㄦ湇鍔★紝鍖呮嫭浣跨敤Bean Context錛屾嫤鎴櫒鍜岄潰鍚戞帴鍙g殑緙栫▼鏂瑰紡銆傚洜姝わ紝Acegi瀹夊叏緋葷粺鑳藉杞繪澗鍦伴傜敤浜庡鏉傜殑瀹夊叏闇姹傘?br />聽聽聽聽聽聽 瀹夊叏娑夊強鍒頒袱涓笉鍚岀殑姒傚康錛岃璇佸拰鎺堟潈銆傚墠鑰呮槸鍏充簬紜鐢ㄦ埛鏄惁紜疄鏄粬浠墍瀹gО鐨勮韓浠姐傛巿鏉冨垯鏄叧浜庣‘璁ょ敤鎴鋒槸鍚︽湁鍏佽鎵ц涓涓壒瀹氱殑鎿嶄綔銆?br />聽聽聽聽聽聽 鍦ˋcegi瀹夊叏緋葷粺涓紝闇瑕佽璁よ瘉鐨勭敤鎴鳳紝緋葷粺鎴栦唬鐞嗙О涓?Principal"銆侫cegi瀹夊叏緋葷粺鍜屽叾浠栫殑瀹夊叏緋葷粺涓嶅悓錛屽畠騫舵病鏈夎鑹插拰鐢ㄦ埛緇勭殑姒傚康銆?br />Acegi緋葷粺璁捐
聽聽鍏抽敭緇勪歡
聽聽聽聽聽聽Acegi瀹夊叏緋葷粺鍖呭惈浠ヤ笅涓冧釜鍏抽敭鐨勫姛鑳界粍浠訛細
聽聽聽聽聽聽聽聽1 Authentication瀵硅薄錛屽寘鍚簡Principal錛孋redential鍜孭rincipal鐨勬巿鏉冧俊鎭傚悓鏃惰繕鍙互鍖呭惈鍏充簬鍙戣搗璁よ瘉璇鋒眰鐨勫鎴風殑鍏朵粬淇℃伅錛屽IP鍦板潃銆?br />聽聽聽聽聽聽聽聽2 ContextHolder瀵硅薄錛屼嬌鐢═hreadLocal鍌ㄥ瓨Authentication瀵硅薄鐨勫湴鏂廣?br />聽聽聽聽聽聽聽聽3 AuthenticationManager錛岀敤浜庤璇丆ontextHolder涓殑Authentication瀵硅薄銆?br />聽聽聽聽聽聽聽聽4 AccessDecissionManager錛岀敤浜庢巿鏉冧竴涓壒瀹氱殑鎿嶄綔銆?br />聽聽聽聽聽聽聽聽5 RunAsManager錛屽綋鎵ц鐗瑰畾鐨勬搷浣滄椂錛岀敤浜庨夋嫨鎬у湴鏇挎崲Authentication瀵硅薄銆?br />聽聽聽聽聽聽聽聽6 Secure Object鎷︽埅鍣紝鐢ㄤ簬鍗忚皟AuthenticationManager錛孉ccessDecissionManager錛孯unAsManager鍜岀壒瀹氭搷浣滅殑鎵ц銆?br />聽聽聽聽聽聽聽聽7 ObjectDefinitionSource錛屽寘鍚簡鐗瑰畾鎿嶄綔鐨勬巿鏉冨畾涔夈?br />聽聽聽聽聽聽榪欎竷涓叧閿殑鍔熻兘緇勪歡鐨勫叧緋誨涓嬪浘鎵紺猴紙鍥句腑鐏拌壊閮ㄥ垎鏄叧閿粍浠訛級錛?br />
瀹夊叏綆$悊瀵硅薄聽聽聽聽聽聽 Acegi瀹夊叏緋葷粺鐩墠鏀寔涓ょ被瀹夊叏綆$悊瀵硅薄銆?br />聽聽聽聽聽聽 絎竴綾葷殑瀹夊叏綆$悊瀵硅薄綆$悊AOP Alliance鐨凪ethodInvocation錛屽紑鍙戜漢鍛樺彲浠ョ敤瀹冩潵淇濇姢Spring瀹瑰櫒涓殑涓氬姟瀵硅薄銆備負浜嗕嬌Spring綆$悊鐨凚ean鍙互浣滀負MethodInvocation鏉ヤ嬌鐢紝Bean鍙互閫氳繃ProxyFactoryBean鍜孊eanNameAutoProxyCreator鏉ョ鐞嗭紝灝卞儚鍦⊿pring鐨勪簨鍔$鐞嗕竴鏍蜂嬌鐢ㄣ?br />聽聽聽聽聽聽 絎簩綾繪槸FilterInvocation銆傚畠鐢ㄨ繃婊ゅ櫒錛團ilter錛夋潵鍒涘緩錛屽茍綆鍗曞湴鍖呰浜咹TTP鐨凷ervletRequest錛孲ervletResponse鍜孎ilterChain銆侳ilterInvocation鍙互鐢ㄦ潵淇濇姢HTTP璧勬簮銆傞氬父錛屽紑鍙戜漢鍛樺茍涓嶉渶瑕佷簡瑙e畠鐨勫伐浣滄満鍒訛紝鍥犱負浠栦滑鍙渶瑕佸皢Filter鍔犲叆web.xml錛孉cegi瀹夊叏緋葷粺灝卞彲浠ュ伐浣滀簡銆?br />
瀹夊叏閰嶇疆鍙傛暟聽聽聽聽聽聽 姣忎釜瀹夊叏綆$悊瀵硅薄閮藉彲浠ユ弿榪版暟閲忎笉闄愮殑鍚勭瀹夊叏璁よ瘉璇鋒眰銆備緥濡傦紝MethodInvocation瀵硅薄鍙互鎻忚堪甯︽湁浠繪剰鍙傛暟鐨勪換鎰忔柟娉曠殑璋冪敤錛岃孎ilterInvocation鍙互鎻忚堪浠繪剰鐨凥TTP URL銆?br />聽聽聽聽聽聽 Acegi瀹夊叏緋葷粺闇瑕佽褰曞簲鐢ㄤ簬姣忎釜璁よ瘉璇鋒眰鐨勫畨鍏ㄩ厤緗弬鏁般備緥濡傦紝瀵逛簬BankManager.getBalance錛坕nt accountNumber錛夋柟娉曞拰BankManager.approveLoan錛坕nt applicationNumber錛夋柟娉曪紝瀹冧滑闇瑕佺殑璁よ瘉璇鋒眰鐨勫畨鍏ㄩ厤緗緢涓嶇浉鍚屻?br />聽聽聽聽聽聽 涓轟簡淇濆瓨涓嶅悓鐨勮璇佽姹傜殑瀹夊叏閰嶇疆錛岄渶瑕佷嬌鐢ㄩ厤緗弬鏁般備粠瀹炵幇鐨勮瑙掓潵鐪嬶紝閰嶇疆鍙傛暟浣跨敤ConfigAttribute鎺ュ彛鏉ヨ〃紺恒侫cegi瀹夊叏緋葷粺鎻愪緵浜咰onfigAttribute鎺ュ彛鐨勪竴涓疄鐜幫紝SecurityConfig錛屽畠鎶婇厤緗弬鏁頒繚瀛樹負涓涓瓧絎︿覆銆?br />聽聽聽聽聽聽 ConfigAttributeDefinition綾繪槸ConfigAttribute瀵硅薄鐨勪竴涓畝鍗曠殑瀹瑰櫒錛屽畠淇濆瓨浜嗗拰鐗瑰畾璇鋒眰鐩稿叧鐨凜onfigAttribute鐨勯泦鍚堛?br />聽聽聽聽聽聽 褰撳畨鍏ㄦ嫤鎴櫒鏀跺埌涓涓畨鍏ㄨ璇佽姹傛椂錛岄渶瑕佸喅瀹氬簲鐢ㄥ摢涓涓厤緗弬鏁般傛崲鍙ヨ瘽璇達紝瀹冮渶瑕佹壘鍑哄簲鐢ㄤ簬榪欎釜璇鋒眰鐨凜onfigAttributeDefinition瀵硅薄銆傝繖涓煡鎵劇殑榪囩▼鏄敱ObjectDefinitionSource鎺ュ彛鏉ュ鐞嗙殑銆傝繖涓帴鍙g殑涓昏鏂規硶鏄痯ublic ConfigAttributeDefinition getAttributes(Object object)錛屽叾涓璒bject鍙傛暟鏄竴涓畨鍏ㄧ鐞嗗璞°傚洜涓哄畨鍏ㄧ鐞嗗璞″寘鍚湁璁よ瘉璇鋒眰鐨勮緇嗕俊鎭紝鎵浠bjectDefinitionSource鎺ュ彛鐨勫疄鐜扮被鍙互浠庝腑鑾峰緱鎵闇鐨勮緇嗕俊鎭紝浠ユ煡鎵劇浉鍏崇殑ConfigAttributeDefiniton瀵硅薄銆?br />
Acegi濡備綍宸ヤ綔聽聽聽聽聽聽 涓轟簡璇存槑Acegi瀹夊叏緋葷粺濡備綍宸ヤ綔錛屾垜浠鎯充竴涓嬌鐢ˋcegi鐨勪緥瀛愩傞氬父錛屼竴涓畨鍏ㄧ郴緇熼渶瑕佸彂鎸ヤ綔鐢紝瀹冨繀欏誨畬鎴愪互涓嬬殑宸ヤ綔錛?br />聽聽聽聽聽聽1 棣栧厛錛岀郴緇熶粠瀹㈡埛绔姹備腑鑾峰緱Principal鍜孋redential錛?br />聽聽聽聽聽聽2 鐒跺悗緋葷粺璁よ瘉Principal鍜孋redential淇℃伅錛?br />聽聽聽聽聽聽3 濡傛灉璁よ瘉閫氳繃錛岀郴緇熷彇鍑篜rincipal鐨勬巿鏉冧俊鎭紱
聽聽聽聽聽聽4 鎺ヤ笅鏉ワ紝瀹㈡埛绔彂璧鋒搷浣滆姹傦紱
聽聽聽聽聽聽5 緋葷粺鏍規嵁棰勫厛閰嶇疆鐨勫弬鏁版鏌rincipal瀵逛簬璇ユ搷浣滅殑鎺堟潈錛?br />聽聽聽聽聽聽6 濡傛灉鎺堟潈媯鏌ラ氳繃鍒欐墽琛屾搷浣滐紝鍚﹀垯鎷掔粷銆?br />聽聽聽聽聽聽閭d箞錛孉cegi瀹夊叏緋葷粺鏄浣曞畬鎴愯繖浜涘伐浣滅殑鍛紵棣栧厛錛屾垜浠潵鐪嬬湅Acegi瀹夊叏緋葷粺鐨勮璇佸拰鎺堟潈鐨勭浉鍏崇被錛?
聽聽聽聽聽聽瀹夊叏鎷︽埅鍣ㄧ殑鎶借薄鍩虹被錛屽畠鍖呭惈鏈変袱涓鐞嗙被錛孉uthenticationManager鍜孉ccessDecisionManager銆侫uthenticationManager鐢ㄤ簬璁よ瘉ContextHolder涓殑Authentication瀵硅薄錛堝寘鍚簡Principal錛孋redential鍜孭rincipal鐨勬巿鏉冧俊鎭級錛汚ccessDecissionManager鍒欑敤浜庢巿鏉冧竴涓壒瀹氱殑鎿嶄綔銆?br />
聽聽聽聽聽聽涓嬮潰鏉ョ湅涓涓狹ethodSecurityInterceptor鐨勪緥瀛愶細
聽聽聽聽聽聽<bean id="bankManagerSecurity"
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 class="net.sf.acegisecurity.intercept.method.MethodSecurityInterceptor">
聽聽聽聽聽聽聽聽聽聽聽聽 <property name="validateConfigAttributes">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<value>true</value>
聽聽聽聽聽聽聽聽聽聽聽聽</property>
聽聽聽聽聽聽聽聽聽聽聽聽<property name="authenticationManager">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <ref bean="authenticationManager"/>
聽聽聽聽聽聽聽聽聽聽聽聽</property>
聽聽聽聽聽聽聽聽聽聽聽聽<property name="accessDecisionManager">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<ref bean="accessDecisionManager"/>
聽聽聽聽聽聽聽聽聽聽聽聽</property>
聽聽聽聽聽聽聽聽聽聽聽聽<property name="objectDefinitionSource">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<value>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 net.sf.acegisecurity.context.BankManager.delete*=
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 ROLE_SUPERVISOR,RUN_AS_SERVER
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 net.sf.acegisecurity.context.BankManager.getBalance=
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 ROLE_TELLER,ROLE_SUPERVISOR,BANKSECURITY_CUSTOMER,RUN_
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</value>
聽聽聽聽聽聽聽聽聽聽聽聽</property>
聽聽聽聽聽聽</bean>
聽聽聽聽聽聽涓婇潰鐨勯厤緗枃浠朵腑錛孧ethodSecurityInterceptor鏄疉bstractSecurityInterceptor鐨勪竴涓疄鐜扮被銆傚畠鍖呭惈浜嗕袱涓鐞嗗櫒錛宎uthenticationManager鍜宎ccessDecisionManager銆傝繖涓よ呯殑閰嶇疆濡備笅錛?br />聽聽聽聽聽聽
<bean id="authenticationDao" class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <property name="dataSource"><ref bean="dataSource"/></property>
聽聽聽聽聽聽</bean>
聽聽聽聽聽聽<bean id="daoAuthenticationProvider"
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <property name="authenticationDao"><ref bean="authenticationDao"/></property>
聽聽聽聽聽聽</bean>
聽聽聽聽聽聽<bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <property name="providers">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<list><ref bean="daoAuthenticationProvider"/></list>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 </property>
聽聽聽聽聽聽</bean>
聽聽聽聽聽聽<bean id="roleVoter" class="net.sf.acegisecurity.vote.RoleVoter"/>
聽聽聽聽聽聽<bean id="accessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <property name="allowIfAllAbstainDecisions"><value>false</value></property>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <property name="decisionVoters">
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽<list><ref bean="roleVoter"/></list>
聽聽聽聽聽聽聽聽聽聽聽聽聽聽 </property>
聽聽聽聽聽聽</bean>
聽聽聽聽聽聽 鍑嗗宸ヤ綔鍋氬ソ浜嗭紝鐜板湪鎴戜滑鏉ョ湅鐪婣cegi瀹夊叏緋葷粺鏄浣曞疄鐜拌璇佸拰鎺堟潈鏈哄埗鐨勩備互浣跨敤HTTP BASIC璁よ瘉鐨勫簲鐢ㄤ負渚嬪瓙錛屽畠鍖呮嫭涓嬮潰鐨勬楠わ細
聽聽聽聽聽聽 1. 鐢ㄦ埛鐧誨綍緋葷粺錛孉cegi浠巃cegisecurity.ui瀛愮郴緇熺殑瀹夊叏鎷︽埅鍣紙濡侭asicProcessingFilter錛変腑寰楀埌鐢ㄦ埛鐨勭櫥褰曚俊鎭紙鍖呮嫭Principal鍜孋redential錛夊茍鏀懼叆Authentication瀵硅薄錛屽茍淇濆瓨鍦–ontextHolder瀵硅薄涓紱
聽聽聽聽聽聽 2. 瀹夊叏鎷︽埅鍣ㄥ皢Authentication瀵硅薄浜ょ粰AuthenticationManager榪涜韜喚璁よ瘉錛屽鏋滆璇侀氳繃錛岃繑鍥炲甫鏈塒rincipal鎺堟潈淇℃伅鐨凙uthentication瀵硅薄銆傛鏃禖ontextHolder瀵硅薄鐨凙uthentication瀵硅薄宸叉嫢鏈塒rincipal鐨勮緇嗕俊鎭紱
聽聽聽聽聽聽 3. 鐢ㄦ埛鐧誨綍鎴愬姛鍚庯紝緇х畫榪涜涓氬姟鎿嶄綔錛?br />聽聽聽聽聽聽 4. 瀹夊叏鎷︽埅鍣紙bankManagerSecurity錛夋敹鍒板鎴風鎿嶄綔璇鋒眰鍚庯紝灝嗘搷浣滆姹傜殑鏁版嵁鍖呰鎴愬畨鍏ㄧ鐞嗗璞★紙FilterInvocation鎴朚ethodInvocation瀵硅薄錛夛紱
聽聽聽聽聽聽 5. 鐒跺悗錛屼粠閰嶇疆鏂囦歡錛圤bjectDefinitionSource錛変腑璇誨嚭鐩稿叧鐨勫畨鍏ㄩ厤緗弬鏁癈onfigAttributeDefinition錛?br />聽聽聽聽聽聽 6. 鎺ョ潃錛屽畨鍏ㄦ嫤鎴櫒鍙栧嚭ContextHolder涓殑Authentication瀵硅薄錛屾妸瀹冧紶閫掔粰AuthenticationManager榪涜韜喚璁よ瘉錛屽茍鐢ㄨ繑鍥炲兼洿鏂癈ontextHolder鐨凙uthentication瀵硅薄錛?br />聽聽聽聽聽聽 7. 灝咥uthentication瀵硅薄錛孋onfigAttributeDefinition瀵硅薄鍜屽畨鍏ㄧ鐞嗗璞★紙secure Object錛変氦緇橝ccessDecisionManager錛屾鏌rincipal鐨勬搷浣滄巿鏉冿紱
聽聽聽聽聽聽 8. 濡傛灉鎺堟潈媯鏌ラ氳繃鍒欐墽琛屽鎴風璇鋒眰鐨勬搷浣滐紝鍚﹀垯鎷掔粷錛?br />
AccessDecisionVoter聽聽聽聽聽聽 娉ㄦ剰涓婅妭鐨刟ccessDecisionManager鏄竴涓狝ffirmativeBased綾伙紝瀹冨浜庣敤鎴鋒巿鏉冪殑鎶曠エ絳栫暐鏄紝鍙閫氳繃鍏朵腑鐨勪竴涓巿鏉冩姇紲ㄦ鏌ワ紝鍗沖彲閫氳繃錛涘畠鐨刟llowIfAllAbstainDecisions灞炴у兼槸false錛屾剰鎬濇槸濡傛灉鎵鏈夌殑鎺堟潈鎶曠エ鏄兘鏄純鏉冿紝鍒欓氫笉榪囨巿鏉冩鏌ャ?br />聽聽聽聽聽聽 Acegi瀹夊叏緋葷粺鍖呮嫭浜嗗嚑涓熀浜庢姇紲ㄧ瓥鐣ョ殑AccessDecisionManager錛屼笂鑺傜殑RoleVoter灝辨槸鍏朵腑鐨勪竴涓姇紲ㄧ瓥鐣ュ疄鐜幫紝瀹冩槸AccessDecisionVoter鐨勪竴涓瓙綾匯侫ccessDecisionVoter鐨勫叿浣撳疄鐜扮被閫氳繃鎶曠エ鏉ヨ繘琛屾巿鏉冨喅絳栵紝AccessDecisionManager鍒欐牴鎹姇紲ㄧ粨鏋滄潵鍐沖畾鏄氳繃鎺堟潈媯鏌ワ紝榪樻槸鎶涘嚭AccessDeniedException渚嬪銆?br />聽聽聽聽聽聽 AccessDecisionVoter鎺ュ彛鍏辨湁涓変釜鏂規硶錛?br />public int vote(Authentication authentication, Object object, ConfigAttributeDefinition config);
public boolean supports(ConfigAttribute attribute);
public boolean supports(Class clazz);
聽聽聽聽聽聽 鍏朵腑鐨剉ote鏂規硶榪斿洖int榪斿洖鍊鹼紝瀹冧滑鏄疉ccessDecisionVoter鐨勪笁涓潤鎬佹垚鍛樺睘鎬э細ACCESS_ABSTAIN,錛孉CCESS_DENIED鍜孉CCESS_GRANTED錛屽畠浠垎鍒槸寮冩潈錛屽惁鍐沖拰璧炴垚銆?br />聽聽聽聽聽聽 Acegi瀹夊叏緋葷粺涓紝浣跨敤鎶曠エ絳栫暐鐨凙ccessDecisionManager鍏辨湁涓変釜鍏蜂綋瀹炵幇綾伙細AffirmativeBased銆丆onsensusBased鍜孶nanimousBased銆傚畠浠殑鎶曠エ絳栫暐鏄紝AffirmativeBased綾誨彧闇鏈変竴涓姇紲ㄨ禐鎴愬嵆鍙氳繃錛汣onsensusBased綾婚渶瑕佸ぇ澶氭暟鎶曠エ璧炴垚鍗沖彲閫氳繃錛涜孶nanimousBased綾婚渶瑕佹墍鏈夌殑鎶曠エ璧炴垚鎵嶈兘閫氳繃銆?br />聽聽聽聽聽聽 RoleVoter綾繪槸涓涓狝cegi瀹夊叏緋葷粺AccessDecisionVoter鎺ュ彛鐨勫疄鐜般傚鏋淐onfigAttribute浠OLE_寮澶達紝RoleVoter鍒欒繘琛屾姇紲ㄣ傚鏋淕rantedAuthority鐨刧etAutority鏂規硶鐨凷tring榪斿洖鍊煎尮閰嶄竴涓垨澶氫釜浠OLE_寮澶寸殑ConfigAttribute錛屽垯鎶曠エ閫氳繃錛屽惁鍒欎笉閫氳繃銆傚鏋滄病鏈変互ROLE_寮澶寸殑ConfigAttribute錛孯oleVoter鍒欏純鏉冦?br />
瀹夊叏鎷︽埅鍣?/b>
聽聽鎷︽埅鍣ㄥ浣曞伐浣?br />聽聽MethodInvocation鎷︽埅鍣?br />聽聽FilterInvocation鎷︽埅鍣?br />璁よ瘉
聽聽璁よ瘉璇鋒眰
聽聽璁よ瘉綆$悊鍣?br />聽聽Authentication Provider
鎺堟潈
聽聽Access Decision Manager
聽聽Voting Decision Manager
聽聽鎺堟潈綆$悊鎺ㄨ崘
ContextHolder鐨勭敤鎴鋒帴鍙?br />聽聽鐢ㄦ埛鎺ュ彛鐩爣
聽聽HTTP浼氳瘽璁よ瘉
聽聽HTTP Basic璁よ瘉
1銆丩og4j鐨勬蹇?br />聽聽 Log4j涓湁涓変釜涓昏鐨勭粍浠訛紝瀹冧滑鍒嗗埆鏄疞ogger銆丄ppender鍜孡ayout錛孡og4j 鍏佽寮鍙戜漢鍛樺畾涔夊涓狶ogger錛屾瘡涓狶ogger鎷ユ湁鑷繁鐨勫悕瀛楋紝Logger涔嬮棿閫氳繃鍚嶅瓧鏉ヨ〃鏄庨毝灞炲叧緋匯傛湁涓涓狶ogger縐頒負Root錛屽畠姘歌繙 瀛樺湪錛屼笖涓嶈兘閫氳繃鍚嶅瓧媯绱㈡垨寮曠敤錛屽彲浠ラ氳繃Logger.getRootLogger()鏂規硶鑾峰緱錛屽叾瀹僉ogger閫氳繃 Logger.getLogger(String name)鏂規硶銆?br />聽聽 Appender鍒欐槸鐢ㄦ潵鎸囨槑灝嗘墍鏈夌殑log淇℃伅瀛樻斁鍒頒粈涔堝湴鏂癸紝Log4j涓敮鎸佸縐峚ppender錛屽 console銆乫iles銆丟UI components銆丯T Event Loggers絳夛紝涓涓狶ogger鍙互鎷ユ湁澶氫釜Appender錛屼篃灝辨槸浣犳棦鍙互灝哃og淇℃伅杈撳嚭鍒板睆騫曪紝鍚屾椂瀛樺偍鍒頒竴涓枃浠朵腑銆?br />聽聽 Layout鐨勪綔鐢ㄦ槸鎺у埗Log淇℃伅鐨勮緭鍑烘柟寮忥紝涔熷氨鏄牸寮忓寲杈撳嚭鐨勪俊鎭?br />聽聽 Log4j涓皢瑕佽緭鍑虹殑Log淇℃伅瀹氫箟浜?縐嶇駭鍒紝渚濇涓篋EBUG銆両NFO銆乄ARN銆丒RROR鍜孎ATAL錛屽綋杈撳嚭鏃訛紝鍙湁綰у埆楂樿繃閰嶇疆涓瀹氱殑 綰у埆鐨勪俊鎭墠鑳界湡姝g殑杈撳嚭錛岃繖鏍峰氨寰堟柟渚跨殑鏉ラ厤緗笉鍚屾儏鍐典笅瑕佽緭鍑虹殑鍐呭錛岃屼笉闇瑕佹洿鏀逛唬鐮侊紝榪欑偣瀹炲湪鏄柟渚垮晩銆?br />
2銆丩og4j鐨勯厤緗枃浠?br />聽聽铏界劧鍙互涓嶇敤閰嶇疆鏂囦歡錛岃屽湪紼嬪簭涓疄鐜伴厤緗紝浣嗚繖縐嶆柟娉曞湪濡備粖鐨勭郴緇熷紑鍙戜腑鏄劇劧鏄笉鍙彇鐨勶紝鑳介噰鐢ㄩ厤緗枃浠剁殑鍦版柟涓瀹氫竴瀹氳鐢ㄩ厤緗枃浠躲侺og4j鏀寔涓?縐嶆牸寮忕殑閰嶇疆鏂囦歡錛歑ML鏍煎紡鍜孞ava鐨刾roperty鏍煎紡錛屾湰浜烘洿鍠滄鍚庤咃紝棣栧厛鐪嬩竴涓畝鍗曠殑渚嬪瓙鍚э紝濡備笅錛?br />
log4j.rootLogger=debug, stdout, R
聽聽log4j.appender.stdout=org.apache.log4j.ConsoleAppender
聽聽log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
聽聽# Pattern to output the caller's file name and line number.
聽聽log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
聽聽log4j.appender.R=org.apache.log4j.RollingFileAppender
聽聽log4j.appender.R.File=example.log
聽聽log4j.appender.R.MaxFileSize=100KB
聽聽# Keep one backup file
聽聽log4j.appender.R.MaxBackupIndex=1
聽聽log4j.appender.R.layout=org.apache.log4j.PatternLayout
聽聽log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
聽聽聽聽聽聽聽聽
聽聽棣栧厛錛屾槸璁劇疆root錛屾牸寮忎負 log4j.rootLogger=[level],appenderName, ...錛屽叾涓璴evel灝辨槸璁劇疆闇瑕佽緭鍑轟俊鎭殑綰у埆錛屽悗闈㈡槸appender鐨勮緭鍑虹殑鐩殑鍦幫紝appenderName灝辨槸鎸囧畾鏃ュ織淇℃伅杈撳嚭鍒板摢涓湴鏂廣傛偍鍙互鍚屾椂鎸囧畾澶氫釜杈撳嚭鐩殑鍦般傞厤緗棩蹇椾俊鎭緭鍑虹洰鐨勫湴Appender錛屽叾璇硶涓?br />聽聽log4j.appender.appenderName = fully.qualified.name.of.appender.class
聽聽log4j.appender.appenderName.option1 = value1
聽聽...
聽聽log4j.appender.appenderName.option = valueN
Log4j鎻愪緵鐨刟ppender鏈変互涓嬪嚑縐嶏細
聽聽org.apache.log4j.ConsoleAppender錛堟帶鍒跺彴錛?br />聽聽org.apache.log4j.FileAppender錛堟枃浠訛級
聽聽org.apache.log4j.DailyRollingFileAppender錛堟瘡澶╀駭鐢熶竴涓棩蹇楁枃浠訛級
聽聽org.apache.log4j.RollingFileAppender錛堟枃浠跺ぇ灝忓埌杈炬寚瀹氬昂瀵哥殑鏃跺欎駭鐢熸柊鏂囦歡錛?br />聽聽org.apache.log4j.WriterAppender錛堝皢鏃ュ織淇℃伅浠ユ祦鏍煎紡鍙戦佸埌浠繪剰鎸囧畾鐨勫湴鏂癸級
閰嶇疆鏃ュ織淇℃伅鐨勬牸寮忥紙甯冨眬錛夛紝鍏惰娉曚負錛?br />聽聽log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
聽聽log4j.appender.appenderName.layout.option1 = value1
聽聽....
聽聽log4j.appender.appenderName.layout.option = valueN
Log4j鎻愪緵鐨刲ayout鏈変互涓嬪嚑縐嶏細
聽聽org.apache.log4j.HTMLLayout錛堜互HTML琛ㄦ牸褰㈠紡甯冨眬錛夛紝
聽聽org.apache.log4j.PatternLayout錛堝彲浠ョ伒媧誨湴鎸囧畾甯冨眬妯″紡錛夛紝
聽聽org.apache.log4j.SimpleLayout錛堝寘鍚棩蹇椾俊鎭殑綰у埆鍜屼俊鎭瓧絎︿覆錛夛紝
聽聽org.apache.log4j.TTCCLayout錛堝寘鍚棩蹇椾駭鐢熺殑鏃墮棿銆佺嚎紼嬨佺被鍒瓑絳変俊鎭級
3銆丩og4j鍦ㄧ▼搴忎腑鐨勪嬌鐢?br />聽聽瑕佸湪鑷繁鐨勭被涓嬌鐢↙og4j錛岄鍏堝0鏄庝竴涓潤鎬佸彉閲廘ogger logger=Logger.getLog("classname")錛涘湪浣跨敤涔嬪墠錛岀敤PropertyConfigurator.configure ("閰嶇疆鏂囦歡")閰嶇疆涓涓嬶紝鐜板湪灝卞彲浠ヤ嬌鐢ㄤ簡錛岀敤娉曞涓嬶細logger.debug("debug message")鎴栬卨ogger.info("info message")錛岀湅涓嬮潰涓涓皬渚嬪瓙錛?br />
import com.foo.Bar;
聽聽import org.apache.log4j.Logger;
聽聽import org.apache.log4j.PropertyConfigurator;
聽聽public class MyApp {
聽聽聽聽static Logger logger = Logger.getLogger(MyApp.class.getName());
聽聽聽聽public static void main(String[] args) {
聽聽聽聽聽聽// BasicConfigurator replaced with PropertyConfigurator.
聽聽聽聽聽聽PropertyConfigurator.configure(args[0]);
聽聽聽聽聽聽logger.info("Entering application.");
聽聽聽聽聽聽Bar bar = new Bar();
聽聽聽聽聽聽bar.doIt();
聽聽聽聽聽聽logger.info("Exiting application.");
聽聽聽聽}
聽聽}
[綆浠媇
瀵逛簬涓涓吀鍨嬬殑Web搴旂敤錛屽畬鍠勭殑璁よ瘉鍜屾巿鏉冩満鍒舵槸蹇呬笉鍙皯鐨勶紝鍦⊿pringFramework涓紝Juergen Hoeller鎻愪緵鐨勮寖渚婮PetStore緇欎簡涓浜涜繖鏂歸潰鐨勪粙緇嶏紝浣嗚繕榪滆繙涓嶅錛孉cegi鏄竴涓笓闂ㄤ負SpringFramework鎻愪緵瀹夊叏鏈哄埗鐨?欏圭洰錛屽叏縐頒負Acegi Security System for Spring錛屽綋鍓嶇増鏈負0.5.1錛屽氨鍏剁洰鍓嶆彁渚涚殑鍔熻兘錛屽簲璇ュ彲浠ユ弧瓚崇粷澶у鏁板簲鐢ㄧ殑闇姹傘?br />
鏈枃鐨勪富瑕佺洰鐨勬槸甯屾湜鑳藉璇存槑濡備綍鍦ㄥ熀浜嶴pring鏋勬灦鐨刉eb搴旂敤涓嬌鐢ˋcegi錛岃屼笉鏄緇嗕粙緇嶅叾涓殑姣忎釜鎺ュ彛銆佹瘡涓被銆傛敞鎰忥紝鍗充嬌瀵瑰凡緇忓瓨鍦ㄧ殑Spring搴旂敤錛岄氳繃涓嬮潰浠嬬粛鐨勬楠わ紝涔熷彲浠ラ┈涓婁韓鍙楀埌Acegi鎻愪緵鐨勮璇佸拰鎺堟潈銆?br />
[鍩虹宸ヤ綔]
鍦ㄤ綘鐨刉eb搴旂敤鐨刲ib涓坊鍔燗cegi涓嬭澆鍖呬腑鐨刟cegi-security.jar
[web.xml]
瀹炵幇璁よ瘉鍜屾巿鏉冪殑鏈甯哥敤鐨勬柟娉曟槸閫氳繃filter錛孉cegi浜︽槸濡傛錛岄氬父Acegi闇瑕佸湪web.xml娣誨姞浠ヤ笅5涓猣ilter:
<filter>
聽聽<filter-name>Acegi Channel Processing Filter</filter-name>
聽聽<filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>
聽聽<init-param>
聽聽聽聽<param-name>targetClass</param-name>
聽聽聽聽<param-value>net.sf.acegisecurity.securechannel.ChannelProcessingFilter</param-value>
聽聽</init-param>
</filter>
<filter>
聽聽<filter-name>Acegi Authentication Processing Filter</filter-name>
聽聽<filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>
聽聽<init-param>
聽聽聽聽<param-name>targetClass</param-name>
聽聽聽聽<param-value>net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter</param-value>
聽聽</init-param>
</filter>
<filter>
聽聽<filter-name>Acegi HTTP BASIC Authorization Filter</filter-name>
聽聽<filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>
聽聽<init-param>
聽聽聽聽<param-name>targetClass</param-name>
聽聽聽聽<param-value>net.sf.acegisecurity.ui.basicauth.BasicProcessingFilter</param-value>
聽聽</init-param>
</filter>
<filter>
聽聽<filter-name>Acegi Security System for Spring Auto Integration Filter</filter-name>
聽聽<filter-class>net.sf.acegisecurity.ui.AutoIntegrationFilter</filter-class>
</filter>
<filter>
聽聽<filter-name>Acegi HTTP Request Security Filter</filter-name>
聽聽<filter-class>net.sf.acegisecurity.util.FilterToBeanProxy</filter-class>
聽聽<init-param>
聽聽聽聽<param-name>targetClass</param-name>
聽聽聽聽<param-value>net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter</param-value>
聽聽</init-param>
</filter>
鏈鍏堝紩璧瘋糠鎯戠殑鏄痭et.sf.acegisecurity.util.FilterToBeanProxy錛孉cegi鑷繁鐨勬枃妗d笂瑙i噴鏄細 鈥淲hat聽聽FilterToBeanProxy does is delegate the Filter's methods through to a bean which is obtained from the
Spring application context. This enables the bean to benefit from the Spring application context lifecycle support and configuration flexibility.鈥濓紝濡傚笇鏈涙繁絀剁殑璇濓紝鍘葷湅鐪嬫簮浠g爜搴旇涓嶉毦鐞嗚В銆?br />
鍐嶄笅鏉ュ氨鏄坊鍔爁ilter-mapping浜嗭細
<filter-mapping>
聽聽<filter-name>Acegi Channel Processing Filter</filter-name>
聽聽<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
聽聽<filter-name>Acegi Authentication Processing Filter</filter-name>
聽聽<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
聽聽<filter-name>Acegi HTTP BASIC Authorization Filter</filter-name>
聽聽<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
聽聽<filter-name>Acegi Security System for Spring Auto Integration Filter</filter-name>
聽聽<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
聽聽<filter-name>Acegi HTTP Request Security Filter</filter-name>
聽聽<url-pattern>/*</url-pattern>
</filter-mapping>
榪欓噷錛岄渶瑕佹敞鎰忎互涓嬩袱鐐癸細
1) 榪欏嚑涓猣ilter鐨勯『搴忔槸涓嶈兘鏇存敼鐨勶紝欏哄簭涓嶅灝嗘棤娉曟甯稿伐浣滐紱
2) 濡傛灉浣犵殑搴旂敤涓嶉渶瑕佸畨鍏ㄤ紶杈擄紝濡俬ttps錛屽垯灝?Acegi Channel Processing Filter"鐩稿叧鍐呭娉ㄩ噴鎺夊嵆鍙紱
3) 濡傛灉浣犵殑搴旂敤涓嶉渶瑕丼pring鎻愪緵鐨勮繙紼嬭闂満鍒訛紝濡侶essian and Burlap錛屽皢"Acegi HTTP BASIC Authorization
Filter"鐩稿叧鍐呭娉ㄩ噴鎺夊嵆鍙?br />
[applicationContext.xml]
鎺ヤ笅鏉ュ氨鏄娣誨姞applicationContext.xml涓殑鍐呭浜嗭紝浠庡垰鎵岶ilterToBeanFactory鐨勮В閲婂彲浠ョ湅鍑猴紝鐪熸鐨刦ilter閮?br />鍦⊿pring鐨刟pplicationContext涓鐞嗭細
1) 棣栧厛錛屼綘鐨勬暟鎹簱涓繀欏誨叿鏈変繚瀛樼敤鎴峰悕鍜屽瘑鐮佺殑table錛孉cegi瑕佹眰table鐨剆chema蹇呴』濡備笅錛?br />
CREATE TABLE users (
聽聽聽聽username VARCHAR(50) NOT NULL PRIMARY KEY,
聽聽聽聽password VARCHAR(50) NOT NULL,
聽聽聽聽enabled BIT NOT NULL
);
CREATE TABLE authorities (
聽聽聽聽username VARCHAR(50) NOT NULL,
聽聽聽聽authority VARCHAR(50) NOT NULL
);
CREATE UNIQUE INDEX ix_auth_username ON authorities ( username, authority );
ALTER TABLE authorities ADD CONSTRAINT fk_authorities_users foreign key (username) REFERENCES users
(username);
2) 娣誨姞璁塊棶浣犵殑鏁版嵁搴撶殑datasource鍜孉cegi鐨刯dbcDao錛屽涓嬶細
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
聽聽<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
聽聽<property name="url"><value>${jdbc.url}</value></property>
聽聽<property name="username"><value>${jdbc.username}</value></property>
聽聽<property name="password"><value>${jdbc.password}</value></property>
</bean>
<bean id="jdbcDaoImpl" class="net.sf.acegisecurity.providers.dao.jdbc.JdbcDaoImpl">
聽聽<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
3) 娣誨姞DaoAuthenticationProvider:
<bean id="daoAuthenticationProvider" class="net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider">
聽聽<property name="authenticationDao"><ref bean="authenticationDao"/></property>
聽聽<property name="userCache"><ref bean="userCache"/></property>
</bean>
<bean id="userCache" class="net.sf.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
聽聽<property name="minutesToIdle"><value>5</value></property>
</bean>
濡傛灉浣犻渶瑕佸瀵嗙爜鍔犲瘑錛屽垯鍦╠aoAuthenticationProvider涓姞鍏ワ細<property name="passwordEncoder"><ref
bean="passwordEncoder"/></property>錛孉cegi鎻愪緵浜嗗嚑縐嶅姞瀵嗘柟娉曪紝璇︾粏鎯呭喌鍙湅鍖?br />net.sf.acegisecurity.providers.encoding
4) 娣誨姞authenticationManager:
<bean id="authenticationManager" class="net.sf.acegisecurity.providers.ProviderManager">
聽聽<property name="providers">
聽聽聽聽<list>
聽聽聽聽聽聽<ref bean="daoAuthenticationProvider"/>
聽聽聽聽</list>
聽聽 </property>
</bean>
5) 娣誨姞accessDecisionManager:
<bean id="accessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased">
聽聽<property name="allowIfAllAbstainDecisions">
聽聽聽聽<value>false</value>
聽聽</property>
聽聽<property name="decisionVoters">
聽聽聽聽<list><ref bean="roleVoter"/></list>
聽聽</property>
</bean>
<bean id="roleVoter" class="net.sf.acegisecurity.vote.RoleVoter"/>
6) 娣誨姞authenticationProcessingFilterEntryPoint:
<bean id="authenticationProcessingFilterEntryPoint"
class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
聽聽<property name="loginFormUrl"><value>/acegilogin.jsp</value></property>
聽聽<property name="forceHttps"><value>false</value></property>
</bean>
鍏朵腑acegilogin.jsp鏄櫥闄嗛〉闈紝涓涓渶綆鍗曠殑鐧誨綍欏甸潰濡備笅錛?br />
<%@ taglib prefix='c' uri='http://java.sun.com/jstl/core' %>
<%@ page import="net.sf.acegisecurity.ui.AbstractProcessingFilter" %>
<%@ page import="net.sf.acegisecurity.AuthenticationException" %>
<html>
聽聽<head>
聽聽聽聽<title>Login</title>
聽聽</head>
聽聽<body>
聽聽聽聽<h1>Login</h1>
聽聽聽聽<form action="<c:url value='j_acegi_security_check'/>" method="POST">
聽聽聽聽聽聽<table>
聽聽聽聽聽聽聽聽<tr><td>User:</td><td><input type='text' name='j_username'></td></tr>
聽聽聽聽聽聽聽聽<tr><td>Password:</td><td><input type='password' name='j_password'></td></tr>
聽聽聽聽聽聽聽聽<tr><td colspan='2'><input name="submit" type="submit"></td></tr>
聽聽聽聽聽聽聽聽<tr><td colspan='2'><input name="reset" type="reset"></td></tr>
聽聽聽聽聽聽</table>
聽聽聽聽</form>
聽聽</body>
</html>
7) 娣誨姞filterInvocationInterceptor:
<bean id="filterInvocationInterceptor"
class="net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor">
聽聽<property name="authenticationManager">
聽聽聽聽<ref bean="authenticationManager"/>
聽聽</property>
聽聽<property name="accessDecisionManager">
聽聽聽聽<ref bean="accessDecisionManager"/>
聽聽</property>
聽聽<property name="objectDefinitionSource">
聽聽聽聽<value>
聽聽聽聽聽聽CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
聽聽聽聽聽聽\A/sec/administrator.*\Z=ROLE_SUPERVISOR
聽聽聽聽聽聽\A/sec/user.*\Z=ROLE_TELLER
聽聽聽聽</value>
聽聽</property>
</bean>
榪欓噷璇鋒敞鎰忥紝瑕乷bjectDefinitionSource涓畾涔夊摢浜涢〉闈㈤渶瑕佹潈闄愯闂紝闇瑕佹牴鎹嚜宸辯殑搴旂敤闇姹傝繘琛屼慨鏀癸紝鎴戜笂闈㈢粰鍑?br />鐨勫畾涔夌殑鎰忔濇槸榪欐牱鐨勶細
a. CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON鎰忔濇槸鍦ㄦ瘮杈冭姹傝礬寰勬椂鍏ㄩ儴杞崲涓哄皬鍐?br />b. \A/sec/administrator.*\Z=ROLE_SUPERVISOR鎰忔濇槸鍙湁鏉冮檺涓篟OLE_SUPERVISOR鎵嶈兘璁塊棶/sec/administrator*鐨勯〉闈?br />c. \A/sec/user.*\Z=ROLE_TELLER鎰忔濇槸鍙湁鏉冮檺涓篟OLE_TELLER鐨勭敤鎴鋒墠鑳借闂?sec/user*鐨勯〉闈?br />
8) 娣誨姞securityEnforcementFilter:
<bean id="securityEnforcementFilter" class="net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter">
聽聽<property name="filterSecurityInterceptor">
聽聽聽聽<ref bean="filterInvocationInterceptor"/>
聽聽</property>
聽聽<property name="authenticationEntryPoint">
聽聽聽聽<ref bean="authenticationProcessingFilterEntryPoint"/>
聽聽</property>
</bean>
9) 娣誨姞authenticationProcessingFilter:
<bean id="authenticationProcessingFilter"
class="net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
聽聽<property name="authenticationManager">
聽聽聽聽<ref bean="authenticationManager"/>
聽聽</property>
聽聽<property name="authenticationFailureUrl">
聽聽聽聽<value>/loginerror.jsp</value>
聽聽</property>
聽聽<property name="defaultTargetUrl">
聽聽聽聽<value>/</value>
聽聽</property>
聽聽<property name="filterProcessesUrl">
聽聽聽聽<value>/j_acegi_security_check</value>
聽聽</property>
</bean>
鍏朵腑authenticationFailureUrl鏄璇佸け璐ョ殑欏甸潰銆?br />
10) 濡傛灉闇瑕佷竴浜涢〉闈㈤氳繃瀹夊叏閫氶亾鐨勮瘽錛屾坊鍔犱笅闈㈢殑閰嶇疆:
<bean id="channelProcessingFilter" class="net.sf.acegisecurity.securechannel.ChannelProcessingFilter">
聽聽<property name="channelDecisionManager">
聽聽聽聽<ref bean="channelDecisionManager"/>
聽聽</property>
聽聽<property name="filterInvocationDefinitionSource">
聽聽聽聽<value>
聽聽聽聽聽聽CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
聽聽聽聽聽聽\A/sec/administrator.*\Z=REQUIRES_SECURE_CHANNEL
聽聽聽聽聽聽\A/acegilogin.jsp.*\Z=REQUIRES_SECURE_CHANNEL
聽聽聽聽聽聽\A/j_acegi_security_check.*\Z=REQUIRES_SECURE_CHANNEL
聽聽聽聽聽聽\A.*\Z=REQUIRES_INSECURE_CHANNEL
聽聽聽聽</value>
聽聽</property>
</bean>
<bean id="channelDecisionManager" class="net.sf.acegisecurity.securechannel.ChannelDecisionManagerImpl">
聽聽<property name="channelProcessors">
聽聽聽聽<list>
聽聽聽聽聽聽<ref bean="secureChannelProcessor"/>
聽聽聽聽聽聽<ref bean="insecureChannelProcessor"/>
聽聽聽聽</list>
聽聽</property>
</bean>
<bean id="secureChannelProcessor" class="net.sf.acegisecurity.securechannel.SecureChannelProcessor"/>
<bean id="insecureChannelProcessor" class="net.sf.acegisecurity.securechannel.InsecureChannelProcessor"/>
[緙哄皯浜嗕粈涔堬紵]
Acegi鐩墠鎻愪緵浜嗕袱縐?secure object"錛屽垎鍒欏甸潰鍜屾柟娉曡繘琛屽畨鍏ㄨ璇佺鐞嗭紝鎴戣繖閲屼粙緇嶇殑鍙槸鍒╃敤
FilterSecurityInterceptor瀵硅闂〉闈㈢殑鏉冮檺鎺у埗錛岄櫎姝や箣澶栵紝Acegi榪樻彁渚涗簡鍙﹀涓涓狪nterceptor鈥曗?br />MethodSecurityInterceptor錛屽畠緇撳悎runAsManager鍙疄鐜板瀵硅薄涓殑鏂規硶鐨勬潈闄愭帶鍒訛紝浣跨敤鏂規硶鍙弬鐪婣cegi鑷甫鐨勬枃妗?br />鍜宑ontact鑼冧緥銆?br />
[鏈鍚庤璇寸殑]
鏈潵浠ヤ負鍙槸璇存槑濡備綍浣跨敤Acegi鑰屽凡錛屽簲璇ラ潪甯哥畝鍗曪紝浣嗙湡姝e啓璧鋒潵鎵嶅彂鐜版兂瑕佹潯鐞嗘竻妤氱殑鐞嗛『鎵鏈夐渶瑕佺殑bean榪樻槸寰?br />鍥伴毦鐨勶紝浣嗘効鎴戞病鏈夐仐婕忓お澶氫笢瑗匡紝濡傛灉鎴戠殑鏂囩珷鏈変粈涔堥仐婕忔垨閿欒鐨勮瘽錛岃繕璇峰弬鐪婣cegi鑷甫鐨剄uick-start鑼冧緥錛屼絾璇?br />娉ㄦ剰錛岃繖涓寖渚嬫槸涓嶈兘鐩存帴鎷挎潵鐢ㄧ殑銆?br />鍒嗘瀽鍜屽涔燬pring涓殑jpetstore鐢ㄦ埛綆$悊
聽聽瀛樺湪鐢ㄦ埛鐨勭郴緇燂紝蹇呯劧闇瑕佺敤鎴風殑鐧誨綍鍜岃璇侊紝浠婂ぉ灝遍氳繃鍒嗘瀽Spring涓嚜甯︾殑jpetstore鐨勪緥瀛愭潵瀛︿範涓涓嬪浣曞疄鐜板湪Spring鏋勬灦鐨勭郴緇熶腑鐢ㄦ埛鐧誨綍銆?br />1銆侀鍏堜粠娉ㄥ唽鐢ㄦ埛寮濮嬶紝鍏堢湅鐪媕petstore-servlet.xml涓叧浜庢敞鍐岀敤鎴風殑bean瀹氫箟錛屼粠瀹氫箟鍛藉悕涓氨鍙互鐪嬪嚭涓嬮潰榪欐灝辨槸娉ㄥ唽鐢ㄦ埛鐨勶細
聽聽<bean name="/shop/newAccount.do" class="org.springframework.samples.jpetstore.web.spring.AccountFormController">
聽聽聽聽<property name="petStore"><ref bean="petStore"/></property>
聽聽聽聽<property name="validator"><ref bean="accountValidator"/></property>
聽聽聽聽<property name="successView"><value>index</value></property>
聽聽</bean>
1). formView鍛紵浠嶢ccountFormController鐨勬瀯閫犲嚱鏁頒腑寰楀埌錛屽師鏉ヤ負EditAccountForm錛浡犅?br />2). EditoAccountForm.jsp涓樉寰楅潪甯鎬貢錛屽叾瀹炴病鏈夊灝戦毦鐞嗚В鐨勫湴鏂癸紝鏈涓昏鐨勬槸榪欎釜form鏃㈡槸娣誨姞鏂扮敤鎴風殑錛屽張鏄紪杈戠敤鎴蜂俊鎭殑錛屾墍浠ユ樉寰楁湁鐐逛貢緋熺碂鐨勩?br />2銆佹坊鍔犲ソ浜嗘柊鐢ㄦ埛錛屾帴涓嬫潵鐪嬬湅濡備綍鐧誨綍錛屽湪jpetstore-servlet涓彂鐜拌繖涓や釜鐩稿叧bean瀹氫箟錛屽涓嬶細
聽聽<bean name="/shop/signon.do" class="org.springframework.samples.jpetstore.web.spring.SignonController">
聽聽聽聽<property name="petStore"><ref bean="petStore"/></property>
聽聽</bean>
聽聽<bean name="/shop/signonForm.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
聽聽聽聽<property name="viewName"><value>SignonForm</value></property>
聽聽</bean>
1). 絎簩涓猙ean鏄湪榪愯鏃剁敤鎴瘋緭鍏ョ敤鎴峰悕鍜屽瘑鐮佺殑form錛屽彨鍋歋ignonForm錛屽浜庤繖涓?ParameterizableViewController錛岀敤鏂囨。閲岀殑璇濊榪欐槸鏈綆鍗曠殑Controller錛屽叾浣滅敤灝辨槸鍦ㄨ繍琛屼腑鎸囧悜 Controller鑰屼笉鏄洿鎺ユ寚鍚慾sp鏂囦歡錛屼粎姝よ屽凡銆?br />2). SignonForm.jsp錛岄噷闈㈠氨鏄竴涓畝鍗曠殑form錛屽叾action灝辨槸絎竴涓猙ean錛屽嵆/shop/signon.do錛屾渶闇瑕佹敞鎰忕殑鏄?signonForwardAction錛屽叾涓昏浣滅敤鏄痜orward鍒伴渶瑕佽緭鍏ョ敤鎴峰悕鍜屽瘑鐮佺殑閭d釜欏甸潰涓婂幓錛岃繖涓彉閲忓摢閲屾潵鐨勫憿錛熺湅鐪嬩笅闈細
聽聽<bean id="secureHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
聽聽聽聽<property name="interceptors">
聽聽聽聽聽聽<list>
聽聽聽聽聽聽聽聽<ref bean="signonInterceptor"/>
聽聽聽聽聽聽</list>
聽聽聽聽</property>
聽聽聽聽<property name="urlMap">
聽聽聽聽聽聽<map>
聽聽聽聽聽聽聽聽<entry key="/shop/editAccount.do"><ref local="secure_editAccount"/></entry>
聽聽聽聽聽聽聽聽<entry key="/shop/listOrders.do"><ref local="secure_listOrders"/></entry>
聽聽聽聽聽聽聽聽<entry key="/shop/newOrder.do"><ref local="secure_newOrder"/></entry>
聽聽聽聽聽聽聽聽<entry key="/shop/viewOrder.do"><ref local="secure_viewOrder"/></entry>
聽聽聽聽聽聽</map>
聽聽聽聽</property>
聽聽</bean>
聽聽鍘熸潵錛屼笂闈㈢殑signonInterceptor瀹炵幇浜唒reHandle錛屽洜姝ゅ湪璇鋒眰涓婇潰鐨刴ap欏甸潰鏃訛紝棣栧厛瑕佺粡榪囪繖涓狪nterceptor錛岀湅鐪?SignonInterceptor鐨勬簮鐮侊紝鍘熸潵鍦ㄥ叾涓負signon.jsp璧嬩簣涓涓猻ignonForwardAction瀵硅薄錛屽懙鍛碉紝鎬葷畻鏄庣櫧浜嗐?br />3). 鎺ヤ笅鏉ュ幓瀛︿範涓涓婼ignonController錛屽叾涓諱綋閮ㄥ垎涓彲浠ョ湅鍑猴紝棣栧厛鍙栧嚭鐢ㄦ埛杈撳叆鐨剈sername鍜宲assword錛岀劧鍚庡埌鏁版嵁搴撲腑楠岃瘉 鏈夋病鏈夎繖涓敤鎴鳳紝濡傛灉娌℃湁榪欎釜鐢ㄦ埛錛岃繑鍥炲悇閿欒欏甸潰錛涘鏋滄垚鍔燂紝棣栧厛鐢熸垚涓涓猆serSession瀵硅薄錛屽湪request鐨剆ession鍔犲叆榪欎釜 userSession錛屾敞鎰忚繖閮ㄥ垎浠g爜涓粰鍑轟簡PagedListHolder鍒嗛〉鐨勭畝鍗曚嬌鐢ㄦ柟娉曪紝鍏充簬鍒嗛〉鏄劇ず錛屼互鍚庡啀瀛︿範鍚с?br />3銆佺櫥褰曟垚鍔熷悗錛屽氨鍙互鏍規嵁涓嶅悓鐨勭敤鎴瘋鏂戒笉鍚岀殑琛屼負浜嗭紝鍙栧緱鐢ㄦ埛淇℃伅錛屾棤闈炲氨鏄粠session鍙栧嚭userSession鍗沖彲銆?br />