java登錄驗證碼
Ⅰ 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;
}
}
Ⅱ 登錄頁面驗證碼,使用Servlet做的,麻煩各位看下,在線等
輸出流用了沒關閉,然後你又調用了
Ⅲ java登錄驗證源代碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test_Login extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton bntLogin;
private JButton bntCannel;
private JPasswordField pwd;
private JTextField username;
private JLabel jLabel2;
private JLabel jLabel1;
public static void main(String[] args) {
Test_Login inst = new Test_Login();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
public Test_Login() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
jLabel1 = new JLabel();
jPanel1.add(jLabel1);
jLabel1.setText("用戶名");
jLabel1.setBounds(45, 30, 75, 25);
}
{
jLabel2 = new JLabel();
jPanel1.add(jLabel2);
jLabel2.setText("密碼");
jLabel2.setBounds(45, 75, 55, 15);
}
{
username = new JTextField();
jPanel1.add(username);
username.setBounds(100, 30, 140, 25);
}
{
pwd = new JPasswordField();
jPanel1.add(pwd);
pwd.setBounds(100, 70, 140, 25);
}
{
bntLogin = new JButton();
jPanel1.add(bntLogin);
bntLogin.setText("登陸");
bntLogin.setBounds(40, 120, 60, 30);
bntLogin.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (username.getText().equals("lisong")
&& pwd.getText().equals("lisong")) {
JOptionPane.showMessageDialog(Test_Login.this,
"登錄成功");
} else {
JOptionPane.showMessageDialog(Test_Login.this,
"登錄失敗");
}
}
});
bntCannel = new JButton();
jPanel1.add(bntCannel);
bntCannel.setText("取消");
bntCannel.setBounds(180, 120, 60, 30);
bntCannel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
}
}
pack();
setSize(300, 215);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Ⅳ java怎樣實現登錄驗證
1.打開編程工具:
打開java編程的界面,採用的是eclipse軟體;
Ⅳ java登錄驗證的驗證碼怎麼做
首先你要找一家靠譜的簡訊平台,他們會提供你一份對接的協議。
巴卜簡訊平台會給到你相應語言的示例代碼,而且還會針對驗證碼給你提供一些解決方案,避免你的網站系統或app被惡意的刷碼。
Ⅵ spring框架的java登錄驗證碼怎麼寫
可以參考下下面的代碼,這個代碼是servlet生成的,稍微修改下就能用的
packageservlet;
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;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
{
publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
BufferedImageimage=newBufferedImage(100,30,BufferedImage.TYPE_INT_RGB);
Graphicsg=image.getGraphics();
Randomr=newRandom();
g.setColor(newColor(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.fillRect(0,0,100,30);
g.setColor(newColor(255,255,255));
g.setFont(newFont(null,Font.BOLD,24));
Stringcode=getCode();
HttpSessionsession=request.getSession();
session.setAttribute("textcode",code);
g.drawString(code,10,25);
response.setContentType("image/jpeg");
OutputStreamos=response.getOutputStream();
ImageIO.write(image,"jpeg",os);
os.close();
}
//隨機生成驗證碼字元串的類
publicstaticStringgetCode(){
char[]letters={'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'};
Stringcode="";
boolean[]bool=newboolean[letters.length];
for(inti=0;i<5;i++){
intnum;
do{
num=(int)(Math.random()*(letters.length));
}while(bool[num]);
code+=letters[num];
bool[num]=true;
}
returncode.toUpperCase();
}
}
Ⅶ java驗證碼的實現 需要知道嗎
一.什麼是驗證碼及它的作用
:驗證碼為全自動區分計算機和人類的圖靈測試的縮寫,是一種區分用戶是計算機的公共全自動程序,這個問題可以由計算機生成並評判,但是必須只有人類才能解答.可以防止惡意破解密碼、刷票、論壇灌水、有效防止某個黑客對某一個特定注冊用戶用特定程序暴力破解方式進行不斷的登錄。
二.圖文驗證碼的原理
:在servlet中隨機生成一個指定位置的驗證碼,一般為四位,然後把該驗證碼保存到session中.在通過Java的繪圖類以圖片的形式輸出該驗證碼。為了增加驗證碼的安全級別,可以輸出圖片的同時輸出干擾線,最後在用戶提交數據的時候,在伺服器端將用戶提交的驗證碼和Session保存的驗證碼進行比較。
三.驗證碼所需的技術
:i.因為驗證碼中的文字,數字,應為都是可變的,故要用到隨機生成數技術。
ii.如果驗證碼中包含漢字,則要用到漢字生成技術.
iii.可以使用Ajax技術實現局部刷新
iv.可以使用圖片的縮放和旋轉技術,
vi.隨機繪制干擾線(可以是折現,直線等)
vii.如果考慮到驗證碼的安全性,可以使用MD5加密.
Ⅷ java 登錄驗證用戶名和密碼
你不會調試程序的么?
一看你的報錯報告就知道問題出在你的Login.java的第62行,
應該是有一個變數為null,所以程序運行出錯了
Ⅸ 用JAVA編寫一個用戶登陸界面,用戶驗證通過資料庫實現
這是我以前寫的系統登錄框的login
package java1;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Rectangle;
import java.awt.Font;
public class login extends JFrame
{
public login() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args)
{
login frm=new login();
frm.setBounds(300,200,300,260);
frm.setVisible(true);
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
jLabel1.setForeground(Color.red);
jLabel1.setText("用戶名");
jLabel1.setBounds(new Rectangle(25, 72, 68, 29));
jButton2.setBounds(new Rectangle(142, 159, 81, 30));
jButton2.setText("取消");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
jButton1.setBounds(new Rectangle(38, 159, 81, 29));
jButton1.setText("確定");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jTextField1.setBounds(new Rectangle(109, 79, 136, 23));
jPasswordField1.setBounds(new Rectangle(109, 115, 136, 23));
this.getContentPane().add(jLabel1);
jLabel3.setFont(new java.awt.Font("隸書", Font.PLAIN, 24));
jLabel3.setText("學 生 考 試 系 統");
jLabel3.setBounds(new Rectangle(22, 15, 268, 47));
this.getContentPane().add(jTextField1);
this.getContentPane().add(jLabel2);
this.getContentPane().add(jButton2);
this.getContentPane().add(jButton1);
this.getContentPane().add(jLabel3);
this.getContentPane().add(jPasswordField1);
jLabel2.setForeground(Color.red);
jLabel2.setText("密碼");
jLabel2.setBounds(new Rectangle(26, 112, 68, 29));
}
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JLabel jLabel3 = new JLabel();
JPasswordField jPasswordField1 = new JPasswordField();
main2 wo=new main2();
int sum;
//判斷許可權的方法
public void success()
{
try{
ResultSet rs1;
String s1="select * from 管理許可權 where name='"+jTextField1.getText()+"' and password='"+jPasswordField1.getText()+"'";
rs1=sqlcx.Rs_jiluji(s1);
rs1.first();
sum=Integer.parseInt((String)(rs1.getString(3)));
System.out.println(sum);
very();
}
catch(Exception c){System.out.println("success error");}
}
//根據許可權設置窗口
public void very()
{
if (sum==1)
{JOptionPane.showOptionDialog(this, "您現在是以教師許可權登陸", "登陸信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
else
{
JOptionPane.showOptionDialog(this, "你現在是以學生許可權登陸", "登陸信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
wo.jButton2.setVisible(false);
wo.jButton3.setVisible(false);
wo.jButton4.setVisible(false);
wo.jButton5.setVisible(false);
wo.jButton6.setVisible(false);
wo.jButton7.setVisible(false);
wo.jButton8.setVisible(false);
wo.jMenu2.setVisible(false);
wo.jMenu3.setVisible(false);
wo.jMenu4.setVisible(false);
}
}
public void jButton1_actionPerformed(ActionEvent e)
{
//判斷用戶名是否為空
if (jTextField1.getText().length() == 0) {
JOptionPane.showOptionDialog(this, "用戶名不能為空", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
//判斷密碼是否為空
else if (jPasswordField1.getText().length()==0)
{
JOptionPane.showOptionDialog(this,"密碼不能為空", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
else
{
try
{
ResultSet rs;//聲明記錄集
String sql="select * from 管理許可權 where name='"+jTextField1.getText()+"'";
rs = sqlcx.Rs_jiluji(sql);
if (rs.next())
{
if(jPasswordField1.getText().equals(rs.getString(2)))
{
success();
wo.setBounds(60,40,700,600);
wo.setVisible(true);
this.dispose();
}
else
{
JOptionPane.showOptionDialog(this,"密碼錯誤", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
else
{
JOptionPane.showOptionDialog(this,"沒有所要找的用戶名", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
catch(Exception c){
JOptionPane.showOptionDialog(this,"連接資料庫失敗", "錯誤信息"
, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null, null, null);
}
}
}
public void jButton2_actionPerformed(ActionEvent e) {
System.exit(0);
}
}