Cage与Spring的整合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
  */
@Component
public  class  MyCage  extends  Cage {
     public  MyCage() {
     }
     @Autowired
     public  MyCage( @Qualifier ( "myTokenGenerator" ) IGenerator<String> myTokenGenerator) {
         super ( null null null null null , myTokenGenerator,  null );
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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());
         }
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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" ;
         }
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<%--
   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 >


1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html >
< head >
     < title ></ title >
</ head >
< body >
验证通过!
</ body >
</ html >


1
2
3
4
5
6
7
8
9
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
< html >
< head >
     < title ></ title >
</ head >
< body >
验证码校验失败!
</ body >
</ html >


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<? 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 >


wKioL1Nnm02xSB_TAACGJBAC9Ec999.jpg

wKiom1Nnm3iiDJj3AACpFrTbl68254.jpg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值