當前位置:首頁 » 編程語言 » java登錄驗證

java登錄驗證

發布時間: 2022-11-26 14:42:47

A. java使用JWT進行登錄驗證

每次需要驗證的網路請求,都要在header里添加jwt

B. java 如何實現同一賬戶登錄驗證

今天繼續討論?-0-#這個只需要session和application就好了,用戶登錄時,這樣寫:User
user
=
.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這種方式是我的一個前輩想到的,我們公司的所有項目都採納了這種方式,確保一個賬號只能在一個瀏覽器中使用

C. java怎麼開通簡訊驗證碼登錄功能

實現jiava簡訊驗證碼可以按下面的步奏進行:
1、首先,找到一個支持Java語言的介面簡訊平台。
2、接著下載介面文檔,和自己的開發平台進行對接。
3、注意在對接之前測試一下簡訊的速度,一旦對接好想換就比較麻煩,之前就吃過這個虧,最後有個朋友介紹我去簡訊網。
4、如果要購買的話,一定要多測試幾家。
如果在碰到有疑問的地方一定要和技術或者客服多多溝通。

D. java鏈接mysql資料庫實現登陸如何驗證

//這是我以前寫的核對資料庫實現登陸的方法,你只看jdbc部分就好,我還特地給你加了點注釋x0dx0aString sql = "select username,password from account";x0dx0aString user = request.getParameter("user");x0dx0aString pass = request.getParameter("password");x0dx0aint j = 0;x0dx0aConnection conn = null;x0dx0aPreparedStatement ps = null;x0dx0aResultSet rs = null;x0dx0atry {x0dx0aconn = JDBCTools1.getConnection();x0dx0aps = conn.prepareStatement(sql);x0dx0ars = ps.executeQuery();x0dx0a//從表中查詢獲取所有賬戶的用戶名&密碼的ResultSet 對象x0dx0awhile(rs.next()){x0dx0aint i = 0;x0dx0ax0dx0aString username[] = new String[10];//用戶名數組x0dx0aString password[] = new String[10];//密碼數組x0dx0ausername[i] = rs.getString(1);x0dx0apassword[i] = rs.getString(2);x0dx0aif(user.equals(username[i])&&pass.equals(password[i])){//比對x0dx0aresponse.getWriter().print("you are welcome!");x0dx0aj++;x0dx0a}else if(user.equals(username[i])&&!pass.equals(password[i])){x0dx0aresponse.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");x0dx0aresponse.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right");x0dx0aj++;x0dx0a}else{x0dx0acontinue;x0dx0a}x0dx0ai++;x0dx0a}x0dx0aif(j == 0){x0dx0aresponse.getWriter().println("Your username may not be properly");x0dx0a}x0dx0a} catch (Exception e) {x0dx0ae.printStackTrace();x0dx0a}finally{x0dx0aJDBCTools1.release(rs, ps, conn);x0dx0a}x0dx0a//這是我JDBCTools的getConnection方法x0dx0agetConnection{x0dx0aString driverClass = oracle.jdbc.driver.OracleDriver;x0dx0aString jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;x0dx0a//你的資料庫的用戶名密碼x0dx0aString user = null;x0dx0aString password = null;x0dx0a// 通過反射創建Driver對象x0dx0aClass.forName(driverClass);x0dx0areturn DriverManager.getConnection(jdbcUrl, user, password);}x0dx0a//這是我JDBCTools的release方法x0dx0apublic static void release(ResultSet rs, Statement statement,x0dx0aConnection conn) {x0dx0aif (rs != null) {x0dx0atry {x0dx0ars.close();x0dx0a} catch (SQLException e) {x0dx0ae.printStackTrace();x0dx0a}x0dx0a}x0dx0ax0dx0aif (statement != null) {x0dx0atry {x0dx0astatement.close();x0dx0a} catch (Exception e2) {x0dx0ae2.printStackTrace();x0dx0a}x0dx0a}x0dx0ax0dx0aif (conn != null) {x0dx0atry {x0dx0aconn.close();x0dx0a} catch (Exception e2) {x0dx0ae2.printStackTrace();x0dx0a}x0dx0a}x0dx0a}

E. 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

F. Java (for循環)編程實現用戶登錄時的信息驗證

import java.util.*;
public class PswVerify { /**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成方法存根
Scanner input =new Scanner(System.in);
String username ="manage";
int password = 0000;

for(int i =3;i>=1;i--)
{

System.out.println("請輸入用戶名:");
String name =input.next();
System.out.println("請輸入密碼:");
int pw =input.nextInt();

if(name.equals(username)&&pw==password)
{
System.out.println("歡迎進入我行我素購物管理系統");
break;
}
else if(i!=1)
{
System.out.println("用戶名和密碼不匹配!");
System.out.println("你還有"+(i-1)+"次機會,請重新輸入:");
}
else
{
System.out.println("您沒有許可權進入系統!");
} }}
}

G. java web 驗證用戶是否已登錄有哪些方式。

你好!
保存登錄可以用session又可以用cookie
只要成功驗證密碼、用戶名、驗證碼後在跳轉之前用session、cookie來保存相關的信息,在下次登錄的時候(不要關閉瀏覽器),驗證session、cookie是否非空,是空,跳轉登錄頁面,非空,跳轉主頁
我的回答你還滿意嗎~~

H. java web 驗證用戶是否已登錄有哪些方式。

首先,你的web需要有登入後將登錄信息保存到session的過程,在用戶訪問那些需要登錄的頁面或者action的時候(可以用過過濾器,或者直接在相應的頁面判斷),從session裡面獲取登錄信息,如果沒有,則說明沒有登錄.跳轉到登錄頁面後,登陸成功,保存登錄信息到session,這個時候再訪問原先的,就可以通過驗證..一整個流程大概就是這樣.重點就是登錄信息保存在session裡面,驗證用戶是否登錄,就依據這個了

I. 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;
}
}

J. JAVA問題:實現用戶登錄身份驗證功能拜託了各位 謝謝

這是問題1後台的,我實在不願意從頭到尾都幫你寫了,希望這段代碼能給你啟發,有問題可以問,可是你這樣把所有問題都拋出來,實在對你的學習不利,希望你明白這個道理 package com.long1; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class MyJdbc { private Connection con = null; private PreparedStatement ps = null; private Statement st = null; private ResultSet rs = null; private String url = "jdbc:mysql://localhost:3306/snake_db1"; private String name = "root"; private String pass = "123456789"; private String driver = "com.mysql.jdbc.Driver"; public MyJdbc() { try { Class.forName(driver); } catch (Exception e) { e.printStackTrace(); } } private void open() { try { con = DriverManager.getConnection(url, name, pass); } catch (Exception e) { e.printStackTrace(); } } public List<Login> query() { ArrayList<Login> arr = null; try { open(); st = con.createStatement(); rs = st.executeQuery("select * from login where flag!=0"); Login l = null; arr = new ArrayList<Login>(); while (rs.next()) { l = new Login(); l.setId(rs.getInt(1)); l.setName(rs.getString(2)); l.setPass(rs.getString(3)); l.setFlag(rs.getString(4)); arr.add(l); } } catch (Exception e) { e.printStackTrace(); } finally { close(); } return arr; } public boolean check(String name, String pass) { boolean flag = false; try { open(); ps = con.prepareStatement("select id from login where name=? and pass=? and flag!=0"); ps.setString(1, name); ps.setString(2, pass); rs = ps.executeQuery(); if (rs.next()) { flag = true; } } catch (Exception e) { e.printStackTrace(); } finally { close(); } return flag; } private void close() { try { if (rs != null) { rs.close(); rs = null; } if (ps != null) { ps.close(); ps = null; } if (con != null) { con.close(); con = null; } } catch (Exception e) { e.printStackTrace(); } } }

熱點內容
安卓手機卸載更新什麼意思 發布:2024-04-24 19:29:35 瀏覽:227
文件des加密 發布:2024-04-24 19:24:20 瀏覽:705
魔獸世界data文件夾 發布:2024-04-24 19:24:13 瀏覽:214
蘋果手機怎麼清空緩存 發布:2024-04-24 19:23:38 瀏覽:893
微信密碼沒有手機號如何找回 發布:2024-04-24 19:18:20 瀏覽:875
微雲解析源碼 發布:2024-04-24 19:13:58 瀏覽:792
紙張記錄存儲 發布:2024-04-24 19:00:35 瀏覽:801
存儲結構中定義一個集合 發布:2024-04-24 18:52:05 瀏覽:428
搜狗網址存儲的密碼在哪裡查看 發布:2024-04-24 18:52:03 瀏覽:918
抖音搶福袋腳本最新 發布:2024-04-24 18:52:00 瀏覽:702