本文目錄一覽:
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;
}
}
JAva登錄驗證窗口
我幫你改了一下,你那個登錄和重置按鈕的監聽器里的代碼書上估計是省略了,運行效果:
代碼在這裡:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.*;
public class UserLogIn extends JFrame{
public JPanel pnluser;
public JLabel lbl用戶登錄,lbl用戶名,lbl密碼;
public JTextField txt用戶名;
public JPasswordField pwd密碼;
public JButton btn登錄,btn重置;
String dburl=”jdbc:odbc:driver={Microsoft Access Driver(*.mdb,*.accdb)};DBQ=D://stdub.mdb”;
Connection conn=null;
Statement stmt=null;
int 查詢記錄=0;
public static void main(String[] args){
new TestClass();
}
public UserLogIn(){
pnluser=new JPanel();
lbl用戶登錄=new JLabel();
lbl用戶名=new JLabel();
lbl密碼=new JLabel();
txt用戶名=new JTextField();
pwd密碼=new JPasswordField();
btn登錄=new JButton();
btn重置=new JButton();
userInit();
}
public void userInit(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,300);
this.setResizable(false);
this.setTitle(“登錄”);
this.pnluser.setLayout(null);
this.pnluser.setBackground(Color.white);
this.lbl用戶登錄.setText(“用戶登錄”);
this.lbl用戶登錄.setFont(new Font(“宋體”,Font.BOLD,18));
this.lbl用戶登錄.setForeground(Color.blue);
this.lbl用戶名.setText(“用戶名”);
this.lbl用戶名.setText(“密碼”);
this.lbl用戶名.setText(“登錄”);
this.lbl用戶名.setText(“重置”);
this.lbl用戶登錄.setBounds(160,15,80,30);
this.lbl用戶名.setBounds(90,70,80,30);
this.lbl密碼.setBounds(90,120,80,30);
this.txt用戶名.setBounds(160,70,150,30);
this.pwd密碼.setBounds(160,120,150,30);
this.btn登錄.setBounds(100,200,80,30);
this.btn登錄.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e){
//這裡寫按登錄按鈕後執行的代碼,書上估計是省略了。
System.out.println(“登錄”);
}
}
);
this.btn重置.setBounds(220,200,80,30);
this.btn重置.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
//這裡寫按重置按鈕後執行的代碼,書上估計是省略了。
System.out.println(“重置”);
}
}
);
this.pnluser.add(lbl用戶登錄);
this.pnluser.add(lbl用戶名);
this.pnluser.add(lbl密碼);
this.pnluser.add(txt用戶名);
this.pnluser.add(pwd密碼);
this.pnluser.add(btn登錄);
this.pnluser.add(btn重置);
this.add(pnluser);
this.setVisible(true);
}
}
java 如何實現同一賬戶登錄驗證
今天繼續討論?-0-#這個只需要session和application就好了,用戶登錄時,這樣寫:User
user
=
dao.login(userName,
password);//
資料庫中判斷用戶名和密碼if
(null
!=
user)
{//
表示用戶存在
session.setAttribute(“user”,
user);//
把用戶放進session中
application.setAttribute(userName,
session.getId());/*
把用戶所在的sessionId放進application中,首先要明白一點,一個session對應一個瀏覽器,其次要注意一點,userName必須是唯一的*/}當用戶訪問到其他url的時候,可以在過濾器或你的攔截器中這樣寫:User
user
=
(User)
session.getAttribute(“user”);//
從session中取出用戶if
(null
==
user)
{//
未登錄或者登錄已經過期
response.sendRedirect(request.getContextPath());//
跳轉到首頁或登錄頁面}String
sessionId
=
(String)
application.getAttribute(user.getUserName());if
(null
==
sessionId
||
!sessionId.equals(session.getId()))
{/*這說明用戶已經在其他電腦或其它瀏覽器登錄了,那麼之前登錄的session就無效了,自動被後面的登錄給踢掉*/
response.sendRedirect(request.getContextPath());//
跳轉到首頁或登錄頁面}chain.doFilter(request,
response);//
通過驗證,放行用戶進入目標url這種方式是我的一個前輩想到的,我們公司的所有項目都採納了這種方式,確保一個賬號只能在一個瀏覽器中使用
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/286796.html