Skip to content

Commit d8185bf

Browse files
committed
修改验证码渲染
1 parent bd21413 commit d8185bf

3 files changed

Lines changed: 62 additions & 37 deletions

File tree

src/main/java/org/unique/web/core/Controller.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ public Long getParaToLong() {
467467
return toLong(getPara(), null);
468468
}
469469

470+
public void render(final Render render) {
471+
this.render = render;
472+
}
473+
470474
public void render(final String view) {
471475
this.render = renderFactory.getRender(view);
472476
}

src/main/java/org/unique/plugin/patchca/PatchcaPlugin.java renamed to src/main/java/org/unique/web/render/impl/PatchcaRender.java

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.unique.plugin.patchca;
1+
package org.unique.web.render.impl;
22

33
import java.awt.Color;
44
import java.io.IOException;
@@ -19,22 +19,42 @@
1919
import org.patchca.utils.encoder.EncoderHelper;
2020
import org.patchca.word.RandomWordFactory;
2121
import 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+
}

src/test/java/com/demo/controller/front/index/IndexController.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.demo.controller.front.index;
22

3-
import java.io.IOException;
4-
53
import org.unique.common.tools.JSONUtil;
64
import org.unique.ioc.annotation.Autowired;
75
import org.unique.plugin.cache.Cache;
86
import org.unique.plugin.cache.JedisCache;
97
import org.unique.plugin.dao.Page;
10-
import org.unique.plugin.patchca.PatchcaPlugin;
118
import org.unique.web.annotation.Path;
129
import org.unique.web.core.Controller;
10+
import org.unique.web.render.impl.PatchcaRender;
1311

1412
import com.demo.model.User;
1513
import com.demo.service.UserService;
@@ -56,12 +54,7 @@ public void index() {
5654
}
5755

5856
public void code() {
59-
try {
60-
PatchcaPlugin.createCode(request, response);
61-
} catch (IOException e) {
62-
// TODO Auto-generated catch block
63-
e.printStackTrace();
64-
}
57+
this.render(new PatchcaRender());
6558
}
6659

6760
@org.unique.web.annotation.Action("ff/show/{mid}")

0 commit comments

Comments
 (0)