Topic: 如何在java中实现nt domain 的user 认证

  Print this page

1.如何在java中实现nt domain 的user 认证 Copy to clipboard
Posted by: merlin45
Posted on: 2002-12-25 23:18

使用package jcifs.smb.*;
实现

private boolean authenticateUser(Request req, HttpServletResponse resp) {
HttpSession session = req.getSession(false);

//get the server ip and path from property file
String ServerPath = "";
try {
  ResourceBundle rb = ResourceBundle.getBundle("top");
  ServerPath = rb.getString("ServerPath");
} catch (Exception e) {
  return false;      
}

String uid = req.getParameter("username");
String pw = req.getParameter("password");
String dm = req.getParameter("domain");
StringBuffer authString = new StringBuffer("smb://");
authString.append(dm);
authString.append(';');
authString.append(uid);
authString.append(':');
authString.append(pw);
authString.append('@');
authString.append(ServerPath);//server's ip address + "/" + shared directory of server

// the codes below are the smb authenticate.
try {
  SmbFile sf = new SmbFile(authString.toString());
  sf.isDirectory();
} catch (Exception e) {
  String str = e.toString();
  if (str.indexOf("Access denied") != -1) {
    return false;
  }
}

Random rand = (Random)session.getValue("random");
if (rand == null) {
  rand = new Random();
  session.putValue("random", rand);
}
String sid = "" + rand.nextLong();
session.putValue("securityid", sid);
Cookie coo = new Cookie("securityid", sid);
resp.addCookie(coo);

String username = (String)session.getValue("logonid");
String domain = (String)session.getValue("logondomain");
if (username == null || !username.equals(uid)) {
  Cookie coo2 = new Cookie("logonid", uid);
  coo2.setMaxAge(10000000);
  resp.addCookie(coo2);
  session.putValue("logonid", uid);
}
if (domain == null || !domain.equals(dm)) {
  Cookie coo3 = new Cookie("logondomain", dm);
  coo3.setMaxAge(10000000);
  resp.addCookie(coo3);
  session.putValue("logondomain", dm);
}

mm.getUsrProfileBean();
return true;
}

这是我3年前一个程序最初的做法,大家看一下什么地方可以改进


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923