博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cage与Spring的整合
阅读量:6254 次
发布时间:2019-06-22

本文共 6568 字,大约阅读时间需要 21 分钟。

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 */@Componentpublic 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 */@Controllerpublic 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("image/" + 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

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

转载地址:http://cvjsa.baihongyu.com/

你可能感兴趣的文章
[转载] 程序员必看:请不要做浮躁的人 24法则
查看>>
JavaWeb_JavaEE_命名规则
查看>>
[HDU 1317]XYZZY[SPFA变形][最长路]
查看>>
程序员面试题:问谁养蛇?
查看>>
js面向对象初步探究(上) js面向对象的5种写方法
查看>>
Create the Data Access Layer
查看>>
Android必知必会-Android Studio修改包名
查看>>
bootstrap -- 一个标签中,同时有 col-xs , col-sm , col-md , col-lg
查看>>
IEEE754标准的浮点数存储格式
查看>>
Babel插件开发入门指南
查看>>
浅谈iOS 自动调节文本高度
查看>>
oracle易忘函数用法(2)
查看>>
总结系列_14(OpenCV2.4.3的新特征以及安装方法)
查看>>
虚拟地址空间分配
查看>>
提高你的Java代码质量吧:使用构造函数协助描述枚举项
查看>>
Struts2 学习笔记20 类型转换part2 写自己的转换器
查看>>
mybatis+spring配置
查看>>
Windows下将程序打包为安装包(最为简易的方式)
查看>>
这个月干啥去了?——H5+移动应用实战开发
查看>>
GMT时间转换为当地时间的方法
查看>>