温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Kaptcha 验证码在springMVC中的使用

发布时间:2020-08-05 23:29:14 来源:网络 阅读:1811 作者:爱笑DE朝 栏目:开发技术

项目中配置 Kaptcha 验证码分一下几步:

        1,添加Maven依赖

          <dependency>
            <groupId>com.github.axet</groupId>
            <artifactId>kaptcha</artifactId>
            <version>0.0.9</version>
        </dependency>

       2,spring容器中注入kaptcha,自定义其相关属性

          <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
        <property name="config">
            <bean class="com.google.code.kaptcha.util.Config">
                <!--通过构造函数注入属性值 -->
                <constructor-arg type="java.util.Properties">
                    <props>
                        <!-- 验证码宽度 -->
                        <prop key="kaptcha.p_w_picpath.width">150</prop>
                        <!-- 验证码高度 -->
                        <prop key="kaptcha.p_w_picpath.height">60</prop>
                        <!-- 生成验证码内容范围 -->
                        <prop key="kaptcha.textproducer.char.string">abcde2345678gfynmnpwx</prop>
                        <!-- 验证码个数 -->
                        <prop key="kaptcha.textproducer.char.length">5</prop>
                        <!-- 是否有边框 -->
                        <prop key="kaptcha.border">no</prop>
                        <!-- 验证码字体颜色 -->
                        <prop key="kaptcha.textproducer.font.color">red</prop>
                        <!-- 验证码字体大小 -->
                        <prop key="kaptcha.textproducer.font.size">35</prop>
                        <!-- 验证码所属字体样式 -->
                        <prop key="kaptcha.textproducer.font.names">Arial, Courier</prop>
                        <prop key="kaptcha.background.clear.from">white</prop>
                        <prop key="kaptcha.background.clear.to">white</prop>
                        <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>
                        <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
                        <!-- 干扰线颜色 -->
                        <prop key="kaptcha.noise.color">red</prop>
                        <!-- 验证码文本字符间距 -->
                        <prop key="kaptcha.textproducer.char.space">4</prop>
                    </props>
                </constructor-arg>
            </bean>
        </property>
    </bean>

     3 在控制器中写获取验证图片的方法

   /**
     * Kaptcha 验证码
     */
    @Autowired
    private DefaultKaptcha captchaProducer;

    @RequestMapping(value = "captcha.htm", method = RequestMethod.GET)
    public void captcha(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control",
                "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("p_w_picpath/jpeg");
        String capText = captchaProducer.createText();
        request.getSession().setAttribute(
                com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY, capText);
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(bi, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();
        }
    }

   4,前台调用

      <img   id="captcha"
                  onclick="this.src='${BASE_PATH}/admin/captcha.htm?'+Math.random();"
                  src="${BASE_PATH}/captcha.htm">

  5,后台验证时从Session中获取验证码

           String kaptcha = (String) request.getSession().getAttribute(
                    com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI