java应用中利用Domino提供的Web Service验证用户

实现一个简单的登录功能,输入用户名、密码,正确跳转到相应的页面,若不正确提示相应的出错信息,只不过这里的用户是在Domino的names库中验证。

    实现方法大致是,由Domino提供一个验证用户的Web Service,接收的参数为用户名、密码,然后返回给客户机一个简单的user对象,包含names库个人文档中的用户名、Email地址、简称、密码是否正确等信息。在java应用中调用这个Web Service,并解析返回的对象,根据对象的信息做页面的跳转。

    一、在Domino的xxx.nsf(最好不要写在names库中)数据库中新建一个Web Service提供者test,可以用lotusscript或者java编写,这里用lotusscript,(Options)部分:

Option Public
%INCLUDE "lsxsd.lss"

在(Declarations)部分实现验证的方法:

Dim ss As NotesSession
Class DominoUser ’定义返回给客户机的对象
 Public userid As String
 Public username As String
 Public lastname As String
 Public password As Boolean
 Public email As String
End Class
Class WsTest
 Sub new
  Set ss = New NotesSession
 End Sub
 Function getDominoUserById(uid As String,pwd As String) As DominoUser ’接收2个参数
  Set getDominoUserById = New DominoUser
  Dim db As NotesDatabase
  Dim view As NotesView
  Dim doc As NotesDocument
  Set db = ss.GetDatabase("kenny","names.nsf") ‘匹配names库
  Set view = db.GetView("SelfPerson")
  Set doc = view.GetDocumentByKey(uid) '得到个人文档
  If Not doc Is Nothing Then '传入的参数pwd不能为空
   If ss.VerifyPassword(pwd,doc.HTTPPassword(0)) Then ‘这里就是验证WEB密码了
    getDominoUserById.password = True
   Else
    getDominoUserById.password = False
   End If
   getDominoUserById.userid = uid '相当于shortname
   getDominoUserById.username = doc.FullName(0)
   getDominoUserById.lastname = doc.LastName(0)
   getDominoUserById.email = doc.InternetAddress(0)
  End If
 End Function
End Class

    那么这个服务返回给客户机的数据格式大致为:

<ReturnMethodValue>
 <userid>xyz</userid>
 <username>XXX</username>
 <lastname>XXX</lastname>
 <password>1</password >
 <email>XXX@xxx.com</email>
</ReturnMethodValue>

    二、Web Service的属性还需要做一些设置:

java应用中利用Domino提供的Web Service验证用户 - Kenny - 人 生 海 海名称是test,那么用URL来描述就是test?wsdl,PortType类就是实现方法的类名,相当于Web Service的入口,必须指定。

java应用中利用Domino提供的Web Service验证用户 - Kenny - 人 生 海 海这里安全等级可以设为1,勾选读者以上有权限访问。

java应用中利用Domino提供的Web Service验证用户 - Kenny - 人 生 海 海SOAP消息格式用RPC/编码的,其它默认。

    三、当然要使外部程序能调用这个Web Service,还需要数据库及服务器有访问权限:

java应用中利用Domino提供的Web Service验证用户 - Kenny - 人 生 海 海数据库ACL设置匿名用户的权限至少为读者。

java应用中利用Domino提供的Web Service验证用户 - Kenny - 人 生 海 海上面设置了Web Service运行安全等级为1,那么就要保证服务器允许执行受限的代理,如图所示。

    四、新建一个java web工程,做一个简单的登录界面,为简单起见,本例的表现层用JSF,登录按钮的响应事件对应UserBean中的方法为:

 public String loginActionDomino() throws MalformedURLException, Exception{
  URL url = new URL(http://localhost/study/xxx.nsf/test?wsdl);
  Client c = new Client(url);
  Object[] results  = c.invoke("GETDOMINOUSERBYID", new Object[]{userid,password}); //方法名,传递2个参数
  Document obj = (Document)results[0];//web service返回的XML对象
  /*如果不知道WSDL返回的对象格式,可以用这个方法打印出来观察
  TransformerFactory tf = TransformerFactory.newInstance();
  Transformer t = tf.newTransformer();
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  t.transform(new DOMSource(obj), new StreamResult(bos));
  String str = bos.toString("UTF-8");
  System.out.println(str);*/
  //把DOM对象解析出来
  if(obj.getElementsByTagName("USERID").item(0).getFirstChild() != null)
   this.userid = obj.getElementsByTagName("USERID").item(0).getFirstChild().getNodeValue();
  if(obj.getElementsByTagName("USERNAME").item(0).getFirstChild() != null)
   this.username = obj.getElementsByTagName("USERNAME").item(0).getFirstChild().getNodeValue();
  if(obj.getElementsByTagName("PASSWORD").item(0).getFirstChild() != null)
   this.password = obj.getElementsByTagName("PASSWORD").item(0).getFirstChild().getNodeValue();
  if(obj.getElementsByTagName("LASTNAME").item(0).getFirstChild() != null)
   this.lastname = obj.getElementsByTagName("LASTNAME").item(0).getFirstChild().getNodeValue();
  if(obj.getElementsByTagName("EMAIL").item(0).getFirstChild() != null)
   this.email = obj.getElementsByTagName("EMAIL").item(0).getFirstChild().getNodeValue();
  if(this.username == null){
   clear();
   this.errmsg = "用户名不存在!!!";
   return "deny";
  }else if(this.password.equals("0")){
   clear();
   this.errmsg = "密码错误!!!";
   return "deny";
  }else{
   this.errmsg = "登录成功!!!";
   return "success";
  }
 }

    另外这里调用Web Service用的是XFire,所以要把xfire-all-1.2.6.jar这个包放到工程下。

    五、将java工程发布到tomcat下,还需要把wsdl4j-1.6.1.jar和XmlSchema-1.2.jar这2个包放到tomcat的库路径,否着会报错。在IE中访问项目,输入用户名和密码点登录验证:

java应用中利用Domino提供的Web Service验证用户 - Kenny - 人 生 海 海java应用中利用Domino提供的Web Service验证用户 - Kenny - 人 生 海 海

    用names中正确的用户登录成功后:

java应用中利用Domino提供的Web Service验证用户 - Kenny - 人 生 海 海

posted @ 2010-10-01 07:46  hannover  阅读(1964)  评论(0编辑  收藏  举报