SpringMVC集成google验证码

第一步、引入google验证码需要的jar包

第二步、在Spring配置文件中添加google的配置文件,把验证码以bean的形式加载到Spring容器中

<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
		<property name="config">
			<bean class="com.google.code.kaptcha.util.Config">
				<constructor-arg>
					<props>
						<!-- 是否有边框  默认为true  我们可以自己设置yes,no   -->
						<prop key="kaptcha.border">no</prop>
						<!-- 边框颜色   默认为Color.BLACK   -->
						<prop key="kaptcha.border.color">105,179,90</prop>
						<!-- 验证码文本字符颜色  默认为Color.BLACK  -->
						<!-- <prop key="kaptcha.textproducer.font.color">91,199,211</prop> -->
						<prop key="kaptcha.textproducer.font.color">50,186,154</prop>
						<!--  验证码图片宽度  默认为200   -->
						<prop key="kaptcha.image.width">500</prop>
						<!-- 验证码图片高度  默认为50   -->
						<prop key="kaptcha.image.height">120</prop>
						<!-- 验证码文本字符间距  默认为2 --> 
						<prop key="kaptcha.textproducer.char.space">10</prop>
						<!-- 验证码文本字符大小  默认为40   -->
						<prop key="kaptcha.textproducer.font.size">100</prop>
						<!-- session key -->
						<prop key="kaptcha.session.key">code</prop>
						<!-- 验证码文本字符长度  默认为5 -->
						<prop key="kaptcha.textproducer.char.length">6</prop>
						<!-- 验证码背景颜色渐进   默认为Color.LIGHT_GRAY -->
						<prop key="kaptcha.background.clear.from">255,255,255</prop>
						<!-- 验证码背景颜色渐进   默认为Color.WHITE -->
						<prop key="kaptcha.background.clear.to">255,255,255</prop>
						<!-- 验证码文本字体样式  默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) -->
						<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
						<!-- 去除干扰线 -->
						<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
					</props>
				</constructor-arg>
			</bean>
		</property>
	</bean>
到这里验证码已经被配置到Spring容器中。

3、我们需要定义一个工具类来完成验证码的显示功能

@Controller  
@RequestMapping("CaptchaImageCreateController")  
public class CaptchaImageCreateController {  
      
    private Producer captchaProducer = null;  
  
    @Autowired  
    public void setCaptchaProducer(Producer captchaProducer) {  
        this.captchaProducer = captchaProducer;  
    }  
  
    @RequestMapping("/captcha-image")  
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {  
        response.setDateHeader("Expires", 0);  
        // Set standard HTTP/1.1 no-cache headers.  
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");  
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).  
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");  
        // Set standard HTTP/1.0 no-cache header.  
        response.setHeader("Pragma", "no-cache");  
        // return a jpeg  
        response.setContentType("image/jpeg");  
        // create the text for the image  
        String capText = captchaProducer.createText();  
//        System.out.println("---***---***---当前验证码为"+capText+"---***---***---");
        // store the text in the session  
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);  
        // create the image with the text  
        BufferedImage bi = captchaProducer.createImage(capText);  
        ServletOutputStream out = response.getOutputStream();  
        // write the data out  
        ImageIO.write(bi, "jpg", out);  
        try {  
            out.flush();  
        } finally {  
            out.close();  
        }  
        return null;  
    }  
  
}  
4、页面直接请求地址就可以得到一个图片,显示就可以

5、后台验证验证码可以直接在session中取的值比对即可


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值