本文目录一览:
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这种方式是我的一个前辈想到的,我们公司的所有项目都采纳了这种方式,确保一个账号只能在一个浏览器中使用