SprigMVC+kaptchaグラフィックス検証コード


基本的な準備
まぜん
pom.xmlに依存を導入すればいいです.
    <dependency>
        <groupId>com.github.axet</groupId>
        <artifactId>kaptcha</artifactId>
        <version>0.0.9</version>
    </dependency>
設定
appication Conteetにプロファイルを書き込みます.
<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> 
コード作成
バックエンド
Controllerが要求を処理して画像に戻ることを宣言します.同時に検証コードはSessionに存在し、後期検証に用いられる.
@Controller
@RequestMapping("/kaptcha")
public class CaptchaController {
        Producer captchaProducer = null;  
        @Autowired  
        public void setCaptchaProducer(Producer captchaProducer) {  
            this.captchaProducer = captchaProducer;  
        }  
        @RequestMapping("/doGet")  
        public ModelAndView doGet(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();  
            // 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();  
            }
                   System.out.println("Captchca:"+request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY));
            return null;  
        }  

}
先端
ここでは、検証コード画像がキャッシュされないように、要求後に乱数を追加する必要があります.
function refreshCaptcha(){
    var ran = Math.floor(Math.random() * 100)
    $('#captcha1').attr('src','/webRoot/kaptcha/doGet?' + ran);
}
認証コード
ユーザーが入力した文字列とSessionに保存されている認証コードを比較すればいいです.
@ResponseBody
    @RequestMapping(value="doLogin", method = RequestMethod.POST)
    public ResultInfo doLogin(HttpServletRequest request, HttpServletResponse response){
        ResultInfo result = new ResultInfo();
        String captcha = request.getParameter("captcha");
        if(!captcha.equals(request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY))){
            result.setStateId(-1);
            result.setErrorMsg("      ");
            return result;
        }
        /*       */
        return result;
    }