当前位置:首页 » 编程语言 » 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-05-06 17:32:01 浏览:966
linux命令查看目录 发布:2024-05-06 17:24:18 浏览:529
sqlvb 发布:2024-05-06 17:24:16 浏览:226
分镜头脚本软件 发布:2024-05-06 17:22:54 浏览:823
华为手机百度的缓存如何清理 发布:2024-05-06 17:22:53 浏览:940
网投源码 发布:2024-05-06 17:10:35 浏览:870
看门狗脚本 发布:2024-05-06 17:10:28 浏览:107
如何查看服务器型号主板型号 发布:2024-05-06 16:52:25 浏览:601
无冬之夜脚本 发布:2024-05-06 16:46:04 浏览:634
双引号在c语言 发布:2024-05-06 16:45:52 浏览:966