溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何用Jpatchca和Spring MVC實現(xiàn)驗證碼

發(fā)布時間:2020-06-17 09:21:14 來源:億速云 閱讀:156 作者:Leah 欄目:web開發(fā)

如何用Jpatchca和Spring MVC實現(xiàn)驗證碼?這篇文章運用了實例代碼展示,代碼非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

Jpatchca不支持集群環(huán)境,默認的驗證碼不是保存在session中,如果想做個性化的處理很麻煩。其實我想要的就是一個聲稱圖片的流就ok了,剩下的事情就交給程序員自己實現(xiàn)吧。

最終,選擇了patchca(另一個開源的組件)來實現(xiàn),這個驗證碼是放到session中的,也可以自己指定。很靈活。

這里對Jpatchca做個備忘:

<dependency>
    <groupId>com.octo.captcha</groupId>
    <artifactId>jcaptcha</artifactId>
    <version>2.0-alpha-1</version>
</dependency>
<dependency>
    <groupId>com.octo.captcha</groupId>
    <artifactId>jcaptcha-integration-simple-servlet</artifactId>
    <version>2.0-alpha-1</version>
    <exclusions>
        <exclusion>
            <artifactId>servlet-api</artifactId>
            <groupId>javax.servlet</groupId>
        </exclusion>
    </exclusions>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>Lrtech_framework</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:framework/spring-front.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress XmlUnboundNsPrefix -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <mvc:annotation-driven/>
    <context:annotation-config/>
    <context:component-scan base-package="com.lavasoft.ntv.web"/>
    <mvc:resources mapping="/backui/**" location="/backui/"/>
    <mvc:resources mapping="/frontui/**" location="/frontui/"/>
    <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="true" />
        <property name="favorParameter" value="true" />
        <property name="parameterName" value="format" />
        <property name="ignoreAcceptHeader" value="true" /> 
        <property name="mediaTypes">
            <value>
                json=application/json
                xml=application/xml
                html=text/html
            </value>
        </property>
        <property name="defaultContentType" value="text/html" />
    </bean>
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="contentNegotiationManager" ref="contentNegotiationManager"/>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                    <property name="prettyPrint" value="true"/>
                    <property name="extractValueFromSingleKeyModel" value="true"/>
                </bean>
            </list>
        </property>
    </bean>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd"
       xmlns:context="http://www.springframework.org/schema/context">
    <import resource="classpath:/framework/spring-back.xml"/>
    <bean id="fastHashMapCaptchaStore" class="com.octo.captcha.service.captchastore.FastHashMapCaptchaStore"/>
    <bean id="captchaEngineEx" class="com.lavasoft.ntv.web.captchea.MyCaptchaEngine"/>
    <bean id="p_w_picpathCaptchaService" class="com.octo.captcha.service.p_w_picpath.DefaultManageableImageCaptchaService">
        <constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0">
            <ref bean="fastHashMapCaptchaStore"/>
        </constructor-arg>
        <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>
</beans>
package com.lavasoft.ntv.web.captchea;
import com.octo.captcha.component.p_w_picpath.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.p_w_picpath.backgroundgenerator.UniColorBackgroundGenerator;
import com.octo.captcha.component.p_w_picpath.color.RandomListColorGenerator;
import com.octo.captcha.component.p_w_picpath.deformation.ImageDeformation;
import com.octo.captcha.component.p_w_picpath.deformation.ImageDeformationByFilters;
import com.octo.captcha.component.p_w_picpath.fontgenerator.FontGenerator;
import com.octo.captcha.component.p_w_picpath.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.p_w_picpath.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.p_w_picpath.textpaster.TextPaster;
import com.octo.captcha.component.p_w_picpath.textpaster.textdecorator.TextDecorator;
import com.octo.captcha.component.p_w_picpath.wordtop_w_picpath.DeformedComposedWordToImage;
import com.octo.captcha.component.p_w_picpath.wordtop_w_picpath.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.p_w_picpath.ListImageCaptchaEngine;
import com.octo.captcha.p_w_picpath.gimpy.GimpyFactory;
import java.awt.*;
import java.awt.p_w_picpath.ImageFilter;
/**
 * 生成驗證碼引擎實現(xiàn)
 *
 * @author leizhimin 14-5-4 上午10:55
 */
public class MyCaptchaEngine extends ListImageCaptchaEngine {
    protected void buildInitialFactories() {
        int minWordLength = 4;
        int maxWordLength = 4;
        int fontSize = 16;
        int p_w_picpathWidth = 120;
        int p_w_picpathHeight = 28;
        WordGenerator wordGenerator = new RandomWordGenerator("23456789abcdefghjkmnpqrstuvwxyz");
        TextPaster randomPaster = new DecoratedRandomTextPaster(minWordLength,
                maxWordLength, new RandomListColorGenerator(new Color[]{
                new Color(23, 170, 27), new Color(220, 34, 11),
                new Color(23, 67, 172)}), new TextDecorator[]{});
        BackgroundGenerator background = new UniColorBackgroundGenerator(p_w_picpathWidth, p_w_picpathHeight, Color.white);
        FontGenerator font = new RandomFontGenerator(fontSize, fontSize,
                new Font[]{new Font("nyala", Font.BOLD, fontSize),
                        new Font("Bell MT", Font.PLAIN, fontSize),
                        new Font("Credit valley", Font.BOLD, fontSize)});
        ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[]{});
        ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[]{});
        ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[]{});
        WordToImage word2p_w_picpath = new DeformedComposedWordToImage(font,
                background, randomPaster, backDef, textDef, postDef);
        addFactory(new GimpyFactory(wordGenerator, word2p_w_picpath));
    }
}
package com.lavasoft.ntv.web;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.p_w_picpath.ImageCaptchaService;
import com.sun.p_w_picpath.codec.jpeg.JPEGCodec;
import com.sun.p_w_picpath.codec.jpeg.JPEGImageEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.p_w_picpath.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
 * 登錄邏輯
 *
 * @author leizhimin 14-5-4 上午11:00
 */
@Controller
public class LoginController {
    @Resource(name = "p_w_picpathCaptchaService")
    private ImageCaptchaService p_w_picpathCaptchaService;
    /**
     * 生成驗證碼圖片io流
     */
    @RequestMapping(value = "/generateImage")
    public void ImageCaptcha(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("-------------------  /generateImage");
        byte[] captchaChallengeAsJpeg = null;
        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
        try {
            String sessionid = request.getSession().getId();
            BufferedImage challenge = p_w_picpathCaptchaService.getImageChallengeForID(sessionid, request.getLocale());
            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
            jpegEncoder.encode(challenge);
        } catch (Exception e) {
            e.printStackTrace();
        }
        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("p_w_picpath/jpeg");
        ServletOutputStream responseOutputStream = response.getOutputStream();
        responseOutputStream.write(captchaChallengeAsJpeg);
        responseOutputStream.flush();
        responseOutputStream.close();
    }
    /**
     * 驗證驗證碼,并進行登錄
     */
    @RequestMapping("/login")
    private String LoginAction(HttpServletRequest request,
                               HttpServletResponse response,
                               String username,
                               String password,
                               String yzm) {
        boolean flag = false;
        String sessionid = request.getSession().getId();
        String captcha_value = request.getParameter("yzm");
        System.out.println("反饋的驗證碼:" + captcha_value);
        try {
            flag = p_w_picpathCaptchaService.validateResponseForID(sessionid, captcha_value);
        } catch (CaptchaServiceException e) {
        }
        System.out.println("驗證結果為:" + flag + ",sessionID=" + sessionid + ",驗證碼=" + captcha_value);
        if (flag) {
            //todo:驗證用戶名和密碼邏輯,
        }
        return flag ? "good" : "error";
    }
    @RequestMapping("/hello")
    private String hello() {
        System.out.println("-------hello!------");
        return "hello";
    }
    @RequestMapping("/mp")
    private ModelMap mp() {
        System.out.println("-------ModelMap!------");
        ModelMap map = new ModelMap();
        map.put("as", "sadfas");
        map.put("as1", "sadfas");
        map.put("as2", "sadfas");
        return map;
    }
    @RequestMapping("/mp2")
    private ModelAndView mp2() {
        System.out.println("-------ModelMap!------");
        ModelAndView mv = new ModelAndView();
        ModelMap map = mv.getModelMap();
        map.put("as1", "sadfas1");
        map.put("as2", "sadfas2");
        mv.setViewName("mp2");
        return mv;
    }
}
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link href="../backui/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css"/>
    <link href="../backui/ligerUI/skins/Tab/css/form.css" rel="stylesheet" type="text/css"/>
    <script src="../backui/jquery/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script src="../backui/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
    <script src="../backui/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
    <script src="../backui/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
    <script src="../backui/jquery-validation/messages_cn.js" type="text/javascript"></script>
    <style type="text/css">
        input.error {
            border: 1px dotted red;
        }
        label.error {
            background-p_w_picpath: url('../backui/jquery-validation/pic/error.png');
            background-repeat: no-repeat;
            padding-left: 18px;
            color: red;
        }
        .align-center1{
            margin:0 auto; /* 居中 這個是必須的,,其它的屬性非必須 */
            width: 400px;
            background-color: #9AC6FF;
            text-align:left; /* 文字等內(nèi)容居中 */
        }
    </style>
    <script>
        function chanage(srcObj){
            srcObj.src="/ntv/generateImage?"+Math.random();
        }
        $().ready(function () {
            $.metadata.setType("attr", "validate");
            $("#bt_login").                alert("bt_login");
            })
            $("#signupForm").validate();
        });
    </script>
</head>
<body>
<div class="align-center1">sdfasdf</div>
<div class="align-center1">
    <form id="signupForm" method="get" action="/ntv/login">
        <p>
            <label for="firstname">username</label>
            <input id="firstname" name="firstname" validate="{required:true}"/>
        <p>
        <p>
            <label for="password">password</label>
            <input id="password" name="password" type="password" validate="{required:true,minlength:1}"/>
        </p>
        <p>
            <label for="confirm_password">確認密碼</label>
            <input id="confirm_password" name="confirm_password" type="password"
                   validate="{equalTo:'#password'}"/>
        </p>
        <p>
            <label for="confirm_password">驗證碼</label>
            <input id="yzm" name="yzm" type="text"validate="{required:true"/>
            <img id="img_yzm" src="/ntv/generateImage"         </p>
        <p>
            <%--<input class="submit" type="submit" value="登錄"/>--%>
            <input type="button" id="bt_login" name="bt_login" value="登錄"/>
        </p>
    </form>
</div>
</body>
</html>

上述就是小編為大家分享的用Jpatchca和Spring MVC實現(xiàn)驗證碼的方法了,如果您也有類似的疑惑,不妨參照上述方法進行嘗試。如果想了解更多相關內(nèi)容,請關注億速云行業(yè)資訊。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI