摘要:
閱讀全文
posted @
2007-03-11 18:58 西紅柿(tomato) 閱讀(1051) |
評(píng)論 (0) |
編輯 收藏
?Acegi安全系統(tǒng)的配置
????????
Acegi 的配置看起來非常復(fù)雜,但事實(shí)上在實(shí)際項(xiàng)目的安全應(yīng)用中我們并不需要那么多功能,清楚的了解Acegi配置中各項(xiàng)的功能,有助于我們靈活的運(yùn)用Acegi于實(shí)踐中。
2.1 在Web.xml中的配置
1)? FilterToBeanProxy
Acegi通過實(shí)現(xiàn)了Filter接口的FilterToBeanProxy提供一種特殊的使用Servlet Filter的方式,它委托Spring中的Bean -- FilterChainProxy來完成過濾功能,這好處是簡化了web.xml的配置,并且充分利用了Spring IOC的優(yōu)勢。FilterChainProxy包含了處理認(rèn)證過程的filter列表,每個(gè)filter都有各自的功能。
? ? <filter>
??????? <filter-name>Acegi Filter Chain Proxy</filter-name>
??????? <filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
??????? <init-param>
??????????? <param-name>targetClass</param-name>
??????????? <param-value>org.acegisecurity.util.FilterChainProxy</param-value>
??????? </init-param>
??? </filter>
2) filter-mapping
<filter-mapping>限定了FilterToBeanProxy的URL匹配模式,只有*.do和*.jsp和/j_acegi_security_check 的請(qǐng)求才會(huì)受到權(quán)限控制,對(duì)javascript,css等不限制。
?? <filter-mapping>
????? <filter-name>Acegi Filter Chain Proxy</filter-name>
????? <url-pattern>*.do</url-pattern>
??? </filter-mapping>
???
??? <filter-mapping>
????? <filter-name>Acegi Filter Chain Proxy</filter-name>
????? <url-pattern>*.jsp</url-pattern>
??? </filter-mapping>
???
??? <filter-mapping>
????? <filter-name>Acegi Filter Chain Proxy</filter-name>
????? <url-pattern>/j_acegi_security_check</url-pattern>
</filter-mapping>
3) HttpSessionEventPublisher
<listener>的HttpSessionEventPublisher用于發(fā)布HttpSessionApplicationEvents和HttpSessionDestroyedEvent事件給spring的applicationcontext。
??? <listener>
??????? <listener-class>org.acegisecurity.ui.session.HttpSessionEventPublisher</listener-class>
??? </listener>
2.2 在applicationContext-acegi-security.xml中
2.2.1 FILTER CHAIN
FilterChainProxy會(huì)按順序來調(diào)用這些filter,使這些filter能享用Spring ioc的功能, CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON定義了url比較前先轉(zhuǎn)為小寫, PATTERN_TYPE_APACHE_ANT定義了使用Apache ant的匹配模式
<bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
??????? <property name="filterInvocationDefinitionSource">
??????????? <value>
??????????????? CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
??????????????? PATTERN_TYPE_APACHE_ANT
?????????????? /**=httpSessionContextIntegrationFilter,authenticationProcessingFilter,
basicProcessingFilter,rememberMeProcessingFilter,anonymousProcessingFilter,
exceptionTranslationFilter,filterInvocationInterceptor
??????????? </value>
??????? </property>
??? </bean>
2.2.2 基礎(chǔ)認(rèn)證
1) authenticationManager
起到認(rèn)證管理的作用,它將驗(yàn)證的功能委托給多個(gè)Provider,并通過遍歷Providers, 以保證獲取不同來源的身份認(rèn)證,若某個(gè)Provider能成功確認(rèn)當(dāng)前用戶的身份,authenticate()方法會(huì)返回一個(gè)完整的包含用戶授權(quán)信息的Authentication對(duì)象,否則會(huì)拋出一個(gè)AuthenticationException。
Acegi提供了不同的AuthenticationProvider的實(shí)現(xiàn),如:
??????? DaoAuthenticationProvider 從數(shù)據(jù)庫中讀取用戶信息驗(yàn)證身份
??????? AnonymousAuthenticationProvider 匿名用戶身份認(rèn)證
??????? RememberMeAuthenticationProvider 已存cookie中的用戶信息身份認(rèn)證
??????? AuthByAdapterProvider 使用容器的適配器驗(yàn)證身份
??????? CasAuthenticationProvider 根據(jù)Yale中心認(rèn)證服務(wù)驗(yàn)證身份, 用于實(shí)現(xiàn)單點(diǎn)登陸
??????? JaasAuthenticationProvider 從JASS登陸配置中獲取用戶信息驗(yàn)證身份
??????? RemoteAuthenticationProvider 根據(jù)遠(yuǎn)程服務(wù)驗(yàn)證用戶身份
??????? RunAsImplAuthenticationProvider 對(duì)身份已被管理器替換的用戶進(jìn)行驗(yàn)證
??????? X509AuthenticationProvider 從X509認(rèn)證中獲取用戶信息驗(yàn)證身份
??????? TestingAuthenticationProvider 單元測試時(shí)使用
??????? 每個(gè)認(rèn)證者會(huì)對(duì)自己指定的證明信息進(jìn)行認(rèn)證,如DaoAuthenticationProvider僅對(duì)UsernamePasswordAuthenticationToken這個(gè)證明信息進(jìn)行認(rèn)證。
<bean id="authenticationManager" class="org.acegisecurity.providers.ProviderManager">
??????? <property name="providers">
??????????? <list>
??????????????? <ref local="daoAuthenticationProvider"/>
??????????????? <ref local="anonymousAuthenticationProvider"/>
??????????????? <ref local="rememberMeAuthenticationProvider"/>
??????????? </list>
??????? </property>
</bean>
2) daoAuthenticationProvider
進(jìn)行簡單的基于數(shù)據(jù)庫的身份驗(yàn)證。DaoAuthenticationProvider獲取數(shù)據(jù)庫中的賬號(hào)密碼并進(jìn)行匹配,若成功則在通過用戶身份的同時(shí)返回一個(gè)包含授權(quán)信息的Authentication對(duì)象,否則身份驗(yàn)證失敗,拋出一個(gè)AuthenticatiionException。
??? <bean id="daoAuthenticationProvider" class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
??????? <property name="userDetailsService" ref="jdbcDaoImpl"/>
??????? <property name="userCache" ref="userCache"/>
??????? <property name="passwordEncoder" ref="passwordEncoder"/>
?? </bean>
3) passwordEncoder
使用加密器對(duì)用戶輸入的明文進(jìn)行加密。Acegi提供了三種加密器:
PlaintextPasswordEncoder—默認(rèn),不加密,返回明文.
ShaPasswordEncoder—哈希算法(SHA)加密
Md5PasswordEncoder—消息摘要(MD5)加密
<bean id="passwordEncoder" class="org.acegisecurity.providers.encoding.Md5PasswordEncoder"/>
4) jdbcDaoImpl
用于在數(shù)據(jù)中獲取用戶信息。 acegi提供了用戶及授權(quán)的表結(jié)構(gòu),但是您也可以自己來實(shí)現(xiàn)。通過usersByUsernameQuery這個(gè)SQL得到你的(用戶ID,密碼,狀態(tài)信息);通過authoritiesByUsernameQuery這個(gè)SQL得到你的(用戶ID,授權(quán)信息)
<bean id="jdbcDaoImpl" class="org.acegisecurity.userdetails.jdbc.JdbcDaoImpl">
??????? <property name="dataSource" ref="dataSource"/>
??????? <property name="usersByUsernameQuery">
??????????? <value>select loginid,passwd,1 from users where loginid = ?</value>
??????? </property>
??????? <property name="authoritiesByUsernameQuery">
??????????? <value>select u.loginid,p.name from users u,roles r,permissions p,user_role ur,role_permis rp where u.id=ur.user_id and r.id=ur.role_id and p.id=rp.permis_id and
??????????????? r.id=rp.role_id and p.status='1' and u.loginid=?</value>
??????? </property>
</bean>
5) userCache &? resourceCache
緩存用戶和資源相對(duì)應(yīng)的權(quán)限信息。每當(dāng)請(qǐng)求一個(gè)受保護(hù)資源時(shí),daoAuthenticationProvider就會(huì)被調(diào)用以獲取用戶授權(quán)信息。如果每次都從數(shù)據(jù)庫獲取的話,那代價(jià)很高,對(duì)于不常改變的用戶和資源信息來說,最好是把相關(guān)授權(quán)信息緩存起來。(詳見 2.6.3 資源權(quán)限定義擴(kuò)展 )
userCache提供了兩種實(shí)現(xiàn): NullUserCache和EhCacheBasedUserCache, NullUserCache實(shí)際上就是不進(jìn)行任何緩存,EhCacheBasedUserCache是使用Ehcache來實(shí)現(xiàn)緩功能。
??? <bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
??????? <property name="cacheManager" ref="cacheManager"/>
??????? <property name="cacheName" value="userCache"/>
??? </bean>
??? <bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache" autowire="byName">
??????? <property name="cache" ref="userCacheBackend"/>
??? </bean>
??? <bean id="resourceCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
??????? <property name="cacheManager" ref="cacheManager"/>
??????? <property name="cacheName" value="resourceCache"/>
??? </bean>
??? <bean id="resourceCache" class="org.springside.modules.security.service.acegi.cache.ResourceCache" autowire="byName">
??????? <property name="cache" ref="resourceCacheBackend"/>
??? </bean>
6) basicProcessingFilter
用于處理HTTP頭的認(rèn)證信息,如從Spring遠(yuǎn)程協(xié)議(如Hessian和Burlap)或普通的瀏覽器如IE,Navigator的HTTP頭中獲取用戶信息,將他們轉(zhuǎn)交給通過authenticationManager屬性裝配的認(rèn)證管理器。如果認(rèn)證成功,會(huì)將一個(gè)Authentication對(duì)象放到會(huì)話中,否則,如果認(rèn)證失敗,會(huì)將控制轉(zhuǎn)交給認(rèn)證入口點(diǎn)(通過authenticationEntryPoint屬性裝配)
??? <bean id="basicProcessingFilter" class="org.acegisecurity.ui.basicauth.BasicProcessingFilter">
??????? <property name="authenticationManager" ref="authenticationManager"/>
??????? <property name="authenticationEntryPoint" ref="basicProcessingFilterEntryPoint"/>
??? </bean>
7) basicProcessingFilterEntryPoint
通過向?yàn)g覽器發(fā)送一個(gè)HTTP401(未授權(quán))消息,提示用戶登錄。
處理基于HTTP的授權(quán)過程, 在當(dāng)驗(yàn)證過程出現(xiàn)異常后的"去向",通常實(shí)現(xiàn)轉(zhuǎn)向、在response里加入error信息等功能。
<bean id="basicProcessingFilterEntryPoint" class="org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint">
??????? <property name="realmName" value="SpringSide Realm"/>
</bean>
8) authenticationProcessingFilterEntryPoint
當(dāng)拋出AccessDeniedException時(shí),將用戶重定向到登錄界面。屬性loginFormUrl配置了一個(gè)登錄表單的URL,當(dāng)需要用戶登錄時(shí),authenticationProcessingFilterEntryPoint會(huì)將用戶重定向到該URL
<bean id="authenticationProcessingFilterEntryPoint" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
??????? <property name="loginFormUrl">
??????????? <value>/security/login.jsp</value>
??????? </property>
??????? <property name="forceHttps" value="false"/>
</bean>
2.2.3 HTTP安全請(qǐng)求
1) httpSessionContextIntegrationFilter
每次request前 HttpSessionContextIntegrationFilter從Session中獲取Authentication對(duì)象,在request完后, 又把Authentication對(duì)象保存到Session中供下次request使用,此filter必須其他Acegi filter前使用,使之能跨越多個(gè)請(qǐng)求。
<bean id="httpSessionContextIntegrationFilter" class="org.acegisecurity.context.HttpSessionContextIntegrationFilter"></bean>
??? <bean id="httpRequestAccessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
??????? <property name="allowIfAllAbstainDecisions" value="false"/>
??????? <property name="decisionVoters">
??????????? <list>
??????????????? <ref bean="roleVoter"/>
??????????? </list>
??????? </property>
</bean>
2) httpRequestAccessDecisionManager
經(jīng)過投票機(jī)制來決定是否可以訪問某一資源(URL或方法)。allowIfAllAbstainDecisions為false時(shí)如果有一個(gè)或以上的decisionVoters投票通過,則授權(quán)通過。可選的決策機(jī)制有ConsensusBased和UnanimousBased
??? <bean id="httpRequestAccessDecisionManager" class="org.acegisecurity.vote.AffirmativeBased">
??????? <property name="allowIfAllAbstainDecisions" value="false"/>
??????? <property name="decisionVoters">
??????????? <list>
??????????????? <ref bean="roleVoter"/>
??????????? </list>
??????? </property>
??? </bean>
3) roleVoter
? 必須是以rolePrefix設(shè)定的value開頭的權(quán)限才能進(jìn)行投票,如AUTH_ , ROLE_
??? <bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter">
??????? <property name="rolePrefix" value="AUTH_"/>
?? </bean>
4)exceptionTranslationFilter
異常轉(zhuǎn)換過濾器,主要是處理AccessDeniedException和AuthenticationException,將給每個(gè)異常找到合適的"去向"?
?? <bean id="exceptionTranslationFilter" class="org.acegisecurity.ui.ExceptionTranslationFilter">
??????? <property name="authenticationEntryPoint" ref="authenticationProcessingFilterEntryPoint"/>
??? </bean>
5) authenticationProcessingFilter
和servlet spec差不多,處理登陸請(qǐng)求.當(dāng)身份驗(yàn)證成功時(shí),AuthenticationProcessingFilter會(huì)在會(huì)話中放置一個(gè)Authentication對(duì)象,并且重定向到登錄成功頁面
???????? authenticationFailureUrl定義登陸失敗時(shí)轉(zhuǎn)向的頁面
???????? defaultTargetUrl定義登陸成功時(shí)轉(zhuǎn)向的頁面
???????? filterProcessesUrl定義登陸請(qǐng)求的頁面
???????? rememberMeServices用于在驗(yàn)證成功后添加cookie信息
??? <bean id="authenticationProcessingFilter" class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
??????? <property name="authenticationManager" ref="authenticationManager"/>
??????? <property name="authenticationFailureUrl">
??????????? <value>/security/login.jsp?login_error=1</value>
??????? </property>
??????? <property name="defaultTargetUrl">
??????????? <value>/admin/index.jsp</value>
??????? </property>
??????? <property name="filterProcessesUrl">
??????????? <value>/j_acegi_security_check</value>
??????? </property>
??????? <property name="rememberMeServices" ref="rememberMeServices"/>
??? </bean>
6) filterInvocationInterceptor
在執(zhí)行轉(zhuǎn)向url前檢查objectDefinitionSource中設(shè)定的用戶權(quán)限信息。首先,objectDefinitionSource中定義了訪問URL需要的屬性信息(這里的屬性信息僅僅是標(biāo)志,告訴accessDecisionManager要用哪些voter來投票)。然后,authenticationManager掉用自己的provider來對(duì)用戶的認(rèn)證信息進(jìn)行校驗(yàn)。最后,有投票者根據(jù)用戶持有認(rèn)證和訪問url需要的屬性,調(diào)用自己的voter來投票,決定是否允許訪問。
??? <bean id="filterInvocationInterceptor" class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
??????? <property name="authenticationManager" ref="authenticationManager"/>
??????? <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
??????? <property name="objectDefinitionSource" ref="filterDefinitionSource"/>
??? </bean>
7) filterDefinitionSource (詳見 2.6.3 資源權(quán)限定義擴(kuò)展)
自定義DBFilterInvocationDefinitionSource從數(shù)據(jù)庫和cache中讀取保護(hù)資源及其需要的訪問權(quán)限信息?
<bean id="filterDefinitionSource" class="org.springside.modules.security.service.acegi.DBFilterInvocationDefinitionSource">
??????? <property name="convertUrlToLowercaseBeforeComparison" value="true"/>
??????? <property name="useAntPath" value="true"/>
??????? <property name="acegiCacheManager" ref="acegiCacheManager"/>
</bean>
2.2.4 方法調(diào)用安全控制
(詳見 2.6.3 資源權(quán)限定義擴(kuò)展)
1) methodSecurityInterceptor
在執(zhí)行方法前進(jìn)行攔截,檢查用戶權(quán)限信息
2) methodDefinitionSource
自定義MethodDefinitionSource從cache中讀取權(quán)限
?? <bean id="methodSecurityInterceptor" class="org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor">
??????? <property name="authenticationManager" ref="authenticationManager"/>
??????? <property name="accessDecisionManager" ref="httpRequestAccessDecisionManager"/>
??????? <property name="objectDefinitionSource" ref="methodDefinitionSource"/>
??? </bean>
??? <bean id="methodDefinitionSource" class="org.springside.modules.security.service.acegi.DBMethodDefinitionSource">
??????? <property name="acegiCacheManager" ref="acegiCacheManager"/>
??? </bean>
2.3 Jcaptcha驗(yàn)證碼
采用 http://jcaptcha.sourceforge.net?作為通用的驗(yàn)證碼方案,請(qǐng)參考SpringSide中的例子,或網(wǎng)上的:
http://www.coachthrasher.com/page/blog?entry=jcaptcha_with_appfuse。
差沙在此過程中又發(fā)現(xiàn)acegi logout filter的錯(cuò)誤,進(jìn)行了修正。
另外它默認(rèn)提供的圖片比較難認(rèn),我們custom了一個(gè)美觀一點(diǎn)的版本。
posted @
2007-02-02 20:31 西紅柿(tomato) 閱讀(462) |
評(píng)論 (0) |
編輯 收藏
摘要:
閱讀全文
posted @
2007-02-02 20:28 西紅柿(tomato) 閱讀(940) |
評(píng)論 (0) |
編輯 收藏
Subscriber:www.1cn.biz
Subscriber Code: jLR8ZC-444-55-4467865481680090
Subscriber:www.1cn.biz
Subscriber Code: jLR8ZC-444-55-4467865481680090
posted @
2006-12-14 15:54 西紅柿(tomato) 閱讀(584) |
評(píng)論 (0) |
編輯 收藏
格式化輸出NumberFormat
相關(guān)內(nèi)容:Local,?DataFormat,?MessageFormat,?SimpleDateFormat,?Format
---------------------------------------------------------------
java.text.NumberFormat類有三個(gè)方法可以產(chǎn)生下列數(shù)據(jù)的標(biāo)準(zhǔn)格式化器:
數(shù)字
貨幣
百分?jǐn)?shù)
---------------------------------------------------------------
創(chuàng)建格式化器(默認(rèn)地區(qū)Local格式):
NumberFormat.getNumberInstance();
NumberFormat.getCurrencyInstance();
NumberFormat.getPercentInstance();
---------------------------------------------------------------
例題:
double?dbl=10000.0/3;
NumberFormat?formatter=NumberFormat.getNumberInstance();
String?s=formatter.format(x);
System.out.println(s);
---------------------------------------------------------------
設(shè)定整數(shù)或小數(shù)部分所顯示的最少和最多位數(shù),可以使用NumberFormat類
的方法:
setMinimumIntegerDigits(int)
setMinimumFractionDigits(int)
setMaximumIntegerDigits(int)
setMaximumFractionDigits(int)
設(shè)定小數(shù)部分的最多位很有用處。如果小數(shù)部分丟失的第一位數(shù)字大于等于5,
那么顯示的最后一位會(huì)增1(四舍五入)。如果要顯示尾隨的零,可以把小數(shù)部分的最少位等于最多位。
如果不想顯示,可以把小數(shù)部分的最少位設(shè)定為0或不設(shè)定。
指定最多位整數(shù)相當(dāng)危險(xiǎn),顯示值將會(huì)被截?cái)啵a(chǎn)生一個(gè)錯(cuò)誤的值。
---------------------------------------------------------------
測試?yán)}:
文件名TestNumberFormat.java
--------------------------------------------------------
import?java.text.NumberFormat;
public?class?TestNumberFormat
{
????public?static?void?main(String[]?args)?{
????????????NumberFormat?nFormat=NumberFormat.getNumberInstance();
????????????nFormat.setMinimumIntegerDigits(3);//設(shè)置整數(shù)部分至少為3位
????????????nFormat.setMaximumFractionDigits(5);//設(shè)置小數(shù)點(diǎn)后面尾數(shù)為5
????????System.out.println("Format?Out?3.2128345="+nFormat.format(3.2128345));
????????????NumberFormat?cFormat=NumberFormat.getCurrencyInstance();
????????????cFormat.setMaximumFractionDigits(3);
????????System.out.println("Format?Out?321283.47656="+cFormat.format(321283.47656));
????????????NumberFormat?pFormat=NumberFormat.getPercentInstance();
????????????pFormat.setMaximumFractionDigits(4);
????????System.out.println("Format?Out?3.2128345="+pFormat.format(3.2128345));
????????System.out.println("Format?Out?null="+nFormat.format(null));//參數(shù)是null,出現(xiàn)異常
????????//Throws??IllegalArgumentException
????}
}
//=================?運(yùn)行結(jié)果如下?=======================
Format?Out?3.2128345=003.21283
Format?Out?321283.47656=¥321,283.477
Format?Out?3.2128345=321.2834%
Exception?in?thread?"main"?java.lang
posted @
2006-12-14 15:50 西紅柿(tomato) 閱讀(15685) |
評(píng)論 (1) |
編輯 收藏
Log4j由三個(gè)重要的組件構(gòu)成:日志信息的優(yōu)先級(jí),日志信息的輸出目的地,日志信息的輸出格式。日志信息的優(yōu)先級(jí)從高到低有ERROR、WARN、INFO、DEBUG,分別用來指定這條日志信息的重要程度;日志信息的輸出目的地指定了日志將打印到控制臺(tái)還是文件中;而輸出格式則控制了日志信息的顯示內(nèi)容。
一、定義配置文件
其實(shí)您也可以完全不使用配置文件,而是在代碼中配置Log4j環(huán)境。但是,使用配置文件將使您的應(yīng)用程序更加靈活。Log4j支持兩種配置文件格式,一種是XML格式的文件,一種是Java特性文件(鍵=值)。下面我們介紹使用Java特性文件做為配置文件的方法:
1.配置根Logger,其語法為:
log4j.rootLogger = [ level ] , appenderName, appenderName, …
其中,level 是日志記錄的優(yōu)先級(jí),分為OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者您定義的級(jí)別。Log4j建議只使用四個(gè)級(jí)別,優(yōu)先級(jí)從高到低分別是ERROR、WARN、INFO、DEBUG。通過在這里定義的級(jí)別,您可以控制到應(yīng)用程序中相應(yīng)級(jí)別的日志信息的開關(guān)。比如在這里定義了INFO級(jí)別,則應(yīng)用程序中所有DEBUG級(jí)別的日志信息將不被打印出來。 appenderName就是指定日志信息輸出到哪個(gè)地方。您可以同時(shí)指定多個(gè)輸出目的地。
2.配置日志信息輸出目的地Appender,其語法為:
log4j.appender.appenderName = fully.qualified.name.of.appender.class
log4j.appender.appenderName.option1 = value1
…
log4j.appender.appenderName.option = valueN
其中,Log4j提供的appender有以下幾種:
org.apache.log4j.ConsoleAppender(控制臺(tái)),
org.apache.log4j.FileAppender(文件),
org.apache.log4j.DailyRollingFileAppender(每天產(chǎn)生一個(gè)日志文件),
org.apache.log4j.RollingFileAppender(文件大小到達(dá)指定尺寸的時(shí)候產(chǎn)生一個(gè)新的文件),
org.apache.log4j.WriterAppender(將日志信息以流格式發(fā)送到任意指定的地方)
3.配置日志信息的格式(布局),其語法為:
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class
log4j.appender.appenderName.layout.option1 = value1
…
log4j.appender.appenderName.layout.option = valueN
其中,Log4j提供的layout有以下幾種:
org.apache.log4j.HTMLLayout(以HTML表格形式布局),
org.apache.log4j.PatternLayout(可以靈活地指定布局模式),
org.apache.log4j.SimpleLayout(包含日志信息的級(jí)別和信息字符串),
org.apache.log4j.TTCCLayout(包含日志產(chǎn)生的時(shí)間、線程、類別等等信息)
Log4J采用類似C語言中的printf函數(shù)的打印格式格式化日志信息,打印參數(shù)如下: %m 輸出代碼中指定的消息
%p 輸出優(yōu)先級(jí),即DEBUG,INFO,WARN,ERROR,F(xiàn)ATAL
%r 輸出自應(yīng)用啟動(dòng)到輸出該log信息耗費(fèi)的毫秒數(shù)
%c 輸出所屬的類目,通常就是所在類的全名
%t 輸出產(chǎn)生該日志事件的線程名
%n 輸出一個(gè)回車換行符,Windows平臺(tái)為“\r\n”,Unix平臺(tái)為“\n”
%d 輸出日志時(shí)間點(diǎn)的日期或時(shí)間,默認(rèn)格式為ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},輸出類似:2002年10月18日 22:10:28,921
%l 輸出日志事件的發(fā)生位置,包括類目名、發(fā)生的線程,以及在代碼中的行數(shù)。舉例:Testlog4.main(TestLog4.java:10)
二、在代碼中使用Log4j
1.得到記錄器
使用Log4j,第一步就是獲取日志記錄器,這個(gè)記錄器將負(fù)責(zé)控制日志信息。其語法為:
public static Logger getLogger( String name)
通過指定的名字獲得記錄器,如果必要的話,則為這個(gè)名字創(chuàng)建一個(gè)新的記錄器。Name一般取本類的名字,比如:
static Logger logger = Logger.getLogger ( ServerWithLog4j.class.getName () )
2.讀取配置文件
當(dāng)獲得了日志記錄器之后,第二步將配置Log4j環(huán)境,其語法為:
BasicConfigurator.configure (): 自動(dòng)快速地使用缺省Log4j環(huán)境。
PropertyConfigurator.configure ( String configFilename) :讀取使用Java的特性文件編寫的配置文件。
DOMConfigurator.configure ( String filename ) :讀取XML形式的配置文件。
3.插入記錄信息(格式化日志信息)
當(dāng)上兩個(gè)必要步驟執(zhí)行完畢,您就可以輕松地使用不同優(yōu)先級(jí)別的日志記錄語句插入到您想記錄日志的任何地方,其語法如下:
Logger.debug ( Object message ) ;
Logger.info ( Object message ) ;
Logger.warn ( Object message ) ;
Logger.error ( Object message ) ;
posted @
2006-12-14 15:48 西紅柿(tomato) 閱讀(349) |
評(píng)論 (0) |
編輯 收藏
Acegi1.0.3 使用的是Spring1.2.8
本人經(jīng)過對(duì)Acegi1.0.3 的源代碼進(jìn)行刪選,并引入到Spring2.0.1環(huán)境下,做了嚴(yán)格的測試。
本demo 就是 在 Spring2.0.1 + Hibernate3.2 環(huán)境下,針對(duì)視圖層使用 Struts 和 JSF 分別進(jìn)行了集成,簡單鮮明描述如何使用 Acegi1.0.3 進(jìn)行基于用戶角色的動(dòng)態(tài)授權(quán)安全控制。
本demo既適合于JSF + Spring2.0.1 + Hibernate3.2 項(xiàng)目也適合于Struts + Spring2.0.1 + Hibernate3.2項(xiàng)目.
未列入特別說明沒有授權(quán)的企業(yè)和個(gè)人都可以自由免費(fèi)下載使用提供寶貴意見.
本人不急功近利,正在做完整的文檔編寫,做最后嚴(yán)格的測試,近期在此免費(fèi)發(fā)布,敬請(qǐng)關(guān)注.
posted @
2006-12-11 12:21 西紅柿(tomato) 閱讀(1814) |
評(píng)論 (34) |
編輯 收藏
摘要: SQL中的單記錄函數(shù)
??2
1
.
ASCII
??3
返回與指定的字符對(duì)應(yīng)的十進(jìn)制數(shù);
??4
SQL
>
?
select
?
ascii
(
'
A
'
)?A,
ascii
(
...
閱讀全文
posted @
2006-12-11 12:07 西紅柿(tomato) 閱讀(391) |
評(píng)論 (0) |
編輯 收藏
一:Oracle
select * from (select rownum,name from table where rownum <=endIndex )
where rownum > startIndex
二:DB2
DB2分頁查詢
SELECT * FROM (Select 字段1,字段2,字段3,rownumber() over(ORDER BY 排序用的列名 ASC) AS rn from 表名) AS a1 WHERE a1.rn BETWEEN 10 AND 20
以上表示提取第10到20的紀(jì)錄
select * from (select rownumber() over(order by id asc ) as rowid from table where rowid <=endIndex )
where rowid > startIndex
三:MySQL
:select ? * ? from ? table ? limit ? start,pageNum??
posted @
2006-12-11 12:06 西紅柿(tomato) 閱讀(4709) |
評(píng)論 (4) |
編輯 收藏
JS檢測郵箱地址正則表達(dá)式
|
[ yangliangy 發(fā)表于 2006-11-29 14:36:00 ]
|
?var strm = document.myform.CoMail.value?? //提交mail地址的文本框 ?var regm = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;//驗(yàn)證Mail的正則表達(dá)式,^[a-zA-Z0-9_-]:開頭必須為字母,下劃線,數(shù)字, ?if (!strm.match(regm) && strm!="") ?? { ???? alert("郵箱地址格式錯(cuò)誤或含有非法字符!\n請(qǐng)檢查!"); ??document.myform.CoMail.select();?? ??return false; ??? }
|
posted @
2006-12-03 18:11 西紅柿(tomato) 閱讀(10704) |
評(píng)論 (7) |
編輯 收藏
?
-----------------------------------------------------------
<script> ?
? function ? show() ?
? { ?
? if ? (t2.style.display=="none") ?
? { ?
? t2.style.display="" ?
? //t3.style.display="none" ?
? } ?
? else ?
? { ?
? t2.style.display="none" ?
? //t3.style.display="" ?
? } ?
? ?
? } ?
? </script> ?
? <table ? name="t1" ? border="2"> ?
? ? ? <tr> ?
? ? ? ? ? <td> ?
? ? ? ? ? ? ? <form ? name="form1"> ?
? ? ? ? ? ? ? ? ? <input ? type="radio" ? onclick="show()">顯示/影藏 ?
? ? ? ? ? ? ? </form> ?
? ? ? ? ? </td> ?
? ? ? </tr> ?
? </table> ?
? <table ? id="t2" ? border="2" ? style="display:none"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td>t2</td> ?
? ? ? </tr> ?
? </table> ?
? <table ? id="t3" ? border="2"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td>t3</td> ?
? ? ? </tr> ?
? </table> ?
? <table ? id="t4" ? border="2"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td>t4</td> ?
? ? ? </tr> ?
? </table> ?
----------------------------------------------------------------------
<table ? name="t1" ? ? width="100%" ? border="0" ? cellspacing="0" ? cellpadding="0"> ?
? ? ? <tr> ?
? ? ? ? ? <td> ?
? ? ? ? ? ? ? <form ? name="form1" ? method="post" ? action=""> ?
? ? ? ? ? ? ? ? ? <input ? type="checkbox" ? name="radiobutton" ? value="radiobutton" ? onclick="t2.style.display=checked?'':'none'" ? checked> ?
? ? ? ? ? ? ? ? ? 顯示/隱藏 ? ? ? ? ? ?
? ? ? ? ? ? ? </form> ?
? ? ? ? ? </td> ?
? ? ? </tr> ?
? </table> ?
? <table ? id="t2" ? width="100%" ? border="0" ? cellspacing="0" ? cellpadding="0"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td> 2</td> ?
? ? ? </tr> ?
? </table> ?
? <table ? name="t3" ? ? width="100%" ? border="0" ? cellspacing="0" ? cellpadding="0"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td> 3</td> ?
? ? ? </tr> ?
? </table> ?
? <table ? name="t4" ? width="100%" ? border="0" ? cellspacing="0" ? cellpadding="0"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td> 4</td> ?
? ? ? </tr> ?
? </table> ?
-----------------------------------------------------------------------
<table ? name="t1" ? ? width="100%" ? border="0" ? cellspacing="0" ? cellpadding="0"> ?
? ? ? <tr> ?
? ? ? ? ? <td> ?
? ? ? ? ? ? ? <form ? name="form1" ? method="post" ? action=""> ?
? ? ? ? ? ? ? ? ? <input ? type="checkbox" ? name="radiobutton" ? value="radiobutton" ? onclick="t2.style.display=checked?'':'none';t3.style.display=checked?'none':''"> ?
? ? ? ? ? ? ? ? ? 顯示/隱藏 ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? </form> ?
? ? ? ? ? </td> ?
? ? ? </tr> ?
? </table> ?
? <table ? id="t2" ? width="100%" ? border="0" ? cellspacing="0" ? cellpadding="0" ? style="display:none"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td> 2</td> ?
? ? ? </tr> ?
? </table> ?
? <table ? id="t3" ? ? width="100%" ? border="0" ? cellspacing="0" ? cellpadding="0"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td> 3</td> ?
? ? ? </tr> ?
? </table> ?
? <table ? name="t4" ? width="100%" ? border="0" ? cellspacing="0" ? cellpadding="0"> ?
? ? ? <tr> ? ?
? ? ? ? ? <td> 4</td> ?
? ? ? </tr> ?
? </table>
---------------------------------------------------------------------------
<table id="sbr">
<tr>
<td>sdaf</td>
</tr>
<table>
<input name=button1 type=button value="按" onclick=show()>
<script language=javascript>
function show()
{
var obj=document.getElementById("sbr")
obj.style.display=(obj.style.display=="")?"none":""
}
</script>
posted @
2006-12-03 17:14 西紅柿(tomato) 閱讀(2824) |
評(píng)論 (0) |
編輯 收藏
摘要: 轉(zhuǎn)自:http://www.cublog.cn/u/11905/showart_162625.html最近在做項(xiàng)目遇到了權(quán)限管理,用戶要求可以自己建立不同的角色對(duì)系統(tǒng)的資源進(jìn)行控制, 不同的用戶有不同的角色,又恰恰框架中用到了struts+spring+hibernate,要求在web層調(diào)用 業(yè)務(wù)邏輯層 時(shí)不考慮權(quán)限,web層可以控制用戶的顯示界面,邏輯層處理用戶權(quán)限問題。 想來想去好像只有spr...
閱讀全文
posted @
2006-12-03 16:04 西紅柿(tomato) 閱讀(1564) |
評(píng)論 (1) |
編輯 收藏
常用正則表達(dá)式
只能輸入數(shù)字:“^[0-9]*$”
只能輸入n位的數(shù)字:“^\d{n}$”
只能輸入至少n位數(shù)字:“^\d{n,}$”
只能輸入m-n位的數(shù)字:“^\d{m,n}$”
只能輸入零和非零開頭的數(shù)字:“^(0|[1-9][0-9]*)$”
只能輸入有兩位小數(shù)的正實(shí)數(shù):“^[0-9]+(.[0-9]{2})?$”
只能輸入有1-3位小數(shù)的正實(shí)數(shù):“^[0-9]+(.[0-9]{1,3})?$”
只能輸入非零的正整數(shù):“^\+?[1-9][0-9]*$”
只能輸入非零的負(fù)整數(shù):“^\-[1-9][0-9]*$”
只能輸入長度為3的字符:“^.{3}$”
只能輸入由26個(gè)英文字母組成的字符串:“^[A-Za-z]+$”
只能輸入由26個(gè)大寫英文字母組成的字符串:“^[A-Z]+$”
只能輸入由26個(gè)小寫英文字母組成的字符串:“^[a-z]+$”
只能輸入由數(shù)字和26個(gè)英文字母組成的字符串:“^[A-Za-z0-9]+$”
只能輸入由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串:“^\w+$”
驗(yàn)證用戶密碼:“^[a-zA-Z]\w{5,17}$”正確格式為:以字母開頭,長度在6-18之間,
只能包含字符、數(shù)字和下劃線。
驗(yàn)證是否含有^%&',;=?$\"等字符:“[^%&',;=?$\x22]+”
只能輸入漢字:“^[\u4e00-\u9fa5],{0,}$”
驗(yàn)證Email地址:“^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$”
驗(yàn)證InternetURL:“^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$”
驗(yàn)證電話號(hào)碼:“^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$”
正確格式為:“XXXX-XXXXXXX”,“XXXX-XXXXXXXX”,“XXX-XXXXXXX”,
“XXX-XXXXXXXX”,“XXXXXXX”,“XXXXXXXX”。
驗(yàn)證身份證號(hào)(15位或18位數(shù)字):“^\d{15}|\d{}18$”
驗(yàn)證一年的12個(gè)月:“^(0?[1-9]|1[0-2])$”正確格式為:“01”-“09”和“1”“12”
驗(yàn)證一個(gè)月的31天:“^((0?[1-9])|((1|2)[0-9])|30|31)$”
正確格式為:“01”“09”和“1”“31”。
用正則表達(dá)式限制文本框只能輸入數(shù)字,小數(shù)點(diǎn),英文字母,漢字等各類代碼
1.文本框只能輸入數(shù)字代碼(小數(shù)點(diǎn)也不能輸入)
<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
2.只能輸入數(shù)字,能輸小數(shù)點(diǎn).
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能輸入數(shù)字');this.value='';}">
3.數(shù)字和小數(shù)點(diǎn)方法二
<input type=text t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}">
4.只能輸入字母和漢字
<input onkeyup="value=value.replace(/[\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))" maxlength=10 name="Numbers">
5.只能輸入英文字母和數(shù)字,不能輸入中文
<input onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">
6.只能輸入數(shù)字和英文<font color="Red">chun</font>
<input onKeyUp="value=value.replace(/[^\d|chun]/g,'')">
7.小數(shù)點(diǎn)后只能有最多兩位(數(shù)字,中文都可輸入),不能輸入字母和運(yùn)算符號(hào):
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
8.小數(shù)點(diǎn)后只能有最多兩位(數(shù)字,字母,中文都可輸入),可以輸入運(yùn)算符號(hào):
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
?
posted @
2006-12-03 16:00 西紅柿(tomato) 閱讀(480) |
評(píng)論 (0) |
編輯 收藏