CZT
2023-08-26 306945ecc97e77659dce5f4562e2ae2ef1f90315
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.bstek.bdf2.core.controller;
 
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Properties;
 
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.lang.StringUtils;
 
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.apache.commons.lang.math.RandomUtils;
 
/**
 * @author Jacky.gao
 * @since 2013-2-21
 */
public class CaptchaController implements IController {
    private int defaultWidth = 165;
    private int defaultHeight = 57;
    private String defaultSessionKey = Constants.KAPTCHA_SESSION_KEY;
    private String url;
 
    public CaptchaController() {
 
    }
 
    public CaptchaController(String url) {
        this.url = url;
    }
 
    @Override
    public String getUrl() {
        return url;
    }
 
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setDateHeader("Expires", 0);
        // Set standard HTTP/1.1 no-cache headers.
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        // Set standard HTTP/1.0 no-cache header.
        response.setHeader("Pragma", "no-cache");
        // return a jpeg
        response.setContentType("image/jpeg");
 
        int width = defaultWidth;
        int height = defaultHeight;
        String key = defaultSessionKey;
        if (StringUtils.isNotEmpty(request.getParameter("key"))) {
            key = request.getParameter("key");
        }
        Producer captchaProducer = this.getProducer(width, height);
        String capText = captchaProducer.createText();
        request.getSession().setAttribute(key, capText);
        // create the image with the text
        BufferedImage bi = captchaProducer.createImage(capText);
        ServletOutputStream out = response.getOutputStream();
        try {
            // write the data out
            ImageIO.write(bi, "jpg", out);
            out.flush();
        } finally {
            out.close();
        }
    }
 
    @Override
    public boolean anonymousAccess() {
        return true;
    }
 
    private Producer getProducer(int width, int height) {
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Properties prop = new Properties();
        prop.put(Constants.KAPTCHA_IMAGE_WIDTH, String.valueOf(width));
        prop.put(Constants.KAPTCHA_IMAGE_HEIGHT, String.valueOf(height));
        prop.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
        prop.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "6");
 
//        prop.put(Constants.KAPTCHA_BACKGROUND_CLR_FROM, String.valueOf(RandomUtils.nextInt(255)) + ","
//                + RandomUtils.nextInt(255) + "," + RandomUtils.nextInt(255));
//
//        prop.put(Constants.KAPTCHA_BACKGROUND_CLR_TO, String.valueOf(RandomUtils.nextInt(255)) + ","
//                + RandomUtils.nextInt(255) + "," + RandomUtils.nextInt(255));
 
        prop.put(Constants.KAPTCHA_NOISE_COLOR, String.valueOf(RandomUtils.nextInt(255)) + ","
                + RandomUtils.nextInt(255) + "," + RandomUtils.nextInt(255));
 
        prop.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, String.valueOf(RandomUtils.nextInt(255)) + ","
                + RandomUtils.nextInt(255) + "," + RandomUtils.nextInt(255));
 
        prop.put(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, "1234567890");
 
        prop.put(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES,"Arial");
 
        Config config = new Config(prop);
        kaptcha.setConfig(config);
        return kaptcha;
    }
 
    @Override
    public boolean isDisabled() {
        return false;
    }
}