struts做登录检验码

前台
<script type="text/javascript">
window.onload=function(){
var verifyObj = document.getElementById("Verify");
verifyObj.src="securityCodeImage.action?timestamp="+new Date().getTime();
verifyObj.onclick=function(){
this.src="securityCodeImage.action?timestamp="+new Date().getTime();
};
}
</script>
<body>
${Message }
<form action="login" method="post">
用户名<input type="text" name="user.username" /><br />
密码<input type="password" name="user.password" /><br />
检验码<input type="text" name="securityCode" /><br />
<img id="Verify" style="cursor: hand;" alt="看不清,换一张" /><br />
<input type="submit" name="submit" value="登录" />
</form>
</body>
</html>


action
 <action name="securityCodeImage" class="securityCodeImageAction" method="securityCodeImage">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="bufferSize">2048</param>
</result>
</action>

后台

/**
* 提供图片验证码
*
*/
@SuppressWarnings("serial")
@Component("securityCodeImageAction")
@Scope("prototype")
public class SecurityCodeImageAction extends ActionSupport {

// 图片流
private ByteArrayInputStream imageStream;

public ByteArrayInputStream getImageStream() {
return imageStream;
}
public void setImageStream(ByteArrayInputStream imageStream) {
this.imageStream = imageStream;
}
public String securityCodeImage() throws Exception {
String securityCode = getSecurityCode();
imageStream=getImageAsInputStream(securityCode);
// 放入session中
ActionContext actionContext = ActionContext.getContext();
Map session=actionContext.getSession();
session.put("SESSION_SECURITY_CODE", securityCode);
return SUCCESS;
}

private String getSecurityCode() {
// 字符集合(除去易混淆的数字0、数字1、字母l、字母o、字母O)
char[] codes = { '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
StringBuffer securityCode =new StringBuffer();
for (int i = 0; i < 4; i++) {
int r = (int) (Math.random() * codes.length);
securityCode =securityCode.append(codes[r]) ;
}
return securityCode.toString();
}
private ByteArrayInputStream getImageAsInputStream(String securityCode) throws Exception {
//图片
BufferedImage image=new BufferedImage(70, 20, BufferedImage.TYPE_INT_RGB);
Graphics g=image.createGraphics();
g.setColor(Color.LIGHT_GRAY);//设置边框颜色
//设置字体颜色和样式
g.setColor(new Color(19,148,246));
for(int i = 0; i < 4;i++){
g.drawString(String.valueOf(securityCode.charAt(i)), i * 15 + 5, 15);
}
//关闭资源
g.dispose();
ByteArrayInputStream inputStream = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos);
jpeg.encode(image);
byte[] bts = bos.toByteArray();
inputStream = new ByteArrayInputStream(bts);
return inputStream;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值