Maven导入本地Kaptcha谷歌验证码并在程序中使用

问题

在用Maven之前,kaptcha的jar包是下载后作为LIbrary导入项目的,但是在项目使用maven后,maven上不存在kaptcha的坐标,只能下载jar包到本地并添加到项目中。

然而问题是:maven只能打包pom.xml里面声明的依赖,不能识别本地jar包。本文介绍把本地jkaptcha的jar包添加到pom.xml中,并在Java项目中使用的方法

解决方法

1. 下载jar包

kaptcha下载地址

下载kaptcha-2.3.2.jar包并解压

2. maven安装本地jar包

cmd进入jar包所在文件夹,输入以下命令
注意:maven会根据安装目录的config目录下的settings.xml中的仓库地址来安装jar包,要更改安装到的仓库位置(默认c盘)请自行百度

mvn install:install-file -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3.2 -Dfile=kaptcha-2.3.2.jar -Dpackaging=jar -DgeneratePom=true

3. 引入坐标

在pom.xml中引入坐标

    <dependency>
      <groupId>com.google.code</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3.2</version>
    </dependency>

4. 使用验证码功能

4.1 applicationContext.xml下配置bean

<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
        <property name="config">
            <bean class="com.google.code.kaptcha.util.Config">
                <constructor-arg>
                    <props>
                        <prop key="kaptcha.border">yes</prop>
                        <prop key="kaptcha.border.color">105,179,90</prop>
                        <prop key="kaptcha.textproducer.font.color">blue</prop>
                        <prop key="kaptcha.image.width">125</prop>
                        <prop key="kaptcha.image.height">45</prop>
                        <prop key="kaptcha.textproducer.font.size">45</prop>
                        <prop key="kaptcha.session.key">code</prop>
                        <prop key="kaptcha.textproducer.char.length">4</prop>
                        <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
                    </props>
                </constructor-arg>
            </bean>
        </property>
    </bean>

4.2 业务代码

Controller层新建KaptchaController类

@Controller
public class KaptchaController {
    @Autowired
    private Producer captchaProducer;

    @RequestMapping("/kaptcha.jpg")
    public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        System.out.println("******************验证码是: " + code + "******************");

        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();

        // store the text in the session
        session.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;
    }

}

5. jsp中使用

<img id="code_img" alt="" src="kaptcha.jpg">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值