「javaweb验证码」Javaweb验证码如何储存
今天给各位分享javaweb验证码的知识,其中也会对Javaweb验证码如何储存进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
解决web界面验证码不显示的问题
背景描述:在liunx系统上,使用tomcat中间件,访问web项目,登录页面的图片验证码显示不出来,但是在window系统上可以正常显示
解决方案: 设置一下这个文件tomcat/bin/catalina.sh,在文件中找到JAVA_OPTS,按如下设置 :JAVA_OPTS="$JAVA_OPTS -Djava.awt.headless=true"
之后就可以正常显示了。问题解决,记录一下!
JAVAWEB项目怎么实现验证码
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class Code {
// 图片的宽度。
private int width = 160;
// 图片的高度。
private int height = 38;
// 验证码字符个数
private int codeCount = 4;
// 验证码干扰线数
private int lineCount = 20;
// 验证码
private String code = null;
// 验证码图片Buffer
private BufferedImage buffImg = null;
Random random = new Random();
private boolean type = false;
public Code() {
}
public Code(int width, int height) {
this.width = width;
this.height = height;
}
public Code(int width, int height, int codeCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
}
public Code(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
}
public void init(boolean type){
this.type = type;
}
// 生成图片
private void creatImage(boolean type) {
int fontWidth = width / codeCount;// 字体的宽度
int fontHeight = height - 5;// 字体的高度
int codeY = height - 8;
// 图像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.getGraphics();
//Graphics2D g = buffImg.createGraphics();
// 设置背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
// 设置字体
Font font = null;
if(!type) font = new Font("Fixedsys", Font.BOLD, fontHeight);
else font = getFont(fontHeight);
g.setFont(font);
// 设置干扰线
for (int i = 0; i lineCount/2; i++) {
int xs = random.nextInt(width);
int ys = random.nextInt(height);
int xe = xs + random.nextInt(width);
int ye = ys + random.nextInt(height);
g.setColor(getRandColor(1, 255));
if(!type) g.drawLine(xs, ys, xe, ye);
else shear(g, width, height, getRandColor(1, 255)) ;
}
// 添加噪点
float yawpRate = 0.01f;// 噪声率
int area = (int) (yawpRate * width * height);
for (int i = 0; i area; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
buffImg.setRGB(x, y, random.nextInt(255));
}
String str1 = randomStr(codeCount);// 得到随机字符
this.code = str1;
for (int i = 0; i codeCount; i++) {
String strRand = str1.substring(i, i + 1);
g.setColor(getRandColor(1, 255));
// g.drawString(a,x,y);
// a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处
g.drawString(strRand, i*fontWidth+3, codeY);
}
}
// 得到随机字符
private String randomStr(int n) {
String str1 = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz1234567890";//I和l不要
String str2 = "";
int len = str1.length() - 1;
double r;
for (int i = 0; i n; i++) {
r = (Math.random()) * len;
str2 = str2 + str1.charAt((int) r);
}
return str2;
}
// 得到随机颜色
private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
if (fc 255)
fc = 255;
if (bc 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
/**
* 产生随机字体
*/
private Font getFont(int size) {
Random random = new Random();
Font font[] = new Font[5];
font[0] = new Font("Ravie", Font.PLAIN, size);
font[1] = new Font("Antique Olive Compact", Font.PLAIN, size);
font[2] = new Font("Fixedsys", Font.PLAIN, size);
font[3] = new Font("Wide Latin", Font.PLAIN, size);
font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, size);
return font[random.nextInt(5)];
}
// 扭曲方法
private void shear(Graphics g, int w1, int h1, Color color) {
shearX(g, w1, h1, color);
shearY(g, w1, h1, color);
}
private void shearX(Graphics g, int w1, int h1, Color color) {
int period = random.nextInt(2);
boolean borderGap = true;
int frames = 1;
int phase = random.nextInt(2);
for (int i = 0; i h1; i++) {
double d = (double) (period 1)
* Math.sin((double) i / (double) period
+ (6.2831853071795862D * (double) phase)
/ (double) frames);
g.copyArea(0, i, w1, 1, (int) d, 0);
if (borderGap) {
g.setColor(color);
g.drawLine((int) d, i, 0, i);
g.drawLine((int) d + w1, i, w1, i);
}
}
}
private void shearY(Graphics g, int w1, int h1, Color color) {
int period = random.nextInt(40) + 10; // 50;
boolean borderGap = true;
int frames = 20;
int phase = 7;
for (int i = 0; i w1; i++) {
double d = (double) (period 1)
* Math.sin((double) i / (double) period
+ (6.2831853071795862D * (double) phase)
/ (double) frames);
g.copyArea(i, 0, 1, h1, 0, (int) d);
if (borderGap) {
g.setColor(color);
g.drawLine(i, (int) d, i, 0);
g.drawLine(i, (int) d + h1, i, h1);
}
}
}
public void write(OutputStream sos) throws IOException {
if(buffImg == null) creatImage(type);
ImageIO.write(buffImg, "png", sos);
// JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
// encoder.encode(buffImg);
sos.close();
}
public BufferedImage getBuffImg() {
if(buffImg == null) creatImage(type);
return buffImg;
}
public String getCode() {
return code.toLowerCase();
}
//使用方法
/*public void getCode3(HttpServletRequest req, HttpServletResponse response,HttpSession session) throws IOException{
// 设置响应的类型格式为图片格式
response.setContentType("image/jpeg");
//禁止图像缓存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
CreateImageCode vCode = new CreateImageCode(100,30,5,10);
session.setAttribute("code", vCode.getCode());
vCode.write(response.getOutputStream());
response.flushBuffer();
}*/
}
javaweb登录三次失败验证码怎么实现
使用redis存储,存储当前登录的用户名与登录失败次数,超过三次就显示验证码
session和cookie都不建议使用,建议服务端开启验证,浏览器端都可以绕过这些检查
北大青鸟java培训:解决WEB性能测试中的验证码问题?
现在越来越多的网站为了安全性或是防止Spam的侵害,采用了验证码的校验技术。
简单地说,验证码就是在进行登录或是内容提交的时候,页面上会随机出现一个人工可识别,但机器不可识别的验证字符串(一般是采用背景、扭曲等方式产生的图片),要求登录或是提交内容时同时输入这个验证码。
验证码可以有效防止对口令的刺探和所谓的网络推广软件带来的大量的Spam内容,目前已经被许多Internet或是Intranet应用接受为标准的实现方式。
但对性能测试来说,这种验证码又带来了很大的问题。
最突出的问题是,性能测试工具本身是自动化工具,由于这种验证码采用的是“防止自动化工具尝试”的方法,因此,在录制了脚本之后会发现,很难对脚本进行调整,以使其适应验证码验证的需要。
已经不止一次有人提到这个问题,并询问有没有较好的解决方案。
对这个问题,我个人的看法是,基本上可以考虑从三个途径来解决该问题:1、第一种方法,也是最容易想到的,在被测系统中暂时屏蔽验证功能,也就是说,临时修改应用,无论用户输入的是什么验证码,都认为是正确的。
这种方法最容易实现,对测试结果也不会有太大的影响(当然,这种方式去掉了“验证验证码”这个环节,不过这个环节本来就很难成为系统性能瓶颈)。
但这种方法有一个致命的问题:如果被测系统是一个实际已上线的系统,屏蔽验证功能会对已经在运行的业务造成非常大的安全性的风险,因此,对于已上线的系统来说,用这种方式就不合适了;2、第二种方法,在第一种方法的基础上稍微进行一些改进。
第一种方法带来了很大的安全性问题,那么我们可以考虑,不取消验证,但在其中留一个后门,我们设定一个所谓的“万能验证码”,只要用户输入这个“万能验证码”,我们就验证通过,否则,还是按照原先的验证方式进行验证。
这种方式仍然存在安全性的问题,但由于我们可以通过管理手段将“万能验证码”控制在一个小的范围内,而且只在性能测试期间保留这个小小的后门,相对第一种方法来说,在安全性方面已经有较大的改进了;3、如果安全性对应用来说真的是至关重要的,不容许有一丝一毫的闪失,那我们还可以用更进一步的方法来处理这个问题。
一般的性能测试工具(MI的LR、Seague的Silkperformer等)都能够调用外部的DLL或是组件接口,因此,江苏电脑培训建议可以考虑获得“验证码验证”部分的实现,写一个验证码获取的DLL,在测试脚本中进行调用即可。
javaweb验证码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Javaweb验证码如何储存、javaweb验证码的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。