Cage与Spring的整合



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<String> 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<String> {
    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("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" %>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="../backui/jquery/jquery-1.5.2.min.js"></script>
    <script type="text/javascript">
        function change(obj) {
            obj.src = "/ntv/crimg?" + Math.random();
        }
        $(function () {
            $("#bt1").click(function () {
                $.post("/ntv/login", {yzm: $("#yzm").val()},
                    function (data) {
                        $("#msg").html(data);
                    }
                );
            })
        })
    </script>
</head>
<body>
验证码:<input id="yzm" type="text"/><br>
<img src="/ntv/crimg"  id="bt1" type="button" value="提交校验"/><br>
<div id="msg"></div>
</body>
</html>


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
验证通过!
</body>
</html>


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
验证码校验失败!
</body>
</html>


<?xml version="1.0" encoding="UTF-8"?>
<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" /> <!-- 忽略HttpHeader:Accept -->
        <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>
    <!--<import resource="classpath*:framework/spring-front.xml"/>-->
</beans>


Cage与Spring的整合

Cage与Spring的整合

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


本文出自 “熔 岩” 博客,转载请与作者联系!

Cage与Spring的整合,布布扣,bubuko.com

Cage与Spring的整合

上一篇:VM,VBOX中CentOS 7 配置网卡


下一篇:新款文章,绝无仅有!微信语音aud文件转换为mp3格式