当前位置:首页 » 编程语言 » java验证登陆

java验证登陆

发布时间: 2023-09-19 15:21:48

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

㈡ java 怎么验证用户名和密码

1,建一个数据库:是有用户名与密码的。
2,java中,通过建立一个类,比如说:DatabaseConnector,用于与数据库进行连接。
3,类建好后,如果你用的是mysql数据库的话,还需要再java中加载相应的jar包(mysql-connector-java-3.0.jar),其他数据库也需要加载相应的包。
4.用户名与密码,放于数据库中一个表中,当用户登录时,在数据库该表中查找是否有用户名与密码与所给相同的字段。如有,登录成功,跳转到相应页面。如无,登录失败。

㈢ java怎样实现登录验证

  • 1.打开编程工具:

    打开java编程的界面,采用的是eclipse软件;

㈣ java web 验证用户是否已登录有哪些方式。

首先,你的web需要有登入后将登录信息保存到session的过程,在用户访问那些需要登录的页面或者action的时候(可以用过过滤器,或者直接在相应的页面判断),从session里面获取登录信息,如果没有,则说明没有登录.跳转到登录页面后,登陆成功,保存登录信息到session,这个时候再访问原先的,就可以通过验证..一整个流程大概就是这样.重点就是登录信息保存在session里面,验证用户是否登录,就依据这个了

㈤ 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链接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}

热点内容
发信息脚本 发布:2025-02-02 07:03:07 浏览:738
l2l3缓存 发布:2025-02-02 06:56:47 浏览:521
为什么安卓下不了虫虫助手 发布:2025-02-02 06:46:47 浏览:42
ftp服务器ui 发布:2025-02-02 06:24:15 浏览:102
wifi有多少种密码 发布:2025-02-02 06:22:06 浏览:586
app账号和密码忘了怎么办啊 发布:2025-02-02 06:21:58 浏览:106
map访问 发布:2025-02-02 06:09:07 浏览:825
android获取应用版本 发布:2025-02-02 05:54:19 浏览:747
pythonif比较 发布:2025-02-02 05:24:03 浏览:260
已连接的无线网如何知道密码 发布:2025-02-02 04:53:51 浏览:634