此前,我们已经讨论过网页自动登录http://www.minioa.net/viewtopic.php?f=6&t=19&p=29。
这里,我们讨论的是MiniOA系统内如何实现自动登录。登录需要用户名和密码,我们选择将用户名和密码加密后传给一个网页,然后实现自动登录,也就是通过一个网址来自动登录系统。
例如我通过http://localhost:8080/minioa /autologin.jsf?url=231293a51b134f92a77ceb4409ca39de8e6ded038e3d019063581fea83904219fa2e5c88c42410c92f95290927c7000b680a621a5fb1511c1f4ffc5d1d10d29506d2060b18d7a4f7821e48cb7de5393a18584867be7f2255f0a4ebf1d95c3ff95eb62fbc504573c71ffa85948b822328 来登录系统。
为什么考虑使用自动登录?企业内网中的用户可能会觉得总是输入用户名和密码会比较麻烦,当然一般用户倒是不必关心,要是老板提出这样的要求,那就得考虑了。
上面这个网址只有一个参数url,显然它是加密的,包含了用户名、密码还有ip地址,只要通过这个网址,而且是自己的电脑(自己的ip),那么就允许通过。我们可以将这个链接添加到收藏夹。
我们定义一个属性autoLogin,将判断的代码放置其getter中,这样就可以在autologin.xhtml中以
<h:outputText value="#{User.autoLogin}" />
方式来执行自动登录的代码。
代码的逻辑,首先判断是否登录,如果登录了就不继续执行,如果没有登录就取得参数url的值,解密,判断是否包含三段字符串,如果是就进而判断传递过来的ip是否和当前ip一致,如果一致就使用用户名和密码登录,登录成功后就设置session数据。
public String getAutoLogin() {
try {
String url;
if (!"true".equals(getMySession().getIsLogin())) {
Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
url = (String) params.get("url");
String passwordKey = FunctionLib.getWebParameter("passwordKey");
url = new Blowfish(passwordKey).decryptString(url);
String[] arr = url.split(";");
if(arr.length==3){
String name = arr[0];
String pwd = arr[1];
String ip = FunctionLib.getIp();
if (ip.equals(arr[2])) {
Query query = getSession().getNamedQuery("core.user.autologin");
query.setParameter("userName", name);
query.setParameter("password", pwd);
if ("1".equals(String.valueOf(query.list().get(0)))) {
query = getSession().getNamedQuery("core.user.getrecordbyusername");
query.setParameter("userName", name);
Iterator it = query.list().iterator();
while (it.hasNext()) {
Object obj[] = (Object[]) it.next();
if ("1".equals(FunctionLib.getString(obj[14])))
return "";
getMySession().setUserId(FunctionLib.getInt(obj[0]));
getMySession().setDepaName(FunctionLib.getString(obj[16]));
getMySession().setEmail(FunctionLib.getString(obj[9]));
getMySession().setDisplayName(FunctionLib.getString(obj[13]));
getMySession().buildOpList(getSession());
getMySession().buildTopMenu();
getMySession().buildLeftMenu();
}
it = null;
getMySession().setUserName(name);
getMySession().setIsLogin("true");
System.out.println(name + " auto login at time " + FunctionLib.dtf.format(new java.util.Date()) + ", ip is " + ip);
} else
System.out.println(name + " attempt to auto login at time " + FunctionLib.dtf.format(new java.util.Date()) + ", ip is " + ip);
} else
System.out.println(name + " auto login at time " + FunctionLib.dtf.format(new java.util.Date()) + ", ip is incorrect");
}
}
url = FunctionLib.getWebAppName();
if ("".equals(url))
url = "http://" + FunctionLib.getRequestHeaderMap("host") + "/";
else
url = "http://" + FunctionLib.getRequestHeaderMap("host") + "/" + url;
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.sendRedirect(url);
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
String passwordKey = FunctionLib.getWebParameter("passwordKey");
String userName = prop.get("userName");
String password = prop.get("password");
//取得网站名称
String url = FunctionLib.getWebAppName();
if ("".equals(url))
url = "http://" + FunctionLib.getRequestHeaderMap("host") + "/autologin.jsf?url=";
else
url = "http://" + FunctionLib.getRequestHeaderMap("host") + "/" + url + "/autologin.jsf?url=";
prop.put("url", url + new Blowfish(passwordKey).encryptString(userName + ";" + password + ";" + FunctionLib.getIp()));
修改密码后,这个加密的网址需要重新生成。