package com.lavasoft.ntv.web.common;
import com.github.cage.Cage;
import com.github.cage.IGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
 * Created by Administrator on 14-5-5.
 *
 * @author leizhimin 14-5-5 下午7:00
 */
@Component
public class MyCage extends Cage {
    public MyCage() {
    }
    @Autowired
    public MyCage(@Qualifier("myTokenGenerator") IGenerator myTokenGenerator) {
        super(null, null, null, null, null, myTokenGenerator, null);
    }
}


package com.lavasoft.ntv.web.common;
import com.github.cage.IGenerator;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
 * 验证码生成器
 *
 * @author leizhimin 14-5-5 下午2:42
 */
@Component("myTokenGenerator")
public class MyTokenGenerator implements IGenerator {
    private int length = 4;
    private String charsetdir = "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ";
    private static final Random r = new Random();
    public MyTokenGenerator() {
    }
    public MyTokenGenerator(int length, String charsetdir) {
        this.length = length;
        this.charsetdir = charsetdir;
    }
    @Override
    public String next() {
        StringBuffer sb = new StringBuffer();
        int len = charsetdir.length();
        for (int i = 0; i < length; i++) {
            sb.append(charsetdir.charAt(r.nextInt(len - 1)));
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        MyTokenGenerator t = new MyTokenGenerator();
        for (int i = 0; i < 100; i++) {
            System.out.println(t.next());
        }
    }
}


package com.lavasoft.ntv.web;
import com.lavasoft.ntv.web.common.MyCage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
 * Created by Administrator on 14-5-4.
 *
 * @author leizhimin 14-5-4 上午11:00
 */
@Controller
public class LoginController {
    @Resource
    private MyCage myCage;
    @RequestMapping("/crimg")
    private void createValidateImg(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        if (session == null) {
            session=request.getSession();
        }
        String token = myCage.getTokenGenerator().next();
        System.out.println("当前的SessionID=" + session.getId() + ",验证码=" + token);
        session.setAttribute("captchaToken", token);
        setResponseHeaders(response);
        myCage.draw(token, response.getOutputStream());
    }
    protected void setResponseHeaders(HttpServletResponse response) {
        response.setContentType("p_w_picpath/" + myCage.getFormat());
        response.setHeader("Cache-Control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        long time = System.currentTimeMillis();
        response.setDateHeader("Last-Modified", time);
        response.setDateHeader("Date", time);
        response.setDateHeader("Expires", time);
    }
    @RequestMapping("/login")
    private String LoginAction(HttpServletRequest request,
                               HttpServletResponse response,
                               String username,
                               String password,
                               @RequestParam(value = "yzm") String yzm) {
        boolean flag = false;
        HttpSession session = request.getSession(false);
        String token = (String) session.getAttribute("captchaToken");
        System.out.println("当前的SessionID=" + session.getId() + ",Session中的验证码=" + session.getAttribute("captchaToken") + ",输入的验证码:" + yzm);
        if(token!=null && token.equalsIgnoreCase(yzm)){
            session.removeAttribute("captchaToken");
            return "success";
        }else{
            return "error";
        }
    }
}


<%--
  Created by IntelliJ IDEA.
  User: leizhimin 14-5-5 下午4:57
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    
    
    


验证码:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    


验证通过!


<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    


验证码校验失败!




    
    
    
    
    
    
        
        
        
         
        
            
                json=application/json
                xml=application/xml
                html=text/html
            
        
        
    
    
        
        
        
            
                
                    
                    
                
            
        
    
    
        
        
        
        
    
    


Cage与Spring的整合_第1张图片

Cage与Spring的整合_第2张图片

2014-05-05 22:00:24 JRebel: Reloading class 'com.lavasoft.ntv.web.LoginController'.
2014-05-05 22:03:09 JRebel: Reconfiguring bean 'myCage' [com.lavasoft.ntv.web.common.MyCage]
2014-05-05 22:03:09 JRebel: Reconfiguring bean 'loginController' [com.lavasoft.ntv.web.LoginController]
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=KRDE
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=S5zM
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=PgUi
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=H8v2
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=tWy4
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=Hham
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=ILd5
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=KBgY
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,Session中的验证码=KBgY,输入的验证码:kbgy
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,Session中的验证码=null,输入的验证码:kbgy
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=B4t9
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=PTD9
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=Xc4U
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=sKF6
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=cEbh
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=bFQx
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=vwpW
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=MeVE
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,验证码=A9Xz
当前的SessionID=5B94D1862193DD6C309BB200D0D9E03F,Session中的验证码=A9Xz,输入的验证码:13213