本文目錄一覽:
- 1、java怎樣實現登錄驗證
- 2、java swing中登錄界面驗證碼的實現。
- 3、北大青鳥java培訓:解決WEB性能測試中的驗證碼問題?
- 4、Java如何實現驗證碼驗證功能
- 5、java 登陸時的驗證碼怎麼做?
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;
}
}
原創文章,作者:PUZP,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/149808.html