您的位置:首页 > 编程语言 > Java开发

Struts2:登录验证码(随机字符串)的实现

2017-06-13 20:39 399 查看

页面代码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>验证码实现</title>
<style>
img{width:100px;}
</style>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<script type="text/javascript">
$(function(){
$("#securityCode-img").attr("src","getSecurityCodeImg.action?rmd="+new Date().getTime())
.click(function(){
$(this).attr("src","getSecurityCodeImg.action?rmd="+new Date().getTime());
});
});
</script>
</head>
<body>
<span>验证码:</span>
<img id="securityCode-img"/>
</body>
</html>


Action代码

public class SecurityCodeAction extends ActionSupport{

private int imgWidth;//图片宽度
private int imgHeight;//图片高度
private int codeCount;//验证码个数
private String securityCode;
private Random random=new Random();

public int getImgWidth() {
return imgWidth;
}

public void setImgWidth(int imgWidth) {
this.imgWidth = imgWidth;
}

public int getImgHeight() {
return imgHeight;
}

public void setImgHeight(int imgHeight) {
this.imgHeight = imgHeight;
}

public int getCodeCount() {
return codeCount;
}

public void setCodeCount(int codeCount) {
this.codeCount = codeCount;
}
//注意返回类型为void
public void getSecurityCodeImg() throws IOException{
securityCode=this.getRandomStr(this.getCodeCount());//获取随机验证码
ActionContext.getContext().getSession().put("securityCode", securityCode);//将验证码存入session,以便登录验证时取出比较

HttpServletResponse response=ServletActionContext.getResponse();
response.setDateHeader("Expires", 1L);
response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");
response.addHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");

this.imgRender(securityCode,response.getOutputStream());//根据生成的随机字符串生成图片
}
//获取随机字符串
private String getRandomStr(int count){
String[] strs={"0","1","2","3","4","5","6","7","8","9",
"a","b","c","d","e","f","g","h","i","j",
"k","l","m","n","p","q","r","s","t","u",
"v","w","x","y","z","A","B","C","D","E",
"F","G","H","I","J","K","L","M","N","O",
"P","Q","R","S","T","U","V","W","X","Y","Z"};
String result="";
for(int i=0;i<this.getCodeCount();i++){
result+=strs[random.nextInt(strs.length)];
}
return result;
}
//获取随机颜色
private Color getRandomColor(){
Color[] colors=new Color[5];
colors[0]=new Color(32, 158, 25);
colors[1]=new Color(218, 42, 19);
colors[2]=new Color(31, 75, 208);
colors[3]=new Color(0, 102, 182);
colors[4]=new Color(171, 0, 85);

return colors[random.nextInt(5)];
}
//获取随机字体
private Font getRandomFont(){
Font fonts[]=new Font[5];
fonts[0] = new Font("Aharoni", Font.PLAIN, 35);
fonts[1] = new Font("Book Antiqua", Font.PLAIN, 40);
fonts[2] = new Font("Calibri", Font.PLAIN, 37);
fonts[3] = new Font("Lucida Console", Font.PLAIN,39);
fonts[4] = new Font("DilleniaUPC", Font.PLAIN, 34);
return fonts[random.nextInt(5)];
}
//生成验证码图片
private void imgRender(String securityCode,OutputStream out) throws IOException{
BufferedImage bufferedImage=new BufferedImage(
this.getImgWidth(),this.getImgHeight(),BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g=(Graphics2D)bufferedImage.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getImgWidth(), this.getImgHeight());

//画出随机字符串
for(int i=0;i<securityCode.length();i++){
int y=0;
char c=securityCode.charAt(i);
if(i==random.nextInt(this.getCodeCount())){
y=30-random.nextInt(6);
}
else{
y=30+random.nextInt(6);
}

g.setColor(this.getRandomColor());
g.setFont(this.getRandomFont());
g.drawString(String.valueOf(c),25*i+15, y);
}
//随机增加100个点
for(int i=0;i<100;i++){
g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
g.drawOval(random.nextInt(this.getImgWidth()), random.nextInt(this.getImgHeight()),
0, 0);
}
//随机增加10条线
for(int i=0;i<20;i++){
g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
g.drawLine(random.nextInt(this.getImgWidth()), random.nextInt(this.getImgWidth()),
random.nextInt(this.getImgHeight()), random.nextInt(this.getImgHeight()));
}
g.dispose();
ImageIO.write(bufferedImage, "jpg", out);
}

}


struts配置文件

<package name="security" extends="struts-default">
<action name="getSecurityCodeImg" class="com.sanwenyu.action.SecurityCodeAction"
method="getSecurityCodeImg">
<param name="imgWidth">130</param>
<param name="imgHeight">36</param>
<param name="codeCount">4</param>
</action>
</package>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts 验证码