首页 > 编程知识 正文

java登陆界面的验证码(java登陆界面的验证码在哪)

时间:2023-12-24 12:05:18 阅读:319998 作者:BONZ

本文目录一览:

java怎样实现登录验证

1.打开编程工具:

打开java编程的界面,采用的是eclipse软件;

2

2.建立一个java工程:

简洁操作如下:单击“file”-“new”-“java project”;

然后,在工程菜单中选中工程,单击鼠标右键出出来菜单,依次选中“new”-“class”;

具体查看“ eclipse如何建立一个java工程”;

0eclipse如何建立一个java工程

END

2.代码实现步骤

1.建立输入扫描:

采用java中的Scanner类实现输入数据的获取,具体代码如下;

Scanner scan = new Scanner(System.in);

2.接收用户名:

建立一个提示信息,提示输入用户名,并储存输入的用户名,代码如下:

System.out.println("请输入登陆用户名:");

String usename=scan.nextLine();

3.接收密码:

建立一个提示信息,提示输入密码,并存储输入的密码,代码如下:

System.out.println("请输入登陆的密码:");

String password=scan.nextLine();

4.验证信息:

采用if else语句来实现对用户名和密码的验证,并打印提示信息,代码如下:

if(!usename.equals("me")){

System.out.println("用户名非法。");

}else if(!password.equals("123456")){

System.out.println("登陆密码错误。");

}else{

System.out.println("恭喜您,登陆信息通过验证。");

}

java swing中登录界面验证码的实现。

public class ValidCode extends JComponent implements MouseListener {

private String code;

private int width, height = 40;

private int codeLength = 4;

private Random random = new Random();

public ValidCode() {

width = this.codeLength * 16 + (this.codeLength - 1) * 10;

setPreferredSize(new Dimension(width, height));

setSize(width, height);

this.addMouseListener(this);

setToolTipText("点击可以更换验证码");

}

public int getCodeLength() {

return codeLength;

}

/*

设置验证码文字的长度

*/

public void setCodeLength(int codeLength) {

if(codeLength 4) {

this.codeLength = 4;

} else {

this.codeLength = codeLength;

}

}

public String getCode() {

return code;

}

/*

产生随机的颜色

*/

public Color getRandColor(int min, int max) {

if (min 255)

min = 255;

if (max 255)

max = 255;

int red = random.nextInt(max - min) + min;

int green = random.nextInt(max - min) + min;

int blue = random.nextInt(max - min) + min;

return new Color(red, green, blue);

}

/*

设置验证码具体的字母是什么

*/

protected String generateCode() {

char[] codes = new char[this.codeLength];

for (int i = 0, len = codes.length; i len; i++) {

if (random.nextBoolean()) {

codes[i] = (char) (random.nextInt(26) + 65);

} else {

codes[i] = (char) (random.nextInt(26) + 97);

}

}

this.code = new String(codes);

return this.code;

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

if(this.code == null || this.code.length() != this.codeLength) {

this.code = generateCode();

}

width = this.codeLength * 16 + (this.codeLength - 1) * 10;

super.setSize(width, height);

super.setPreferredSize(new Dimension(width, height));

Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);

g.setFont(mFont);

//绘制出验证码的背景的矩形轮廓

Graphics2D g2d = (Graphics2D) g;

g2d.setColor(getRandColor(200, 250));

g2d.fillRect(0, 0, width, height);

g2d.setColor(getRandColor(180, 200));

g2d.drawRect(0, 0, width - 1, height - 1);

//绘制出验证码背景的线

int i = 0, len = 150;

for (; i len; i++) {

int x = random.nextInt(width - 1);

int y = random.nextInt(height - 1);

int x1 = random.nextInt(width - 10) + 10;

int y1 = random.nextInt(height - 4) + 4;

g2d.setColor(getRandColor(180, 200));

g2d.drawLine(x, y, x1, y1);

}

/*i = 0; len = 300;

for (; i len; i++) {

int x = random.nextInt(width);

int y = random.nextInt(height);

g2d.setColor(getRandColor(150, 180));

g2d.drawRect(x, y, 0, 0);

}*/

//绘制出验证码的具体字母

i = 0; len = this.codeLength;

FontMetrics fm = g2d.getFontMetrics();

int base = (height - fm.getHeight())/2 + fm.getAscent();

for(;ilen;i++) {

int b = random.nextBoolean() ? 1 : -1;

g2d.rotate(random.nextInt(10)*0.01*b);

g2d.setColor(getRandColor(20, 130));

g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);

}

}

//下一个验证码

public void nextCode() {

generateCode();

repaint();

}

@Override

public void mouseClicked(MouseEvent e) {

nextCode();

}

@Override

public void mousePressed(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseReleased(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseEntered(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseExited(MouseEvent e) {

// TODO Auto-generated method stub

}

}

北大青鸟java培训:解决WEB性能测试中的验证码问题?

现在越来越多的网站为了安全性或是防止Spam的侵害,采用了验证码的校验技术。

简单地说,验证码就是在进行登录或是内容提交的时候,页面上会随机出现一个人工可识别,但机器不可识别的验证字符串(一般是采用背景、扭曲等方式产生的图片),要求登录或是提交内容时同时输入这个验证码。

验证码可以有效防止对口令的刺探和所谓的网络推广软件带来的大量的Spam内容,目前已经被许多Internet或是Intranet应用接受为标准的实现方式。

但对性能测试来说,这种验证码又带来了很大的问题。

最突出的问题是,性能测试工具本身是自动化工具,由于这种验证码采用的是“防止自动化工具尝试”的方法,因此,在录制了脚本之后会发现,很难对脚本进行调整,以使其适应验证码验证的需要。

已经不止一次有人提到这个问题,并询问有没有较好的解决方案。

对这个问题,我个人的看法是,基本上可以考虑从三个途径来解决该问题:1、第一种方法,也是最容易想到的,在被测系统中暂时屏蔽验证功能,也就是说,临时修改应用,无论用户输入的是什么验证码,都认为是正确的。

这种方法最容易实现,对测试结果也不会有太大的影响(当然,这种方式去掉了“验证验证码”这个环节,不过这个环节本来就很难成为系统性能瓶颈)。

但这种方法有一个致命的问题:如果被测系统是一个实际已上线的系统,屏蔽验证功能会对已经在运行的业务造成非常大的安全性的风险,因此,对于已上线的系统来说,用这种方式就不合适了;2、第二种方法,在第一种方法的基础上稍微进行一些改进。

第一种方法带来了很大的安全性问题,那么我们可以考虑,不取消验证,但在其中留一个后门,我们设定一个所谓的“万能验证码”,只要用户输入这个“万能验证码”,我们就验证通过,否则,还是按照原先的验证方式进行验证。

这种方式仍然存在安全性的问题,但由于我们可以通过管理手段将“万能验证码”控制在一个小的范围内,而且只在性能测试期间保留这个小小的后门,相对第一种方法来说,在安全性方面已经有较大的改进了;3、如果安全性对应用来说真的是至关重要的,不容许有一丝一毫的闪失,那我们还可以用更进一步的方法来处理这个问题。

一般的性能测试工具(MI的LR、Seague的Silkperformer等)都能够调用外部的DLL或是组件接口,因此,广西电脑培训建议可以考虑获得“验证码验证”部分的实现,写一个验证码获取的DLL,在测试脚本中进行调用即可。

Java如何实现验证码验证功能

Java如何实现验证码验证功能呢?日常生活中,验证码随处可见,他可以在一定程度上保护账号安全,那么他是怎么实现的呢?

Java实现验证码验证功能其实非常简单:用到了一个Graphics类在画板上绘制字母,随机选取一定数量的字母随机生成,然后在画板上随机生成几条干扰线。

首先,写一个验证码生成帮助类,用来绘制随机字母:

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 final class GraphicHelper {

/**

* 以字符串形式返回生成的验证码,同时输出一个图片

*

* @param width

*            图片的宽度

* @param height

*            图片的高度

* @param imgType

*            图片的类型

* @param output

*            图片的输出流(图片将输出到这个流中)

* @return 返回所生成的验证码(字符串)

*/

public static String create(final int width, final int height, final String imgType, OutputStream output) {

StringBuffer sb = new StringBuffer();

Random random = new Random();

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics graphic = image.getGraphics();

graphic.setColor(Color.getColor("F8F8F8"));

graphic.fillRect(0, 0, width, height);

Color[] colors = new Color[] { Color.BLUE, Color.GRAY, Color.GREEN, Color.RED, Color.BLACK, Color.ORANGE,

Color.CYAN };

// 在 "画板"上生成干扰线条 ( 50 是线条个数)

for (int i = 0; i  50; i++) {

graphic.setColor(colors[random.nextInt(colors.length)]);

final int x = random.nextInt(width);

final int y = random.nextInt(height);

final int w = random.nextInt(20);

final int h = random.nextInt(20);

final int signA = random.nextBoolean() ? 1 : -1;

final int signB = random.nextBoolean() ? 1 : -1;

graphic.drawLine(x, y, x + w * signA, y + h * signB);

}

// 在 "画板"上绘制字母

graphic.setFont(new Font("Comic Sans MS", Font.BOLD, 30));

for (int i = 0; i  6; i++) {

final int temp = random.nextInt(26) + 97;

String s = String.valueOf((char) temp);

sb.append(s);

graphic.setColor(colors[random.nextInt(colors.length)]);

graphic.drawString(s, i * (width / 6), height - (height / 3));

}

graphic.dispose();

try {

ImageIO.write(image, imgType, output);

} catch (IOException e) {

e.printStackTrace();

}

return sb.toString();

}

接着,创建一个servlet,用来固定图片大小,以及处理验证码的使用场景,以及捕获页面生成的验证码(捕获到的二维码与用户输入的验证码一致才能通过)。

import java.io.OutputStream;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = "/verify/regist.do" )

public class VerifyCodeServlet extends HttpServlet {

private static final long serialVersionUID = 3398560501558431737L;

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// 获得 当前请求 对应的 会话对象

HttpSession session = request.getSession();

// 从请求中获得 URI ( 统一资源标识符 )

String uri = request.getRequestURI();

System.out.println("hello : " + uri);

final int width = 180; // 图片宽度

final int height = 40; // 图片高度

final String imgType = "jpeg"; // 指定图片格式 (不是指MIME类型)

final OutputStream output = response.getOutputStream(); // 获得可以向客户端返回图片的输出流

// (字节流)

// 创建验证码图片并返回图片上的字符串

String code = GraphicHelper.create(width, height, imgType, output);

System.out.println("验证码内容: " + code);

// 建立 uri 和 相应的 验证码 的关联 ( 存储到当前会话对象的属性中 )

session.setAttribute(uri, code);

System.out.println(session.getAttribute(uri));

}

接着写一个HTML注册页面用来检验一下:

html

head

meta charset="UTF-8"

title注册/title

link rel="stylesheet" href="styles/general.css"

link rel="stylesheet" href="styles/cell.css"

link rel="stylesheet" href="styles/form.css"

script type="text/javascript" src="js/ref.js"/script

style type="text/css" 

.logo-container {

margin-top: 50px ;

}

.logo-container img {

width: 100px ;

}

.message-container {

height: 80px ;

}

.link-container {

height: 40px ;

line-height: 40px ;

}

.link-container a {

text-decoration: none ;

}

/style

/head

body

div class="container form-container"

form action="/wendao/regist.do" method="post"

div class="form" !-- 注册表单开始 --

div class="form-row"

span class="cell-1"

i class="fa fa-user"/i

/span

span class="cell-11" style="text-align: left;"

input type="text" name="username" placeholder="请输入用户名"

/span

/div

div class="form-row"

span class="cell-1"

i class="fa fa-key"/i

/span

span class="cell-11" style="text-align: left;"

input type="password" name="password" placeholder="请输入密码"

/span

/div

div class="form-row"

span class="cell-1"

i class="fa fa-keyboard-o"/i

/span

span class="cell-11" style="text-align: left;"

input type="password" name="confirm" placeholder="请确认密码"

/span

/div

div class="form-row"

span class="cell-7"

input type="text" name="verifyCode" placeholder="请输入验证码"

/span

span class="cell-5" style="text-align: center;"

img src="/demo/verify/regist.do" onclick="myRefersh(this)"

/span

/div

div class="form-row" style="border: none;"

span class="cell-6" style="text-align: left"

input type="reset" value="重置"

/span

span class="cell-6"  style="text-align:right;"

input type="submit" value="注册"

/span

/div

/div !-- 注册表单结束 --

/form

/div

/body

/html

效果如下图:

在控制台接收到的图片中验证码的变化如下:

当点击刷新页面的时候,验证码也会随着变化,但我们看不清验证码时,只要点击验证码就会刷新,这样局部的刷新可以用JavaScript来实现。

在img

src="/demo/verify/regist.do"中,添加一个问号和一串后缀数字,当刷新时让后缀数字不断改变,那么形成的验证码也会不断变化,我们可以采用的一种办法是后缀数字用date代替,date获取本机时间,时间是随时变的,这样就保证了刷新验证码可以随时变化。

代码如下:

function myRefersh( e ) {

const source = e.src ; // 获得原来的 src 中的内容

//console.log( "source : " + source  ) ;

var index = source.indexOf( "?" ) ;  // 从 source 中寻找 ? 第一次出现的位置 (如果不存在则返回 -1 )

//console.log( "index : " + index  ) ;

if( index  -1 ) { // 如果找到了 ?  就进入内部

var s = source.substring( 0 , index ) ; // 从 source 中截取 index 之前的内容 ( index 以及 index 之后的内容都被舍弃 )

//console.log( "s : " + s  ) ;

var date = new Date(); // 创建一个 Date 对象的 一个 实例

var time = date.getTime() ; // 从 新创建的 Date 对象的实例中获得该时间对应毫秒值

e.src = s + "?time=" + time ; // 将 加了 尾巴 的 地址 重新放入到 src 上

//console.log( e.src ) ;

} else {

var date = new Date();

e.src = source + "?time=" + date.getTime();

}

}

如回答不详细可追问

java 登陆时的验证码怎么做?

后台写一个生成图片随机的代码,生成图片给前台。切换图片的时候,使用ajax获取图片数据就行。

附上生成图片的代码

public class ValidateCode {

private int width=180;

private int height=60;

private int codeCount = 4;

private int x = 0;

private int codeY;

private String Code;

private BufferedImage buffImg;

static char[] codeSequence = { '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','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', 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

private int fontHeight;

public ValidateCode() {

x = width / (codeCount + 2);

fontHeight = height - 2;

codeY = height - 4;

CreateCode();

}

public void CreateCode(){

// 定义图像buffer

BufferedImage buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

Graphics2D g = buffImg.createGraphics();

// 创建一个随机数生成器类

Random random = new Random();

// 将图像填充为白色

g.setColor(Color.WHITE);

g.fillRect(0, 0, width, height);

// 创建字体,字体的大小应该根据图片的高度来定。

Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);

// 设置字体。

g.setFont(font);

// 画边框。

g.setColor(Color.BLACK);

g.drawRect(0, 0, width - 1, height - 1);

// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。

StringBuffer randomCode = new StringBuffer();

int red = 0, green = 0, blue = 0;

// 随机产生codeCount数字的验证码。

for (int i = 0; i codeCount; i++) {

// 得到随机产生的验证码数字。

String strRand = String.valueOf(codeSequence[random.nextInt(62)]);

// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。

red = random.nextInt(255);

green = random.nextInt(255);

blue = random.nextInt(255);

// 用随机产生的颜色将验证码绘制到图像中。

g.setColor(new Color(red, green, blue));

g.drawString(strRand, (i ) * x+20, codeY);

// 将产生的四个随机数组合在一起。

randomCode.append(strRand);

}

this.Code=randomCode.toString().toUpperCase();

this.buffImg=buffImg;

}

public String getCode() {

return Code;

}

public void setCode(String code) {

Code = code;

}

public BufferedImage getBuffImg() {

return buffImg;

}

public void setBuffImg(BufferedImage buffImg) {

this.buffImg = buffImg;

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。