java登錄驗證,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;

}

}

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-hant/n/286796.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-23 03:48
下一篇 2024-12-23 03:48

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Python 常用數據庫有哪些?

    在Python編程中,數據庫是不可或缺的一部分。隨着互聯網應用的不斷擴大,處理海量數據已成為一種趨勢。Python有許多成熟的數據庫管理系統,接下來我們將從多個方面介紹Python…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • openeuler安裝數據庫方案

    本文將介紹在openeuler操作系統中安裝數據庫的方案,並提供代碼示例。 一、安裝MariaDB 下面介紹如何在openeuler中安裝MariaDB。 1、更新軟件源 sudo…

    編程 2025-04-29

發表回復

登錄後才能評論