source: branches/TaskRewrite/src/plugins/acegi-0.5.1/src/templates/manager/controllers/_CaptchaController.groovy @ 58

Last change on this file since 58 was 58, checked in by gav, 15 years ago

Configure BootStrap? with latest concepts.
Install and setup Acegi plugin with custom views.
Test Fixture plugin in a test app but couldn't get it to work with Acegi encodePassword() so gave up.

File size: 2.6 KB
Line 
1import java.awt.Color
2import java.awt.Font
3import java.awt.Graphics2D
4import java.awt.RenderingHints
5import java.awt.geom.Rectangle2D
6import java.awt.image.BufferedImage
7import javax.imageio.ImageIO
8
9class CaptchaController {
10
11        private static final String SOURCECHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
12
13        def index = {
14                response.setContentType('image/png')
15                response.setHeader('Cache-control', 'no-cache')
16
17                // Generate and remember the Source Character string (6 characters)
18                int l = SOURCECHARS.length()
19                StringBuilder b = new StringBuilder()
20                6.times {
21                    int r = (int)(Math.random() * l)
22                    b.append(SOURCECHARS.charAt(r))
23                }
24
25                final int height = 200
26                final int width = 200
27                final int space = 8
28
29                System.setProperty('java.awt.headless', 'true')
30                BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
31                Graphics2D g2d = bufferedImage.createGraphics()
32                Font font = new Font('Serif', Font.BOLD, 18)
33                g2d.setFont(font)
34                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
35                Rectangle2D fontRect = font.getStringBounds(b.toString(), g2d.getFontRenderContext())
36                // Now, create a graphic 'space' pixels wider and taller than the the font
37                bufferedImage = new BufferedImage((int)fontRect.getWidth() + space,
38                                (int)fontRect.getHeight() + space,
39                                BufferedImage.TYPE_INT_RGB)
40                g2d = bufferedImage.createGraphics()
41                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
42                g2d.setFont(font)
43
44                // Draw the background
45                g2d.setColor(Color.WHITE)
46                g2d.fillRect(0, 0, width, height)
47
48                // Draw the lines
49                g2d.setColor(Color.GRAY)
50                int x1
51                int y1
52                int x2
53                int y2
54                final int step = 10
55                x1 = 0
56                y1 = step
57                x2 = step
58                y2 = 0
59                while (x1 < width || x2 < width || y1 < height || y2 < height) {
60                    g2d.drawLine(x1, y1, x2, y2)
61                    if (y1 < height) {
62                                x1 = 0
63                                y1 += step
64                    }
65                    else if (x1 < width) {
66                                y1 = height
67                                x1 += step
68                    }
69                    else {
70                                x1 = width
71                                y1 = height
72                    }
73
74                    if (x2 < width) {
75                                y2 = 0
76                                x2 += step
77                    }
78                    else if (y2 < height) {
79                                x2 = width
80                                y2 += step
81                    }
82                    else {
83                                y2 = height
84                                x2 = width
85                    }
86                }
87
88                // Draw the String
89                g2d.setColor(Color.BLACK)
90
91                g2d.drawString(b.toString(), (int)(space/2), (int)(space/4) + (int)fontRect.getHeight())
92
93                OutputStream out = response.getOutputStream()
94                ImageIO.write(bufferedImage, 'PNG', out)
95                out.close()
96
97                session.setAttribute('captcha', b.toString())
98        }
99}
Note: See TracBrowser for help on using the repository browser.