Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
solo-1
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-1
Commits
0602c7d8
Commit
0602c7d8
authored
Feb 13, 2018
by
Liang Ding
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
🚸
#12399
parent
3f6f7170
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
65 additions
and
97 deletions
+65
-97
pom.xml
pom.xml
+9
-1
src/main/java/org/b3log/solo/processor/CaptchaProcessor.java
src/main/java/org/b3log/solo/processor/CaptchaProcessor.java
+54
-87
src/main/java/org/b3log/solo/processor/CommentProcessor.java
src/main/java/org/b3log/solo/processor/CommentProcessor.java
+2
-9
src/main/resources/captcha_static.zip
src/main/resources/captcha_static.zip
+0
-0
src/main/resources/lib/net/pusuo/patchca-0.5.0.jar
src/main/resources/lib/net/pusuo/patchca-0.5.0.jar
+0
-0
No files found.
pom.xml
View file @
0602c7d8
<?xml version="1.0" encoding="UTF-8"?>
<!--
Description: Solo POM.
Version: 3.18.2.5
3, Feb 1
, 2018
Version: 3.18.2.5
4, Feb 13
, 2018
Author: <a href="http://88250.b3log.org">Liang Ding</a>
Author: <a href="http://www.annpeter.cn">Ann Peter</a>
Author: <a href="http://vanessa.b3log.org">Vanessa</a>
...
...
@@ -229,6 +229,14 @@
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.patchca
</groupId>
<artifactId>
patchca
</artifactId>
<version>
0.5.0
</version>
<scope>
system
</scope>
<systemPath>
${project.basedir}/src/main/resources/lib/net/pusuo/patchca-0.5.0.jar
</systemPath>
</dependency>
</dependencies>
<build>
...
...
src/main/java/org/b3log/solo/processor/CaptchaProcessor.java
View file @
0602c7d8
...
...
@@ -15,11 +15,8 @@
*/
package
org
.
b3log
.
solo
.
processor
;
import
org.apache.commons.io.IOUtils
;
import
org.b3log.latke.image.Image
;
import
org.b3log.latke.image.ImageService
;
import
org.b3log.latke.image.ImageServiceFactory
;
import
org.b3log.latke.logging.Level
;
import
org.b3log.latke.logging.Logger
;
import
org.b3log.latke.servlet.HTTPRequestContext
;
...
...
@@ -27,26 +24,26 @@ import org.b3log.latke.servlet.HTTPRequestMethod;
import
org.b3log.latke.servlet.annotation.RequestProcessing
;
import
org.b3log.latke.servlet.annotation.RequestProcessor
;
import
org.b3log.latke.servlet.renderer.PNGRenderer
;
import
org.b3log.solo.SoloServletListener
;
import
javax.servlet.http.HttpServletRequest
;
import
org.b3log.latke.util.Strings
;
import
org.patchca.color.SingleColorFactory
;
import
org.patchca.filter.predefined.CurvesRippleFilterFactory
;
import
org.patchca.service.Captcha
;
import
org.patchca.service.ConfigurableCaptchaService
;
import
org.patchca.word.RandomWordFactory
;
import
javax.imageio.ImageIO
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpSession
;
import
java.io.*
;
import
java.util.Enumeration
;
import
java.util.Random
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipFile
;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayOutputStream
;
import
java.util.HashSet
;
import
java.util.Set
;
/**
* Captcha processor.
* <p>
* Checkout <a href="http://toy-code.googlecode.com/svn/trunk/CaptchaGenerator"> the sample captcha generator</a> for more details.
* </p>
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version
1.1.0.5, Jul 6, 2017
* @version
2.0.0.0, Feb 13, 2018
* @since 0.3.1
*/
@RequestProcessor
...
...
@@ -57,11 +54,6 @@ public class CaptchaProcessor {
*/
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
CaptchaProcessor
.
class
);
/**
* Images service.
*/
private
static
final
ImageService
IMAGE_SERVICE
=
ImageServiceFactory
.
getImageService
();
/**
* Key of captcha.
*/
...
...
@@ -70,12 +62,12 @@ public class CaptchaProcessor {
/**
* Captchas.
*/
private
Image
[]
captchas
;
private
static
final
Set
<
String
>
CAPTCHAS
=
new
HashSet
<>()
;
/**
* C
ount of static captchas
.
* C
aptcha length
.
*/
private
static
final
int
CAPTCHA_
COUNT
=
100
;
private
static
final
int
CAPTCHA_
LENGTH
=
4
;
/**
* Gets captcha.
...
...
@@ -85,33 +77,40 @@ public class CaptchaProcessor {
@RequestProcessing
(
value
=
"/captcha.do"
,
method
=
HTTPRequestMethod
.
GET
)
public
void
get
(
final
HTTPRequestContext
context
)
{
final
PNGRenderer
renderer
=
new
PNGRenderer
();
context
.
setRenderer
(
renderer
);
if
(
null
==
captchas
)
{
loadCaptchas
();
}
try
{
final
HttpServletRequest
request
=
context
.
getRequest
();
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
);
if
(
null
!=
httpSession
)
{
LOGGER
.
log
(
Level
.
DEBUG
,
"Captcha[{0}] for session[id={1}]"
,
captcha
,
httpSession
.
getId
());
httpSession
.
setAttribute
(
CAPTCHA
,
captcha
);
final
ConfigurableCaptchaService
cs
=
new
ConfigurableCaptchaService
();
cs
.
setColorFactory
(
new
SingleColorFactory
(
new
Color
(
25
,
60
,
170
)));
cs
.
setFilterFactory
(
new
CurvesRippleFilterFactory
(
cs
.
getColorFactory
()));
final
RandomWordFactory
randomWordFactory
=
new
RandomWordFactory
();
randomWordFactory
.
setCharacters
(
"abcdefghijklmnprstuvwxy23456789"
);
randomWordFactory
.
setMinLength
(
CAPTCHA_LENGTH
);
randomWordFactory
.
setMaxLength
(
CAPTCHA_LENGTH
);
cs
.
setWordFactory
(
randomWordFactory
);
final
Captcha
captcha
=
cs
.
getCaptcha
();
final
String
challenge
=
captcha
.
getChallenge
();
final
BufferedImage
bufferedImage
=
captcha
.
getImage
();
if
(
CAPTCHAS
.
size
()
>
64
)
{
CAPTCHAS
.
clear
();
}
CAPTCHAS
.
add
(
challenge
);
final
HttpServletResponse
response
=
context
.
getResponse
();
response
.
setHeader
(
"Pragma"
,
"no-cache"
);
response
.
setHeader
(
"Cache-Control"
,
"no-cache"
);
response
.
setDateHeader
(
"Expires"
,
0
);
final
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
ImageIO
.
write
(
bufferedImage
,
"png"
,
baos
);
final
byte
[]
data
=
baos
.
toByteArray
();
IOUtils
.
closeQuietly
(
baos
);
final
Image
captchaImg
=
new
Image
();
captchaImg
.
setData
(
data
);
renderer
.
setImage
(
captchaImg
);
}
catch
(
final
Exception
e
)
{
LOGGER
.
log
(
Level
.
ERROR
,
e
.
getMessage
(),
e
);
...
...
@@ -119,53 +118,21 @@ public class CaptchaProcessor {
}
/**
* Loads captcha.
* Checks whether the specified captcha is invalid.
*
* @param captcha the specified captcha
* @return {@code true} if it is invalid, returns {@code false} otherwise
*/
private
synchronized
void
loadCaptchas
()
{
LOGGER
.
debug
(
"Loading captchas...."
);
try
{
captchas
=
new
Image
[
CAPTCHA_COUNT
];
final
InputStream
inputStream
=
SoloServletListener
.
class
.
getClassLoader
().
getResourceAsStream
(
"captcha_static.zip"
);
final
File
file
=
File
.
createTempFile
(
"b3log_captcha_static"
,
null
);
final
OutputStream
outputStream
=
new
FileOutputStream
(
file
);
IOUtils
.
copy
(
inputStream
,
outputStream
);
final
ZipFile
zipFile
=
new
ZipFile
(
file
);
IOUtils
.
closeQuietly
(
inputStream
);
IOUtils
.
closeQuietly
(
outputStream
);
final
Enumeration
<?
extends
ZipEntry
>
entries
=
zipFile
.
entries
();
int
i
=
0
;
while
(
entries
.
hasMoreElements
())
{
final
ZipEntry
entry
=
entries
.
nextElement
();
final
BufferedInputStream
bufferedInputStream
=
new
BufferedInputStream
(
zipFile
.
getInputStream
(
entry
));
final
byte
[]
captchaCharData
=
new
byte
[
bufferedInputStream
.
available
()];
bufferedInputStream
.
read
(
captchaCharData
);
bufferedInputStream
.
close
();
final
Image
image
=
IMAGE_SERVICE
.
makeImage
(
captchaCharData
);
image
.
setName
(
entry
.
getName
().
substring
(
0
,
entry
.
getName
().
lastIndexOf
(
'.'
)));
captchas
[
i
]
=
image
;
i
++;
}
zipFile
.
close
();
}
catch
(
final
Exception
e
)
{
LOGGER
.
error
(
"Can not load captchs!"
);
public
static
boolean
invalidCaptcha
(
final
String
captcha
)
{
if
(
Strings
.
isEmptyOrNull
(
captcha
)
||
captcha
.
length
()
!=
CAPTCHA_LENGTH
)
{
return
true
;
}
throw
new
IllegalStateException
(
e
);
boolean
ret
=
!
CaptchaProcessor
.
CAPTCHAS
.
contains
(
captcha
);
if
(!
ret
)
{
CaptchaProcessor
.
CAPTCHAS
.
remove
(
captcha
);
}
LOGGER
.
debug
(
"Loaded captch images"
)
;
return
ret
;
}
}
src/main/java/org/b3log/solo/processor/CommentProcessor.java
View file @
0602c7d8
...
...
@@ -149,13 +149,9 @@ public class CommentProcessor {
return
;
}
final
String
storedCaptcha
=
(
String
)
session
.
getAttribute
(
CaptchaProcessor
.
CAPTCHA
);
session
.
removeAttribute
(
CaptchaProcessor
.
CAPTCHA
);
if
(!
userQueryService
.
isLoggedIn
(
httpServletRequest
,
httpServletResponse
))
{
final
String
captcha
=
requestJSONObject
.
optString
(
CaptchaProcessor
.
CAPTCHA
);
if
(
null
==
storedCaptcha
||
!
storedCaptcha
.
equals
(
captcha
))
{
if
(
CaptchaProcessor
.
invalidCaptcha
(
captcha
))
{
jsonObject
.
put
(
Keys
.
STATUS_CODE
,
false
);
jsonObject
.
put
(
Keys
.
MSG
,
langPropsService
.
get
(
"captchaErrorLabel"
));
...
...
@@ -259,12 +255,9 @@ public class CommentProcessor {
return
;
}
final
String
storedCaptcha
=
(
String
)
session
.
getAttribute
(
CaptchaProcessor
.
CAPTCHA
);
session
.
removeAttribute
(
CaptchaProcessor
.
CAPTCHA
);
if
(!
userQueryService
.
isLoggedIn
(
httpServletRequest
,
httpServletResponse
))
{
final
String
captcha
=
requestJSONObject
.
optString
(
CaptchaProcessor
.
CAPTCHA
);
if
(
null
==
storedCaptcha
||
!
storedCaptcha
.
equals
(
captcha
))
{
if
(
CaptchaProcessor
.
invalidCaptcha
(
captcha
))
{
jsonObject
.
put
(
Keys
.
STATUS_CODE
,
false
);
jsonObject
.
put
(
Keys
.
MSG
,
langPropsService
.
get
(
"captchaErrorLabel"
));
...
...
src/main/resources/captcha_static.zip
deleted
100644 → 0
View file @
3f6f7170
File deleted
src/main/resources/lib/net/pusuo/patchca-0.5.0.jar
0 → 100644
View file @
0602c7d8
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