spring+ jcaptcha(spring框架下的彩色驗(yàn)證碼)
系統(tǒng)
1939 0
1、從jcaptcha官方網(wǎng)站下載jcaptcha的發(fā)行包,并將其發(fā)行包中的jar文件考貝到本地項(xiàng)目WEB-INF目錄下的lib目錄中。
官方網(wǎng)址
http://jcaptcha.sourceforge.net/
2、在web.xml文件中配置
-
<servlet> ??
-
?????<servlet-name>jcaptcha</servlet-name> ??
-
?????<servlet-
class
>cn.hxex.order.core.jcaptcha.ImageCaptchaServlet</servlet-
class
> ??
-
?????<load-on-startup>
3
</load-on-startup> ??
-
?</servlet> ??
-
??
-
?<servlet-mapping> ??
-
?????<servlet-name>jcaptcha</servlet-name> ??
-
?????<url-pattern>/captcha.jpg</url-pattern> ??
-
?</servlet-mapping>??
<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>cn.hxex.order.core.jcaptcha.ImageCaptchaServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jcaptcha</servlet-name>
<url-pattern>/captcha.jpg</url-pattern>
</servlet-mapping>
3、jcaptcha在spring中的配置
<bean id="channelProcessingFilter"
class="org.acegisecurity.securechannel.ChannelProcessingFilter">
<property name="channelDecisionManager">
<ref local="channelDecisionManager"/>
</property>
<property name="filterInvocationDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/j_security_check=REQUIRES_CAPTCHA_ONCE_ABOVE_THRESOLD_REQUESTS
</value>
</property>
</bean>
<bean id="channelDecisionManager"
class="org.acegisecurity.securechannel.ChannelDecisionManagerImpl">
<property name="channelProcessors">
<list>
<ref local="testOnceAfterMaxRequestsCaptchaChannelProcessor"/>
<ref local="alwaysTestAfterTimeInMillisCaptchaChannelProcessor"/>
<ref local="alwaysTestAfterMaxRequestsCaptchaChannelProcessor"/>
<ref local="alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor"/>
</list>
</property>
</bean>
<!-- REQUIRES_CAPTCHA_ONCE_ABOVE_THRESOLD_REQUESTS -->
<bean id="testOnceAfterMaxRequestsCaptchaChannelProcessor"
class="org.acegisecurity.captcha.TestOnceAfterMaxRequestsCaptchaChannelProcessor">
<property name="thresold">
<value>0</value>
</property>
<property name="entryPoint">
<ref bean="captchaEntryPoint"/>
</property>
</bean>
<!-- REQUIRES_CAPTCHA_ABOVE_THRESOLD_REQUESTS -->
<bean id="alwaysTestAfterMaxRequestsCaptchaChannelProcessor"
class="org.acegisecurity.captcha.AlwaysTestAfterMaxRequestsCaptchaChannelProcessor">
<property name="thresold">
<value>5</value>
</property>
<property name="entryPoint">
<ref bean="captchaEntryPoint"/>
</property>
</bean>
<!-- REQUIRES_CAPTCHA_AFTER_THRESOLD_IN_MILLIS -->
<bean id="alwaysTestAfterTimeInMillisCaptchaChannelProcessor"
class="org.acegisecurity.captcha.AlwaysTestAfterTimeInMillisCaptchaChannelProcessor">
<property name="thresold">
<value>5000</value>
</property>
<property name="entryPoint">
<ref bean="captchaEntryPoint"/>
</property>
</bean>
<!-- REQUIRES_CAPTCHA_BELOW_AVERAGE_TIME_IN_MILLIS_REQUESTS -->
<bean
id="alwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor"
class="org.acegisecurity.captcha.AlwaysTestBelowAverageTimeInMillisBetweenRequestsChannelProcessor">
<property name="thresold">
<value>20000</value>
</property>
<property name="entryPoint">
<ref bean="captchaEntryPoint"/>
</property>
</bean>
<bean id="captchaEntryPoint"
class="org.acegisecurity.captcha.CaptchaEntryPoint">
<!--驗(yàn)證碼驗(yàn)證失敗后轉(zhuǎn)向的頁(yè)面!-->
<property name="captchaFormUrl">
<value>/admin/login.jsp?login_error=code_error</value>
</property>
<property name="includeOriginalRequest">
<value>false</value>
</property>
<property name="includeOriginalParameters">
<value>false</value>
</property>
</bean>
<bean id="captchaValidationProcessingFilter"
class="org.acegisecurity.captcha.CaptchaValidationProcessingFilter">
<property name="captchaService">
<ref bean="captchaService"/>
</property>
<property name="captchaValidationParameter" value="j_captcha_response"/>
</bean>
<!-- imageCaptchaService is injected into captchaImageCreateController as well as to captchaService beans -->
<!--自己定義的實(shí)體類(注意路徑!!)-->
<bean id="captchaService" class="cn.hxex.order.core.jcaptcha.JCaptchaServiceProxyImpl">
<property name="jcaptchaService" ref="imageCaptchaService"/>
</bean>
<bean id="imageCaptchaService" class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">
<constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">
<ref bean="fastHashMapCaptchaStore"/>
</constructor-arg>
<!-- (1) which captcha Engine you use -->
<constructor-arg type="com.octo.captcha.engine.CaptchaEngine" index="1">
<ref bean="captchaEngineEx"/>
</constructor-arg>
<constructor-arg index="2">
<value>180</value>
</constructor-arg>
<constructor-arg index="3">
<value>100000</value>
</constructor-arg>
<constructor-arg index="4">
<value>75000</value>
</constructor-arg>
</bean>
<bean id="fastHashMapCaptchaStore" class="com.octo.captcha.service.captchastore.FastHashMapCaptchaStore"/>
<!-- (2) you can define more than one captcha engine here -->
<bean id="captchaEngineEx"
class="cn.hxex.order.core.jcaptcha.engine.CaptchaEngineEx">
</bean>
<bean id="filterChainProxy"
class="org.acegisecurity.util.FilterChainProxy">
<property name="filterInvocationDefinitionSource">
<value>
CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
PATTERN_TYPE_APACHE_ANT
/**=httpSessionContextIntegrationFilter,captchaValidationProcessingFilter,channelProcessingFilter,authenticationProcessingFilter,exceptionTranslationFilter,filterInvocationInterceptor
</value>
</property>
</bean>
<bean id="httpSessionContextIntegrationFilter"
class="org.acegisecurity.context.HttpSessionContextIntegrationFilter">
<!-- 將下面的property注釋掉,驗(yàn)證碼將無(wú)效!!! -->
<property name="context">
<value>
org.acegisecurity.captcha.CaptchaSecurityContextImpl
</value>
</property>
</bean>
·············省略了一些spring安全框架的bean,自己加去吧
4、編寫jcaptcha的實(shí)體類
實(shí)體類包的路徑一定要和spring配置文件里的路徑一樣
(1)CaptchaEngine 類
-
package
?cn.hxex.order.core.jcaptcha.engine; ??
-
??
-
import
?java.awt.Color; ??
-
??
-
import
?com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator; ??
-
import
?com.octo.captcha.component.image.backgroundgenerator ??
-
??.FunkyBackgroundGenerator; ??
-
import
?com.octo.captcha.component.image.fontgenerator.FontGenerator; ??
-
import
?com.octo.captcha.component.image.fontgenerator ??
-
??.TwistedAndShearedRandomFontGenerator; ??
-
import
?com.octo.captcha.component.image.textpaster.RandomTextPaster; ??
-
import
?com.octo.captcha.component.image.textpaster.TextPaster; ??
-
import
?com.octo.captcha.component.image.wordtoimage.ComposedWordToImage; ??
-
import
?com.octo.captcha.component.image.wordtoimage.WordToImage; ??
-
import
?com.octo.captcha.component.word.wordgenerator.RandomWordGenerator; ??
-
import
?com.octo.captcha.component.word.wordgenerator.WordGenerator; ??
-
import
?com.octo.captcha.engine.image.ListImageCaptchaEngine; ??
-
import
?com.octo.captcha.image.gimpy.GimpyFactory; ??
-
??
-
?
-
?
-
?
-
?
-
??
-
public
?
class
?CaptchaEngine?
extends
?ListImageCaptchaEngine?{ ??
-
??
?
-
?
-
??
-
??
protected
?
void
?buildInitialFactories()?{ ??
-
????WordGenerator?wordGenerator? ??
-
??????=?
new
?RandomWordGenerator(
"023456789"
); ??
-
????
??
-
????
??
-
????TextPaster?textPaster?=?
new
?RandomTextPaster(
4
,
5
,?Color.WHITE); ??
-
????
??
-
????BackgroundGenerator?backgroundGenerator? ??
-
??????=?
new
?FunkyBackgroundGenerator(
100
,
40
); ??
-
????
??
-
????FontGenerator?fontGenerator?=?
new
?TwistedAndShearedRandomFontGenerator(
20
,?
22
); ??
-
????WordToImage?wordToImage?=?
new
?ComposedWordToImage(fontGenerator, ??
-
????????backgroundGenerator,?textPaster); ??
-
????addFactory(
new
?GimpyFactory(wordGenerator,?wordToImage)); ??
-
??} ??
-
}??
package cn.hxex.order.core.jcaptcha.engine;
import java.awt.Color;
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator
.FunkyBackgroundGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator
.TwistedAndShearedRandomFontGenerator;
import com.octo.captcha.component.image.textpaster.RandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
/**
* SpringSide Custom的認(rèn)證圖片
*
* @author cac
*/
public class CaptchaEngine extends ListImageCaptchaEngine {
/**
* @see ListImageCaptchaEngine
*/
protected void buildInitialFactories() {
WordGenerator wordGenerator
= new RandomWordGenerator("023456789");
// nteger minAcceptedWordLength, Integer maxAcceptedWordLength,Color[]
// textColors
TextPaster textPaster = new RandomTextPaster(4,5, Color.WHITE);
// Integer width, Integer height
BackgroundGenerator backgroundGenerator
= new FunkyBackgroundGenerator(100,40);
// Integer minFontSize, Integer maxFontSize
FontGenerator fontGenerator = new TwistedAndShearedRandomFontGenerator(20, 22);
WordToImage wordToImage = new ComposedWordToImage(fontGenerator,
backgroundGenerator, textPaster);
addFactory(new GimpyFactory(wordGenerator, wordToImage));
}
}
(2)CaptchaEngineEx 類
-
package
?cn.hxex.order.core.jcaptcha.engine; ??
-
??
-
import
?java.awt.Color; ??
-
??
-
import
?com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator; ??
-
import
?com.octo.captcha.component.image.backgroundgenerator ??
-
??.GradientBackgroundGenerator; ??
-
import
?com.octo.captcha.component.image.color.SingleColorGenerator; ??
-
import
?com.octo.captcha.component.image.fontgenerator.FontGenerator; ??
-
import
?com.octo.captcha.component.image.fontgenerator.RandomFontGenerator; ??
-
import
?com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster; ??
-
import
?com.octo.captcha.component.image.textpaster.TextPaster; ??
-
import
?com.octo.captcha.component.image.textpaster.textdecorator ??
-
??.BaffleTextDecorator; ??
-
import
?com.octo.captcha.component.image.textpaster.textdecorator ??
-
??.LineTextDecorator; ??
-
import
?com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator; ??
-
import
?com.octo.captcha.component.image.wordtoimage.ComposedWordToImage; ??
-
import
?com.octo.captcha.component.image.wordtoimage.WordToImage; ??
-
import
?com.octo.captcha.component.word.wordgenerator.RandomWordGenerator; ??
-
import
?com.octo.captcha.component.word.wordgenerator.WordGenerator; ??
-
import
?com.octo.captcha.engine.image.ListImageCaptchaEngine; ??
-
import
?com.octo.captcha.image.gimpy.GimpyFactory; ??
-
??
-
??
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
?
-
??
-
public
?
class
?CaptchaEngineEx?
extends
?ListImageCaptchaEngine?{ ??
-
??
?
-
?
-
??
-
??
protected
?
void
?buildInitialFactories()?{ ??
-
???? ??
-
?????
??
-
????Integer?minAcceptedWordLength?=?
new
?Integer(
4
); ??
-
????Integer?maxAcceptedWordLength?=?
new
?Integer(
5
); ??
-
????
??
-
????Integer?imageHeight?=?
new
?Integer(
40
); ??
-
????Integer?imageWidth?=?
new
?Integer(
100
); ??
-
???? ??
-
????
??
-
????Integer?minFontSize?=?
new
?Integer(
20
); ??
-
????Integer?maxFontSize?=?
new
?Integer(
22
); ??
-
????
??
-
????
??
-
????WordGenerator?wordGenerator? ??
-
??????=?
new
?RandomWordGenerator(
"023456789"
); ??
-
? ??
-
?????
??
-
?????
??
-
?????
??
-
????BackgroundGenerator?backgroundGenerator?=?
new
?GradientBackgroundGenerator( ??
-
????????imageWidth,?imageHeight,?Color.white,?Color.white); ??
-
?? ??
-
?????
??
-
?????
??
-
????FontGenerator?fontGenerator?=?
new
?RandomFontGenerator(minFontSize, ??
-
????????maxFontSize);???? ??
-
?????
??
-
????SingleColorGenerator?scg?=?
new
?SingleColorGenerator(Color.blue); ??
-
?? ??
-
?????
??
-
?????
??
-
????? ??
-
????LineTextDecorator?lineDecorator?=?
new
?LineTextDecorator(
1
,?Color.blue); ??
-
????
??
-
????TextDecorator[]?textdecorators?=?
new
?TextDecorator[
1
]; ??
-
??
-
????textdecorators[
0
]?=?lineDecorator; ??
-
????
??
-
??
-
????TextPaster?textPaster?=?
new
?DecoratedRandomTextPaster( ??
-
????????minAcceptedWordLength,?maxAcceptedWordLength,?scg, ??
-
????????
new
?TextDecorator[]?{?
new
?BaffleTextDecorator(
new
?Integer(
1
), ??
-
????????????Color.white)?}); ??
-
??
-
????
spring+ jcaptcha(spring框架下的彩色驗(yàn)證碼)
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元