Ⅰ java如何實現驗證碼驗證功能
Java如何實現驗證碼驗證功能呢?日常生活中,驗證碼隨處可見,他可以在一定程度上保護賬號安全,那麼他是怎麼實現的呢?
Java實現驗證碼驗證功能其實非常簡單:用到了一個Graphics類在畫板上繪制字母,隨機選取一定數量的字母隨機生成,然後在畫板上隨機生成幾條干擾線。
首先,寫一個驗證碼生成幫助類,用來繪制隨機字母:
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Random;
importjavax.imageio.ImageIO;
publicfinalclassGraphicHelper{
/**
*以字元串形式返回生成的驗證碼,同時輸出一個圖片
*
*@paramwidth
*圖片的寬度
*@paramheight
*圖片的高度
*@paramimgType
*圖片的類型
*@paramoutput
*圖片的輸出流(圖片將輸出到這個流中)
*@return返回所生成的驗證碼(字元串)
*/
publicstaticStringcreate(finalintwidth,finalintheight,finalStringimgType,OutputStreamoutput){
StringBuffersb=newStringBuffer();
Randomrandom=newRandom();
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsgraphic=image.getGraphics();
graphic.setColor(Color.getColor("F8F8F8"));
graphic.fillRect(0,0,width,height);
Color[]colors=newColor[]{Color.BLUE,Color.GRAY,Color.GREEN,Color.RED,Color.BLACK,Color.ORANGE,
Color.CYAN};
//在"畫板"上生成干擾線條(50是線條個數)
for(inti=0;i<50;i++){
graphic.setColor(colors[random.nextInt(colors.length)]);
finalintx=random.nextInt(width);
finalinty=random.nextInt(height);
finalintw=random.nextInt(20);
finalinth=random.nextInt(20);
finalintsignA=random.nextBoolean()?1:-1;
finalintsignB=random.nextBoolean()?1:-1;
graphic.drawLine(x,y,x+w*signA,y+h*signB);
}
//在"畫板"上繪制字母
graphic.setFont(newFont("ComicSansMS",Font.BOLD,30));
for(inti=0;i<6;i++){
finalinttemp=random.nextInt(26)+97;
Strings=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(IOExceptione){
e.printStackTrace();
}
returnsb.toString();
}
}
接著,創建一個servlet,用來固定圖片大小,以及處理驗證碼的使用場景,以及捕獲頁面生成的驗證碼(捕獲到的二維碼與用戶輸入的驗證碼一致才能通過)。
importjava.io.OutputStream;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
@WebServlet(urlPatterns="/verify/regist.do")
{
=3398560501558431737L;
@Override
protectedvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//獲得當前請求對應的會話對象
HttpSessionsession=request.getSession();
//從請求中獲得URI(統一資源標識符)
Stringuri=request.getRequestURI();
System.out.println("hello:"+uri);
finalintwidth=180;//圖片寬度
finalintheight=40;//圖片高度
finalStringimgType="jpeg";//指定圖片格式(不是指MIME類型)
finalOutputStreamoutput=response.getOutputStream();//獲得可以向客戶端返回圖片的輸出流
//(位元組流)
//創建驗證碼圖片並返回圖片上的字元串
Stringcode=GraphicHelper.create(width,height,imgType,output);
System.out.println("驗證碼內容:"+code);
//建立uri和相應的驗證碼的關聯(存儲到當前會話對象的屬性中)
session.setAttribute(uri,code);
System.out.println(session.getAttribute(uri));
}
}
接著寫一個HTML注冊頁面用來檢驗一下:
<html>
<head>
<metacharset="UTF-8">
<title>注冊</title>
<linkrel="stylesheet"href="styles/general.css">
<linkrel="stylesheet"href="styles/cell.css">
<linkrel="stylesheet"href="styles/form.css">
<scripttype="text/javascript"src="js/ref.js"></script>
<styletype="text/css">
.logo-container{
margin-top:50px;
}
.logo-containerimg{
width:100px;
}
.message-container{
height:80px;
}
.link-container{
height:40px;
line-height:40px;
}
.link-containera{
text-decoration:none;
}
</style>
</head>
<body>
<divclass="containerform-container">
<formaction="/wen/regist.do"method="post">
<divclass="form"><!--注冊表單開始-->
<divclass="form-row">
<spanclass="cell-1">
<iclass="fafa-user"></i>
</span>
<spanclass="cell-11"style="text-align:left;">
<inputtype="text"name="username"placeholder="請輸入用戶名">
</span>
</div>
<divclass="form-row">
<spanclass="cell-1">
<iclass="fafa-key"></i>
</span>
<spanclass="cell-11"style="text-align:left;">
</span>
</div>
<divclass="form-row">
<spanclass="cell-1">
<iclass="fafa-keyboard-o"></i>
</span>
<spanclass="cell-11"style="text-align:left;">
<inputtype="password"name="confirm"placeholder="請確認密碼">
</span>
</div>
<divclass="form-row">
<spanclass="cell-7">
<inputtype="text"name="verifyCode"placeholder="請輸入驗證碼">
</span>
<spanclass="cell-5"style="text-align:center;">
<imgsrc="/demo/verify/regist.do"onclick="myRefersh(this)">
</span>
</div>
<divclass="form-row"style="border:none;">
<spanclass="cell-6"style="text-align:left">
<inputtype="reset"value="重置">
</span>
<spanclass="cell-6"style="text-align:right;">
<inputtype="submit"value="注冊">
</span>
</div>
</div><!--注冊表單結束-->
</form>
</div>
</body>
</html>
效果如下圖:
當點擊刷新頁面的時候,驗證碼也會隨著變化,但我們看不清驗證碼時,只要點擊驗證碼就會刷新,這樣局部的刷新可以用JavaScript來實現。
在<img
src="/demo/verify/regist.do">中,添加一個問號和一串後綴數字,當刷新時讓後綴數字不斷改變,那麼形成的驗證碼也會不斷變化,我們可以採用的一種辦法是後綴數字用date代替,date獲取本機時間,時間是隨時變的,這樣就保證了刷新驗證碼可以隨時變化。
代碼如下:
functionmyRefersh(e){
constsource=e.src;//獲得原來的src中的內容
//console.log("source:"+source);
varindex=source.indexOf("?");//從source中尋找?第一次出現的位置(如果不存在則返回-1)
//console.log("index:"+index);
if(index>-1){//如果找到了?就進入內部
vars=source.substring(0,index);//從source中截取index之前的內容(index以及index之後的內容都被舍棄)
//console.log("s:"+s);
vardate=newDate();//創建一個Date對象的一個實例
vartime=date.getTime();//從新創建的Date對象的實例中獲得該時間對應毫秒值
e.src=s+"?time="+time;//將加了尾巴的地址重新放入到src上
//console.log(e.src);
}else{
vardate=newDate();
e.src=source+"?time="+date.getTime();
}
}
如回答不詳細可追問
Ⅱ java怎麼開通簡訊驗證碼登錄功能
實現jiava簡訊驗證碼可以按下面的步奏進行:
1、首先,找到一個支持Java語言的介面簡訊平台。
2、接著下載介面文檔,和自己的開發平台進行對接。
3、注意在對接之前測試一下簡訊的速度,一旦對接好想換就比較麻煩,之前就吃過這個虧,最後有個朋友介紹我去簡訊網。
4、如果要購買的話,一定要多測試幾家。
如果在碰到有疑問的地方一定要和技術或者客服多多溝通。
Ⅲ java里怎麼發驗證碼到郵箱,然後登陸郵箱獲取驗證碼請高手指點,盡量具體些。。。
郵箱驗證分幾種,
最簡單的是直接發送驗證碼:驗證碼隨機產生,可以放到cookie里,限制有效時版間就是cookie的存活時權間。產生之後和發送郵件一樣直接發送到郵箱。當用戶驗證的時候直接從cookie里拿到驗證碼進行比較。
還有一種是驗證鏈接。在鏈接里要放一個驗證參數,這個參數包括用戶名或用戶id,創建時間,驗證碼。當然都要用某種方法去加密,當用戶點擊這個鏈接的時候,根據相應的方法解密獲得參數。這個可以放到一張動態表裡,表中有驗證碼和對應的用戶名或id。當用戶要求驗證的時候從資料庫中拿到驗證碼,根據鏈接中的時間判斷是否過期。驗證成功後刪除本條數據即可。如果鏈接只能使用一次,那麼在根據用戶名拿到數據之後不管驗證是否成功都要刪除數據
Ⅳ 用java編寫登陸界面要求有用戶名,密碼和驗證碼的輸出,下面是登錄和取消
importjava.util.Scanner;
publicclassLogon{
='青';
privatestaticfinalintpassword=123;
privatestaticfinalScannersan=newScanner(System.in);
publicstaticvoidmain(String[]args){
chariname;
intipass;
System.out.print("請輸入用戶名:");
iname=san.next().charAt(0);
System.out.print("請輸入密碼:");
ipass=san.nextInt();
if(Logon.username==iname&&Logon.password==ipass){
System.out.println("歡迎你,"+Logon.username);
}else{
System.out.println("對不起,你不是"+Logon.username);
}
}
}
Ⅳ java登錄模塊驗證出現問題求解答
前期准備
首先要先明確有個大體的思路,要實現什麼樣的功能,了解完成整個模塊要運用到哪些方面的知識,以及從做的過程中去發現自己的不足。技術方面的進步大都都需要從實踐中出來的。
功能:用戶注冊功能+系統登錄功能+生成驗證碼
知識:窗體設計、資料庫設計、JavaBean封裝屬性、JDBC實現對資料庫的連接、驗證碼(包括彩色驗證碼)生成技術,還有就些比如像使用正則表達式校驗用戶注冊信息、隨機獲得字元串、對文本可用字元數的控制等
設計的模塊預覽圖:
使用BoxLayout布局,將控制項排列方式設置從上至下:
復制代碼代碼如下:
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
窗體使用了標簽、文本域、密碼域和按鈕等控制項
實現代碼:
public class login extends JFrame{
private static final long serialVersionUID = -4655235896173916415L;
private JPanel contentPane;
private JTextField usernameTextField;
private JPasswordField passwordField;
private JTextField validateTextField;
private String randomText;
public static void main(String args[]){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
login frame=new login();
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public login(){
setTitle("系統登錄");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=new JPanel();
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.PAGE_AXIS));
JPanel usernamePanel=new JPanel();
contentPane.add(usernamePanel);
JLabel usernameLable=new JLabel("u7528u6237u540DuFF1A");
usernameLable.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameLable);
usernameTextField=new JTextField();
usernameTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameTextField);
usernameTextField.setColumns(10);
JPanel passwordPanel = new JPanel();
contentPane.add(passwordPanel);
JLabel passwordLabel = new JLabel("u5BC6 u7801uFF1A");
passwordLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordPanel.add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setColumns(10);
passwordField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
passwordPanel.add(passwordField);
JPanel validatePanel = new JPanel();
contentPane.add(validatePanel);
JLabel validateLabel = new JLabel("u9A8Cu8BC1u7801uFF1A");
validateLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(validateLabel);
validateTextField = new JTextField();
validateTextField.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(validateTextField);
validateTextField.setColumns(5);
randomText = RandomStringUtils.randomAlphanumeric(4);
CAPTCHALabel label = new CAPTCHALabel(randomText);//隨機驗證碼
label.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
validatePanel.add(label);
JPanel buttonPanel=new JPanel();
contentPane.add(buttonPanel);
JButton submitButton=new JButton("登錄");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
do_submitButton_actionPerformed(e);
}
});
submitButton.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
buttonPanel.add(submitButton);
JButton cancelButton=new JButton("退出");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_cancelButton_actionPerformed(e);
}
});
cancelButton.setFont(new Font("微軟雅黑",Font.PLAIN,15));
buttonPanel.add(cancelButton);
pack();// 自動調整窗體大小
setLocation(com.lixiyu.util.SwingUtil.centreContainer(getSize()));// 讓窗體居中顯示
}
窗體居中顯示:
public class SwingUtil {
/*
* 根據容器的大小,計算居中顯示時左上角坐標
*
* @return 容器左上角坐標
*/
public static Point centreContainer(Dimension size) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();// 獲得屏幕大小
int x = (screenSize.width - size.width) / 2;// 計算左上角的x坐標
int y = (screenSize.height - size.height) / 2;// 計算左上角的y坐標
return new Point(x, y);// 返回左上角坐標
}
}
1.2獲取及繪制驗證碼
public class CAPTCHALabel extends JLabel {
private static final long serialVersionUID = -963570191302793615L;
private String text;// 用於保存生成驗證圖片的字元串
public CAPTCHALabel(String text) {
this.text = text;
setPreferredSize(new Dimension(60, 36));// 設置標簽的大小
}
@Override
public void paint(Graphics g) {
super.paint(g);// 調用父類的構造方法
g.setFont(new Font("微軟雅黑", Font.PLAIN, 16));// 設置字體
g.drawString(text, 5, 25);// 繪制字元串
}
}
*彩色驗證碼:
public class ColorfulCAPTCHALabel extends JLabel {
private static final long serialVersionUID = -963570191302793615L;
private String text;// 用於保存生成驗證圖片的字元串
private Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };// 定義畫筆顏色數組
public ColorfulCAPTCHALabel(String text) {
this.text = text;
setPreferredSize(new Dimension(60, 36));// 設置標簽的大小
}
@Override
public void paint(Graphics g) {
super.paint(g);// 調用父類的構造方法
g.setFont(new Font("微軟雅黑", Font.PLAIN, 16));// 設置字體
for (int i = 0; i < text.length(); i++) {
g.setColor(colors[RandomUtils.nextInt(colors.length)]);
g.drawString("" + text.charAt(i), 5 + i * 13, 25);// 繪制字元串
}
}
}
1
Ⅵ 用java怎麼製作驗證碼
驗證方法很多
蠢一點的後台寫代碼,或者前台頁面加js
當然你用框架自帶的也行,例如struts的
Ⅶ java簡訊驗證碼如何驗證
先創建一個驗證碼,通過簡訊代理發送到用戶指定的手機,待用戶提交後做對比,看驗證碼是否一致。
整體流程:
用戶輸入手機號碼,點擊獲取驗證碼
伺服器創建驗證碼,並通過簡訊代理商發送到用戶手機
用戶查看驗證碼,輸入提交
伺服器進行確認,反饋成功或者失敗。
Ⅷ java模擬登陸(突破了驗證碼)怎麼拿驗證碼跟用戶名、密碼一起post登陸網站
沒有通用方法抄,網站稍微做襲點手腳你原來的方法就失效了。
===========================補充=======================
不是說安全設置問題,只要它把登錄網站頁面中的表單元素名字一改,你所有工作就白費了啊。對方很容易做的。你又得重頭來。
你非要試一下,有2種方法,一是直接在提交url後附加表單各元素,不過伺服器可能要檢查,不一定行。
第二你自己做個假頁面,與提交表單內容一致,你用js自己填元素值,自動提交。