Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
P
pica-cloud-account
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
1
合并请求
1
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
Close sidebar
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
com.pica.cloud.account
pica-cloud-account
提交
afba8405
提交
afba8405
编写于
8月 09, 2019
作者:
dong.an
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
图形验证码
上级
13fb0826
流水线
#13065
已失败 于阶段
变更
15
流水线
1
隐藏空白字符变更
内嵌
并排
正在显示
15 个修改的文件
包含
122 行增加
和
48 行删除
+122
-48
pom.xml
server/pom.xml
+6
-0
CaptchaController.java
.../account/account/server/controller/CaptchaController.java
+0
-2
CaptchaServiceImpl.java
...count/account/server/service/impl/CaptchaServiceImpl.java
+5
-7
MemcachedClientWrapper.java
...d/account/account/server/util/MemcachedClientWrapper.java
+40
-0
CaptchaGenerator.java
...account/account/server/util/captcha/CaptchaGenerator.java
+25
-6
CaptchaUtil.java
...loud/account/account/server/util/captcha/CaptchaUtil.java
+11
-5
Generator.java
.../cloud/account/account/server/util/captcha/Generator.java
+20
-0
NumberLetterGenerator.java
...nt/account/server/util/captcha/NumberLetterGenerator.java
+0
-23
1.png
server/src/main/resources/1.png
+0
-0
2.png
server/src/main/resources/2.png
+0
-0
bootstrap-dev.properties
server/src/main/resources/bootstrap-dev.properties
+3
-1
bootstrap-prod.properties
server/src/main/resources/bootstrap-prod.properties
+3
-1
bootstrap-test1.properties
server/src/main/resources/bootstrap-test1.properties
+3
-1
bootstrap-test2.properties
server/src/main/resources/bootstrap-test2.properties
+3
-1
bootstrap-uat.properties
server/src/main/resources/bootstrap-uat.properties
+3
-1
未找到文件。
server/pom.xml
浏览文件 @
afba8405
...
...
@@ -113,6 +113,12 @@
<artifactId>
pica-cloud-account-common
</artifactId>
<version>
1.0.0
</version>
</dependency>
<dependency>
<groupId>
net.spy
</groupId>
<artifactId>
spymemcached
</artifactId>
<version>
2.12.3
</version>
</dependency>
</dependencies>
<dependencyManagement>
...
...
server/src/main/java/com/pica/cloud/account/account/server/controller/CaptchaController.java
浏览文件 @
afba8405
...
...
@@ -34,7 +34,6 @@ public class CaptchaController {
return
PicaResponse
.
toResponse
(
captchaToken
);
}
/*
@ApiOperation
(
"校验图形验证码"
)
@GetMapping
(
"/acknowledge"
)
public
PicaResponse
<
Boolean
>
acknowledge
(
@ApiParam
(
"token"
)
@RequestParam
(
"token"
)
String
token
,
...
...
@@ -42,6 +41,5 @@ public class CaptchaController {
boolean
valid
=
captchaService
.
acknowledge
(
token
,
answer
);
return
PicaResponse
.
toResponse
(
valid
);
}
*/
}
server/src/main/java/com/pica/cloud/account/account/server/service/impl/CaptchaServiceImpl.java
浏览文件 @
afba8405
package
com
.
pica
.
cloud
.
account
.
account
.
server
.
service
.
impl
;
import
com.pica.cloud.account.account.server.service.CaptchaService
;
import
com.pica.cloud.account.account.server.util.MemcachedClientWrapper
;
import
com.pica.cloud.account.account.server.util.captcha.CaptchaGenerator
;
import
com.pica.cloud.account.account.server.util.captcha.CaptchaToken
;
import
com.pica.cloud.foundation.redis.ICacheClient
;
...
...
@@ -18,14 +19,11 @@ import java.util.UUID;
public
class
CaptchaServiceImpl
implements
CaptchaService
{
private
final
int
size
=
5
;
private
final
int
EXPIRE
=
5
*
60
;
// 5 minutes
private
final
String
CAPTCHA_PREFIX
=
"captcha-"
;
@Autowired
private
CaptchaGenerator
agent
;
@Autowired
@Qualifier
(
"cacheMigrateClient"
)
private
ICacheClient
redisClient
;
MemcachedClientWrapper
cache
;
@Override
public
CaptchaToken
generateToken
(
int
width
,
int
height
)
{
String
origin
=
agent
.
generateChars
(
size
);
...
...
@@ -34,14 +32,14 @@ public class CaptchaServiceImpl implements CaptchaService {
ret
.
setBuf
(
buf
);
ret
.
setOrigin
(
origin
);
ret
.
setToken
(
DigestUtils
.
md5Hex
(
UUID
.
randomUUID
().
toString
()));
redisClient
.
set
(
CAPTCHA_PREFIX
+
ret
.
getToken
(),
origin
,
EXPIRE
);
cache
.
set
(
CAPTCHA_PREFIX
+
ret
.
getToken
(),
origin
);
return
ret
;
}
@Override
public
boolean
acknowledge
(
String
token
,
String
answer
)
{
String
origin
=
redisClient
.
get
(
CAPTCHA_PREFIX
+
token
);
redisClient
.
del
(
CAPTCHA_PREFIX
+
token
);
String
origin
=
cache
.
get
(
CAPTCHA_PREFIX
+
token
);
cache
.
remove
(
CAPTCHA_PREFIX
+
token
);
if
(
origin
!=
null
&&
answer
!=
null
)
{
return
origin
.
compareToIgnoreCase
(
answer
)
==
0
;
}
...
...
server/src/main/java/com/pica/cloud/account/account/server/util/MemcachedClientWrapper.java
0 → 100644
浏览文件 @
afba8405
package
com
.
pica
.
cloud
.
account
.
account
.
server
.
util
;
import
net.spy.memcached.AddrUtil
;
import
net.spy.memcached.ConnectionFactoryBuilder
;
import
net.spy.memcached.MemcachedClient
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
java.io.IOException
;
/**
* @author Laurence Cao
*
*/
@Component
public
class
MemcachedClientWrapper
{
private
final
int
EXPIRE
=
5
*
60
;
// 5 minutes
private
MemcachedClient
cli
;
public
MemcachedClientWrapper
(
@Value
(
"${memcached.url}"
)
String
url
)
{
try
{
cli
=
new
MemcachedClient
(
new
ConnectionFactoryBuilder
().
setProtocol
(
ConnectionFactoryBuilder
.
Protocol
.
BINARY
).
build
(),
AddrUtil
.
getAddresses
(
url
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
String
get
(
String
token
)
{
return
(
String
)
cli
.
get
(
token
);
}
public
void
set
(
String
token
,
String
answer
)
{
cli
.
set
(
token
,
EXPIRE
,
answer
);
}
public
void
remove
(
String
token
)
{
cli
.
delete
(
token
);
}
}
server/src/main/java/com/pica/cloud/account/account/server/util/captcha/CaptchaGenerator.java
浏览文件 @
afba8405
...
...
@@ -3,24 +3,38 @@ package com.pica.cloud.account.account.server.util.captcha;
import
com.google.common.collect.Range
;
import
com.google.common.collect.RangeMap
;
import
com.google.common.collect.TreeRangeMap
;
import
org.springframework.stereotype.Component
;
import
java.awt.*
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.ThreadLocalRandom
;
/**
* @author Laurence Cao
*
*/
public
abstract
class
CaptchaGenerator
{
@Component
public
class
CaptchaGenerator
{
protected
RangeMap
<
Integer
,
CaptchaContext
>
ctxs
=
TreeRangeMap
.
create
();
protected
List
<
Generator
>
gens
=
new
ArrayList
();
public
CaptchaGenerator
(
boolean
showGrid
)
throws
FontFormatException
,
IOException
{
class
NumberLetterGenerator
implements
Generator
{
@Override
public
String
generateChars
(
int
size
)
{
return
CaptchaUtil
.
generateUUIDText
(
size
);
}
}
public
CaptchaGenerator
()
throws
FontFormatException
,
IOException
{
int
h
=
40
;
ctxs
.
put
(
Range
.
closed
(
0
,
h
),
new
CaptchaContext
(
h
*
4
,
h
,
h
/
5
*
4
,
showGrid
));
ctxs
.
put
(
Range
.
closed
Open
(
0
,
h
),
new
CaptchaContext
(
h
*
4
,
h
,
h
/
5
*
4
,
true
));
h
*=
2
;
ctxs
.
put
(
Range
.
closed
(
h
/
2
,
h
),
new
CaptchaContext
(
h
*
4
,
h
,
h
/
5
*
4
,
showGrid
));
ctxs
.
put
(
Range
.
closed
Open
(
h
/
2
,
h
),
new
CaptchaContext
(
h
*
4
,
h
,
h
/
5
*
4
,
true
));
h
*=
2
;
ctxs
.
put
(
Range
.
closed
(
h
/
2
,
Integer
.
MAX_VALUE
),
new
CaptchaContext
(
h
*
4
,
h
,
h
/
5
*
4
,
showGrid
));
ctxs
.
put
(
Range
.
closedOpen
(
h
/
2
,
Integer
.
MAX_VALUE
),
new
CaptchaContext
(
h
*
4
,
h
,
h
/
5
*
4
,
true
));
gens
.
add
(
new
NumberLetterGenerator
());
}
@SuppressWarnings
(
"unused"
)
...
...
@@ -32,5 +46,10 @@ public abstract class CaptchaGenerator {
return
CaptchaUtil
.
generateImage
(
text
,
ctx
);
}
public
abstract
String
generateChars
(
int
size
);
public
String
generateChars
(
int
size
)
{
int
idx
=
ThreadLocalRandom
.
current
().
nextInt
(
gens
.
size
());
Generator
gen
=
gens
.
get
(
idx
);
return
gen
.
generateChars
(
size
);
}
}
server/src/main/java/com/pica/cloud/account/account/server/util/captcha/CaptchaUtil.java
浏览文件 @
afba8405
...
...
@@ -7,7 +7,10 @@ import javax.imageio.ImageIO;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.net.URL
;
import
java.util.UUID
;
import
java.util.concurrent.ThreadLocalRandom
;
...
...
@@ -40,22 +43,25 @@ public class CaptchaUtil {
return
ret
;
}
public
static
BufferedImage
createCaptcha
(
String
src
,
CaptchaContext
ctx
)
{
public
static
BufferedImage
createCaptcha
(
String
src
,
CaptchaContext
ctx
)
throws
IOException
{
char
[]
text
=
src
==
null
?
new
char
[
0
]
:
src
.
toCharArray
();
if
(
text
==
null
||
text
.
length
==
0
)
{
throw
new
IllegalArgumentException
(
"No captcha text given"
);
}
BufferedImage
image
=
new
BufferedImage
(
ctx
.
width
,
ctx
.
height
,
BufferedImage
.
TYPE_INT_RGB
);
//BufferedImage image = new BufferedImage(ctx.width, ctx.height, BufferedImage.TYPE_INT_RGB);
String
name
=
(
ThreadLocalRandom
.
current
().
nextInt
(
2
)
+
1
)
+
".png"
;
URL
url
=
CaptchaUtil
.
class
.
getClassLoader
().
getResource
(
name
);
BufferedImage
image
=
ImageIO
.
read
(
url
);
Graphics2D
g2d
=
image
.
createGraphics
();
g2d
.
setRenderingHint
(
RenderingHints
.
KEY_ANTIALIASING
,
RenderingHints
.
VALUE_ANTIALIAS_ON
);
g2d
.
setBackground
(
Color
.
WHITE
);
//
g2d.setBackground(Color.WHITE);
g2d
.
setColor
(
Color
.
BLACK
);
clearCanvas
(
g2d
,
ctx
);
//
clearCanvas(g2d, ctx);
if
(
ctx
.
showGrid
)
{
drawGrid
(
g2d
,
ctx
);
//
drawGrid(g2d, ctx);
}
int
charMaxWidth
=
ctx
.
width
/
text
.
length
;
...
...
server/src/main/java/com/pica/cloud/account/account/server/util/captcha/Generator.java
0 → 100644
浏览文件 @
afba8405
/**
*
*/
package
com
.
pica
.
cloud
.
account
.
account
.
server
.
util
.
captcha
;
/**
* @author Laurence Cao
*
*/
public
interface
Generator
{
/**
* generate string by fixed size
*
* @param size
* @return
*/
String
generateChars
(
int
size
);
}
server/src/main/java/com/pica/cloud/account/account/server/util/captcha/NumberLetterGenerator.java
已删除
100644 → 0
浏览文件 @
13fb0826
package
com
.
pica
.
cloud
.
account
.
account
.
server
.
util
.
captcha
;
import
org.springframework.stereotype.Component
;
import
java.awt.*
;
import
java.io.IOException
;
/**
* @author Laurence Cao
*
*/
@Component
public
class
NumberLetterGenerator
extends
CaptchaGenerator
{
public
NumberLetterGenerator
()
throws
FontFormatException
,
IOException
{
super
(
true
);
}
@Override
public
String
generateChars
(
int
size
)
{
return
CaptchaUtil
.
generateUUIDText
(
size
);
}
}
server/src/main/resources/1.png
0 → 100644
浏览文件 @
afba8405
147.7 KB
server/src/main/resources/2.png
0 → 100644
浏览文件 @
afba8405
91.2 KB
server/src/main/resources/bootstrap-dev.properties
浏览文件 @
afba8405
...
...
@@ -32,4 +32,6 @@ ribbon.ConnectTimeout=60000
#------------ Please don't change above configurations ------------
spring.jackson.date-format
=
yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone
=
GMT+8
\ No newline at end of file
spring.jackson.time-zone
=
GMT+8
memcached.url
=
192.168.130.230:11211
\ No newline at end of file
server/src/main/resources/bootstrap-prod.properties
浏览文件 @
afba8405
...
...
@@ -32,4 +32,6 @@ ribbon.ConnectTimeout=60000
#------------ Please don't change above configurations ------------
spring.jackson.date-format
=
yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone
=
GMT+8
\ No newline at end of file
spring.jackson.time-zone
=
GMT+8
memcached.url
=
192.168.130.230:11211
\ No newline at end of file
server/src/main/resources/bootstrap-test1.properties
浏览文件 @
afba8405
...
...
@@ -32,4 +32,6 @@ ribbon.ConnectTimeout=60000
#------------ Please don't change above configurations ------------
spring.jackson.date-format
=
yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone
=
GMT+8
\ No newline at end of file
spring.jackson.time-zone
=
GMT+8
memcached.url
=
192.168.130.230:11211
\ No newline at end of file
server/src/main/resources/bootstrap-test2.properties
浏览文件 @
afba8405
...
...
@@ -32,4 +32,6 @@ ribbon.ConnectTimeout=60000
#------------ Please don't change above configurations ------------
spring.jackson.date-format
=
yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone
=
GMT+8
\ No newline at end of file
spring.jackson.time-zone
=
GMT+8
memcached.url
=
192.168.130.230:11211
\ No newline at end of file
server/src/main/resources/bootstrap-uat.properties
浏览文件 @
afba8405
...
...
@@ -32,4 +32,6 @@ ribbon.ConnectTimeout=60000
#------------ Please don't change above configurations ------------
spring.jackson.date-format
=
yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone
=
GMT+8
\ No newline at end of file
spring.jackson.time-zone
=
GMT+8
memcached.url
=
192.168.130.230:11211
\ No newline at end of file
写
预览
Markdown
格式
0%
请重试
or
附加一个文件
附加文件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录