用户登录是Web程序中的重要功能,相关操作包括注册、登录检查。经济有这样的情况:初始时用户信息(例如用户名、密码)是透明的,但将来有可能对这些信息进行加密保存,那么如何预留加密接口呢?访问者模式是较好的实现方式之一。
具体代码如下:
1、定义抽象访问接口IVisitor
public interface IVisitor{
public void visit(User u);
}
//初始时默认具体加密访问者EncryptVistor
public class EncryptVisitor implements IVisitor{
public void visit(User user){}
}
visit()是空方法,表明没有对用户对象进行加密,说明初始时用户名、对象是透明的。
//用户基础类
public class User {
String user;
String pwd;
int type;
public User(String user,String pwd,int type) {
this.user = user;
this.pwd = pwd;
this.type = type;
}
public User(String user,String pwd){
this.user = user;
this.pwd = pwd;
}
public void encrypt(IVisitor v){
v.visit(this);
}
}
enctypt()是用户对象加密方法。
2、访问者模式功能调用代码
访问者模式框架代码加在哪些功能上呢?主要有三个方面。(1)注册新用户要预留加密代码;(2)用户登录检查要预留加密代码;(3)整体用户信息修改要预留加密代码。这是由于初始时用户名、密码没有加密;当采用加密代码后,必须对数据库已有用户进行加密处理。因此主要编制了以下三个servlet.
(1)注册新用户servlet类Regist,url为regist.
public class Regist extends HttpServlet {
public Regist(){}
protected void service(HttpServletRequest request,HttpServletResponse response){
String user = request.getParameter("user");
String pwd = request.getParameter("pwd");
String type = request.getParameter("type");
IVisitor v = new EncryptVisitor();
User u = new User(user,pwd,Integer.valueOf(type));
u.encrypt(v);
String result = "success";
//向数据库中写入数据代码
}
}
(2)登录检查servlet类Login,URL为login。
public class Login extends HttpServlet{
public Login(){};
protected void service(HttpServletRequest request,HttpServletResponse response){
String user = request.getParameter("user");
String pwd = request.getParameter("pwd");
IVisitor v = new EncryptVisitor();
User u = new User(user,pwd);
u.encrypt(v);
String result = "success";
//向数据库发送查询操作代码
}
}
(3)全部用户信息加密处理servlet类ChangeAllUser,URL为changeAllUser。
public class ChangeAllUser extends HttpServlet{
public ChangeAllUser(){}
protected void service (HttpServletRequest request,HttpServletResponse response){
IVisitor v = new EncryptVisitor();
//查询数据库操作,获取登录用户信息
Connection connection = DBHelper.getConnection();
Statement stmt = connection.createStatement();
Result rs = stmt.executeQuery("select * from t_login");
Vector vector = new Vector();
//利用循环
while (rs.next()){
User u = new User(rs.getString("user"),rs.getString("pwd"),rs.getInt("type"));
vector.add(u);
}
rs.close();
stmt.executeUpdate("delete from t_login");
for (int i = 0; i < vector.size(); i++) {
User u = vector.get(i);
u.encrypt(v);
//执行数据库写入操作
}
}
}
(3)加密具体访问者类EncryptVisitor
public class EncryptVisitor implements IVisitor{
private String encode(String str){
String result = null;
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(str.getBytes());
byte[] buf = messageDigest.digest();
result = byteToString(buf);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
private String byteToString(byte[] bytes){
String hash = "";
for (int i = 0; i < bytes.length; i++) {
int temp;
temp = bytes[i]<0?bytes[i]+256:bytes[i];
if (temp < 16) {
hash += "0";
}
hash += Integer.toString(temp,16);
}
hash = hash.toUpperCase();
return hash;
}
public void visit(User u){
u.user = encode(u.user);
u.pwd = encode(u.pwd);
}
}
至此,就完成了以访问者模式开发用户登录的功能。