1- package org .unique .plugin . patchca ;
1+ package org .unique .web . render . impl ;
22
33import java .awt .Color ;
44import java .io .IOException ;
1919import org .patchca .utils .encoder .EncoderHelper ;
2020import org .patchca .word .RandomWordFactory ;
2121import org .unique .web .core .Const ;
22+ import org .unique .web .render .Render ;
2223
2324/**
2425 * patchca生成多彩验证码
25- *
26- * @author rex
26+ * @author:rex
27+ * @date:2014年10月16日
28+ * @version:1.0
2729 */
28- public class PatchcaPlugin {
30+ public class PatchcaRender implements Render {
31+
32+ private static Logger logger = Logger .getLogger (PatchcaRender .class );
33+
34+ private static final String RANDOM_CHAR = "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ" ;
35+ private static int MIN_LEN = 4 ;
36+ private static int MAX_LEN = 4 ;
37+
38+ private ConfigurableCaptchaService cs = new ConfigurableCaptchaService ();
2939
30- private static Logger logger = Logger .getLogger (PatchcaPlugin .class );
31-
32- private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService ();
33-
34- private static Random random = new Random ();
35-
36- static {
37- cs .setColorFactory (new ColorFactory () {
40+ private Random random = new Random ();
41+
42+ public PatchcaRender () {
43+ init (MIN_LEN , MAX_LEN );
44+ }
45+
46+ public PatchcaRender (int len ) {
47+ init (len , len );
48+ }
49+
50+ public PatchcaRender (int min , int max ) {
51+ init (min , max );
52+ }
53+
54+ private void init (int min , int max ){
55+ MIN_LEN = min ;
56+ MAX_LEN = max ;
57+ cs .setColorFactory (new ColorFactory () {
3858 @ Override
3959 public Color getColor (int x ) {
4060 int [] c = new int [3 ];
@@ -50,13 +70,13 @@ public Color getColor(int x) {
5070 }
5171 });
5272 RandomWordFactory wf = new RandomWordFactory ();
53- wf .setCharacters ("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ" );
54- wf .setMaxLength (4 );
55- wf .setMinLength (4 );
73+ wf .setCharacters (RANDOM_CHAR );
74+ wf .setMaxLength (MIN_LEN );
75+ wf .setMinLength (MAX_LEN );
5676 cs .setWordFactory (wf );
57- }
58-
59- public static String createCode (HttpServletRequest request , HttpServletResponse response ) throws IOException {
77+ }
78+
79+ private String createCode (HttpServletRequest request , HttpServletResponse response ) throws IOException {
6080 switch (random .nextInt (5 )) {
6181 case 0 :
6282 cs .setFilterFactory (new CurvesRippleFilterFactory (cs .getColorFactory ()));
@@ -79,14 +99,6 @@ public static String createCode(HttpServletRequest request, HttpServletResponse
7999 session = request .getSession ();
80100 }
81101
82- response .setContentType ("image/png" );
83- response .setHeader ("Cache-Control" , "no-cache, no-store" );
84- response .setHeader ("Pragma" , "no-cache" );
85- long time = System .currentTimeMillis ();
86- response .setDateHeader ("Last-Modified" , time );
87- response .setDateHeader ("Date" , time );
88- response .setDateHeader ("Expires" , time );
89-
90102 String token = null ;
91103 token = EncoderHelper .getChallangeAndWriteImage (cs , "png" , response .getOutputStream ());
92104 if (null != token ){
@@ -95,5 +107,21 @@ public static String createCode(HttpServletRequest request, HttpServletResponse
95107 logger .debug ("当前的SessionID=" + session .getId () + ",验证码=" + token );
96108 return token ;
97109 }
98-
99- }
110+
111+ @ Override
112+ public void render (HttpServletRequest request , HttpServletResponse response , String viewPath ) {
113+ try {
114+ response .setContentType ("image/png" );
115+ response .setHeader ("Cache-Control" , "no-cache, no-store" );
116+ response .setHeader ("Pragma" , "no-cache" );
117+ long time = System .currentTimeMillis ();
118+ response .setDateHeader ("Last-Modified" , time );
119+ response .setDateHeader ("Date" , time );
120+ response .setDateHeader ("Expires" , time );
121+ this .createCode (request , response );
122+ } catch (IOException e ) {
123+ e .printStackTrace ();
124+ }
125+ }
126+
127+ }
0 commit comments