Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
solo
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
solo
Commits
692f8820
Commit
692f8820
authored
Sep 18, 2012
by
Liang Ding
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed #101
只生成了 100 个静态验证码。
parent
819c196d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
75 deletions
+36
-75
core/nb-configuration.xml
core/nb-configuration.xml
+6
-5
core/src/main/java/org/b3log/solo/processor/CaptchaProcessor.java
.../main/java/org/b3log/solo/processor/CaptchaProcessor.java
+30
-70
war/src/main/resources/captcha.zip
war/src/main/resources/captcha.zip
+0
-0
war/src/main/resources/captcha_static.zip
war/src/main/resources/captcha_static.zip
+0
-0
No files found.
core/nb-configuration.xml
View file @
692f8820
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<project-shared-configuration>
<!--
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
-->
<spellchecker-wordlist
xmlns=
"http://www.netbeans.org/ns/spellchecker-wordlist/1"
>
<spellchecker-wordlist
xmlns=
"http://www.netbeans.org/ns/spellchecker-wordlist/1"
>
<word>
Admin
</word>
<word>
Admin
</word>
...
@@ -14,6 +14,7 @@ Without this configuration present, some functionality in the IDE may be limited
...
@@ -14,6 +14,7 @@ Without this configuration present, some functionality in the IDE may be limited
<word>
blogging
</word>
<word>
blogging
</word>
<word>
captcha
</word>
<word>
captcha
</word>
<word>
Captcha
</word>
<word>
Captcha
</word>
<word>
Captchas
</word>
<word>
cron
</word>
<word>
cron
</word>
<word>
datastore
</word>
<word>
datastore
</word>
<word>
ftl
</word>
<word>
ftl
</word>
...
...
core/src/main/java/org/b3log/solo/processor/CaptchaProcessor.java
View file @
692f8820
...
@@ -17,16 +17,10 @@ package org.b3log.solo.processor;
...
@@ -17,16 +17,10 @@ package org.b3log.solo.processor;
import
java.io.BufferedInputStream
;
import
java.io.BufferedInputStream
;
import
java.net.URL
;
import
java.net.URL
;
import
java.util.ArrayList
;
import
java.util.Enumeration
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
javax.servlet.http.HttpSession
;
import
javax.servlet.http.HttpSession
;
import
org.b3log.latke.image.Image
;
import
org.b3log.latke.image.Image
;
import
java.util.Random
;
import
java.util.Random
;
import
java.util.Set
;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipEntry
;
...
@@ -46,14 +40,12 @@ import org.b3log.solo.SoloServletListener;
...
@@ -46,14 +40,12 @@ import org.b3log.solo.SoloServletListener;
* Captcha processor.
* Captcha processor.
*
*
* <p>
* <p>
* See <a href="http://isend-blog.appspot.com/2010/03/25/captcha_on_GAE.html">
* Checkout <a href="http://toy-code.googlecode.com/svn/trunk/CaptchaGenerator">
* 在GAE上拼接生成图形验证码</a> for philosophy. Checkout
* the sample captcha generator</a> for more details.
* <a href="http://toy-code.googlecode.com/svn/trunk/CaptchaGenerator">
* the sample captcha generator</a> for mor details.
* </p>
* </p>
*
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.1.0.
1, Oct 10, 2011
* @version 1.1.0.
2, Sep 18, 2012
* @since 0.3.1
* @since 0.3.1
*/
*/
@RequestProcessor
@RequestProcessor
...
@@ -63,43 +55,22 @@ public final class CaptchaProcessor {
...
@@ -63,43 +55,22 @@ public final class CaptchaProcessor {
* Logger.
* Logger.
*/
*/
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
CaptchaProcessor
.
class
.
getName
());
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
CaptchaProcessor
.
class
.
getName
());
/**
* Length of captcha.
*/
private
static
final
int
LENGTH
=
4
;
/**
/**
* Images service.
* Images service.
*/
*/
private
static
final
ImageService
IMAGE_SERVICE
=
ImageServiceFactory
.
getImageService
();
private
static
final
ImageService
IMAGE_SERVICE
=
ImageServiceFactory
.
getImageService
();
/**
* Random.
*/
private
static
final
Random
RANDOM
=
new
Random
();
/**
/**
* Key of captcha.
* Key of captcha.
*/
*/
public
static
final
String
CAPTCHA
=
"captcha"
;
public
static
final
String
CAPTCHA
=
"captcha"
;
/**
/**
* Maximum captcha row.
* Captchas.
*/
public
static
final
int
MAX_CAPTCHA_ROW
=
10
;
/**
* Maximum captcha column.
*/
public
static
final
int
MAX_CAPTCHA_COLUM
=
10
;
/**
* Width of a captcha character.
*/
public
static
final
int
WIDTH_CAPTCHA_CHAR
=
13
;
/**
* Height of a captcha character.
*/
*/
p
ublic
static
final
int
HEIGHT_CAPTCHA_CHAR
=
20
;
p
rivate
Image
[]
captchas
;
/**
/**
* Captcha <"imageName", Image>.
* Count of static captchas.
* For example <"0/5.png", Image>.
*/
*/
private
static
final
Map
<
String
,
Image
>
CAPTCHAS
=
new
HashMap
<
String
,
Image
>()
;
private
static
final
int
CAPTCHA_COUNT
=
100
;
/**
/**
* Gets captcha.
* Gets captcha.
...
@@ -111,28 +82,19 @@ public final class CaptchaProcessor {
...
@@ -111,28 +82,19 @@ public final class CaptchaProcessor {
final
PNGRenderer
renderer
=
new
PNGRenderer
();
final
PNGRenderer
renderer
=
new
PNGRenderer
();
context
.
setRenderer
(
renderer
);
context
.
setRenderer
(
renderer
);
if
(
CAPTCHAS
.
isEmpty
()
)
{
if
(
null
==
captchas
)
{
loadCaptchas
();
loadCaptchas
();
}
}
try
{
try
{
final
String
row
=
String
.
valueOf
(
RANDOM
.
nextInt
(
MAX_CAPTCHA_ROW
));
String
captcha
=
""
;
final
List
<
Image
>
images
=
new
ArrayList
<
Image
>();
for
(
int
i
=
0
;
i
<
LENGTH
;
i
++)
{
final
String
column
=
String
.
valueOf
(
RANDOM
.
nextInt
(
MAX_CAPTCHA_COLUM
));
captcha
+=
column
;
final
String
imageName
=
row
+
"/"
+
column
+
".png"
;
final
Image
captchaChar
=
CAPTCHAS
.
get
(
imageName
);
images
.
add
(
captchaChar
);
}
final
Image
captchaImage
=
IMAGE_SERVICE
.
makeImage
(
images
);
final
HttpServletRequest
request
=
context
.
getRequest
();
final
HttpServletRequest
request
=
context
.
getRequest
();
final
HttpServletResponse
response
=
context
.
getResponse
();
final
HttpServletResponse
response
=
context
.
getResponse
();
final
Random
random
=
new
Random
();
final
int
index
=
random
.
nextInt
(
CAPTCHA_COUNT
);
final
Image
captchaImg
=
captchas
[
index
];
final
String
captcha
=
captchaImg
.
getName
();
final
HttpSession
httpSession
=
request
.
getSession
(
false
);
final
HttpSession
httpSession
=
request
.
getSession
(
false
);
if
(
null
!=
httpSession
)
{
if
(
null
!=
httpSession
)
{
LOGGER
.
log
(
Level
.
FINER
,
"Captcha[{0}] for session[id={1}]"
,
new
Object
[]{
captcha
,
httpSession
.
getId
()});
LOGGER
.
log
(
Level
.
FINER
,
"Captcha[{0}] for session[id={1}]"
,
new
Object
[]{
captcha
,
httpSession
.
getId
()});
...
@@ -143,7 +105,7 @@ public final class CaptchaProcessor {
...
@@ -143,7 +105,7 @@ public final class CaptchaProcessor {
response
.
setHeader
(
"Cache-Control"
,
"no-cache"
);
response
.
setHeader
(
"Cache-Control"
,
"no-cache"
);
response
.
setDateHeader
(
"Expires"
,
0
);
response
.
setDateHeader
(
"Expires"
,
0
);
renderer
.
setImage
(
captchaIm
age
);
renderer
.
setImage
(
captchaIm
g
);
}
catch
(
final
Exception
e
)
{
}
catch
(
final
Exception
e
)
{
LOGGER
.
log
(
Level
.
SEVERE
,
e
.
getMessage
(),
e
);
LOGGER
.
log
(
Level
.
SEVERE
,
e
.
getMessage
(),
e
);
}
}
...
@@ -152,34 +114,32 @@ public final class CaptchaProcessor {
...
@@ -152,34 +114,32 @@ public final class CaptchaProcessor {
/**
/**
* Loads captcha.
* Loads captcha.
*/
*/
private
s
tatic
void
loadCaptchas
()
{
private
s
ynchronized
void
loadCaptchas
()
{
LOGGER
.
info
(
"Loading captchas...."
);
LOGGER
.
info
(
"Loading captchas...."
);
try
{
try
{
final
URL
captchaURL
=
SoloServletListener
.
class
.
getClassLoader
().
getResource
(
"captcha.zip"
);
captchas
=
new
Image
[
CAPTCHA_COUNT
];
final
ZipFile
zipFile
=
new
ZipFile
(
captchaURL
.
getFile
());
final
Set
<
String
>
imageNames
=
new
HashSet
<
String
>();
for
(
int
row
=
0
;
row
<
MAX_CAPTCHA_ROW
;
row
++)
{
for
(
int
column
=
0
;
column
<
MAX_CAPTCHA_COLUM
;
column
++)
{
imageNames
.
add
(
row
+
"/"
+
column
+
".png"
);
}
}
final
URL
captchaURL
=
SoloServletListener
.
class
.
getClassLoader
().
getResource
(
"captcha_static.zip"
);
final
ZipFile
zipFile
=
new
ZipFile
(
captchaURL
.
getFile
());
final
ImageService
imageService
=
ImageServiceFactory
.
getImageService
();
final
Enumeration
<?
extends
ZipEntry
>
entries
=
zipFile
.
entries
();
final
Iterator
<
String
>
i
=
imageNames
.
iterator
();
int
i
=
0
;
while
(
i
.
hasNext
())
{
while
(
entries
.
hasMoreElements
())
{
final
String
imageName
=
i
.
next
();
final
ZipEntry
entry
=
entries
.
nextElement
();
final
ZipEntry
zipEntry
=
zipFile
.
getEntry
(
imageName
);
final
BufferedInputStream
bufferedInputStream
=
new
BufferedInputStream
(
zipFile
.
getInputStream
(
zipE
ntry
));
final
BufferedInputStream
bufferedInputStream
=
new
BufferedInputStream
(
zipFile
.
getInputStream
(
e
ntry
));
final
byte
[]
captchaCharData
=
new
byte
[
bufferedInputStream
.
available
()];
final
byte
[]
captchaCharData
=
new
byte
[
bufferedInputStream
.
available
()];
bufferedInputStream
.
read
(
captchaCharData
);
bufferedInputStream
.
read
(
captchaCharData
);
bufferedInputStream
.
close
();
bufferedInputStream
.
close
();
final
Image
captchaChar
=
imageService
.
makeImage
(
captchaCharData
);
final
Image
image
=
IMAGE_SERVICE
.
makeImage
(
captchaCharData
);
image
.
setName
(
entry
.
getName
().
substring
(
0
,
entry
.
getName
().
lastIndexOf
(
'.'
)));
captchas
[
i
]
=
image
;
CAPTCHAS
.
put
(
imageName
,
captchaChar
)
;
i
++
;
}
}
zipFile
.
close
();
zipFile
.
close
();
...
...
war/src/main/resources/captcha.zip
deleted
100644 → 0
View file @
819c196d
File deleted
war/src/main/resources/captcha_static.zip
0 → 100644
View file @
692f8820
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment