Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nouiWithSpringMVC
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
gechengyang
nouiWithSpringMVC
Commits
f9c83778
Commit
f9c83778
authored
Sep 06, 2020
by
WeiCong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
验证码生成
parent
9bc4d82a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
0 deletions
+60
-0
VerifyCodeCreateController.java
...sentation/noui/controller/VerifyCodeCreateController.java
+60
-0
No files found.
src/main/java/org/sss/presentation/noui/controller/VerifyCodeCreateController.java
0 → 100644
View file @
f9c83778
package
org
.
sss
.
presentation
.
noui
.
controller
;
import
log.Log
;
import
log.LogFactory
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.sss.presentation.noui.common.Constants
;
import
javax.imageio.ImageIO
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.awt.*
;
import
java.awt.image.BufferedImage
;
import
java.io.IOException
;
import
java.util.Random
;
@Controller
public
class
VerifyCodeCreateController
{
final
static
String
STR
=
"QWERTYUPLKJHGFDSAZXCVBNMqwertyupkjhgfdsazxcvbnm1234567890"
;
private
static
final
Log
log
=
LogFactory
.
getLog
(
VerifyCodeCreateController
.
class
);
private
static
final
int
width
=
100
;
//验证码图片宽度
private
static
final
int
height
=
50
;
//验证码图片高度
@RequestMapping
(
"/VerifyCode"
)
public
void
create
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
drawVerifyCode
(
request
,
response
);
}
/**
* 绘画验证码
*/
private
void
drawVerifyCode
(
HttpServletRequest
request
,
HttpServletResponse
response
)
{
Graphics2D
g
=
null
;
try
{
Random
r
=
new
Random
();
BufferedImage
bi
=
new
BufferedImage
(
width
,
height
,
BufferedImage
.
TYPE_INT_RGB
);
g
=
bi
.
createGraphics
();
g
.
setBackground
(
new
Color
(
200
,
150
,
255
));
g
.
fillRect
(
0
,
0
,
width
,
height
);
StringBuilder
codes
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
String
code
=
STR
.
charAt
(
r
.
nextInt
(
STR
.
length
()))
+
""
;
g
.
setColor
(
new
Color
(
r
.
nextInt
(
88
),
r
.
nextInt
(
188
),
r
.
nextInt
(
255
)));
Font
font
=
new
Font
(
"Times New Roman"
,
Font
.
ITALIC
,
r
.
nextInt
(
1
)*
7
+
30
);
g
.
setFont
(
font
);
g
.
drawString
(
code
,
(
i
*
18
)
+
10
,
30
);
codes
.
append
(
code
);
}
request
.
getSession
().
setAttribute
(
Constants
.
VERIFYCODE
,
codes
.
toString
());
ImageIO
.
write
(
bi
,
"JPG"
,
response
.
getOutputStream
());
}
catch
(
IOException
e
)
{
log
.
error
(
"绘制登陆验证码异常"
,
e
);
}
finally
{
if
(
g
!=
null
){
g
.
dispose
();
}
}
}
}
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