码迷,mamicode.com
首页 > 其他好文 > 详细

(转)OpenFire源码学习之六:用户注册

时间:2017-07-13 17:15:09      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:bar   tail   ict   server   comm   strong   compress   qname   ros   

转:http://blog.csdn.net/huwenfeng_2011/article/details/43413509

用户注册

注册流程:

1、客户端进行握手给服务端发送连接消息:

 

[html] view plain copy
 
  1. <stream:stream to="192.168.2.104" xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0"></stream:stream>  

 

2、服务端回执:

[html] view plain copy
 
  1. <?xml version=‘1.0‘ encoding=‘UTF-8‘?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="hytest240" id="9fd61155" xml:lang="en" version="1.0">  
  2. <stream:features>  
  3. <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">  
  4. <mechanism>DIGEST-MD5</mechanism>  
  5. <mechanism>JIVE-SHAREDSECRET</mechanism>  
  6. <mechanism>PLAIN</mechanism>  
  7. <mechanism>ANONYMOUS</mechanism>  
  8. <mechanism>CRAM-MD5</mechanism>  
  9. </mechanisms>  
  10. <compression xmlns="http://jabber.org/features/compress">  
  11. <method>zlib</method>  
  12. </compression><auth xmlns="http://jabber.org/features/iq-auth"/>  
  13. <register xmlns="http://jabber.org/features/iq-register"/>  
  14. </stream:features>  

3、客户端发送注册申请

[html] view plain copy
 
  1. <iq id="69Bxy-0" to="hytest240" type="get">  
  2. <query xmlns="jabber:iq:register"></query>  
  3. </iq>  

4、服务端给出注册需要的填写的信息,相当与给客户端发送一个申请单

[html] view plain copy
 
  1. <iq type="result" id="69Bxy-0" from="hytest240">  
  2. <query xmlns="jabber:iq:register">  
  3. <username/><password/><email/><name/>  
  4. <xmlns="jabber:x:data" type="form">  
  5. <title>XMPP Client Registration</title>  
  6. <instructions>Please provide the following information</instructions>  
  7. <field var="FORM_TYPE" type="hidden">  
  8. <value>jabber:iq:register</value>  
  9. </field>  
  10. <field var="username" type="text-single" label="Username">  
  11. <required/></field>  
  12. <field var="name" type="text-single" label="Full name"/>  
  13. <field var="email" type="text-single" label="Email"/>  
  14. <field var="password" type="text-private" label="Password">  
  15. <required/>  
  16. </field>  
  17. </x>  
  18. </query>  
  19. </iq>  

5、客户端接收到服务端发送的申请单后,并填写回复:

[html] view plain copy
 
  1. <iq id="69Bxy-1" to="hytest240" type="set">  
  2. <query xmlns="jabber:iq:register">  
  3. <username>test</username>  
  4. <email></email>  
  5. <name></name>  
  6. <password>123456</password>  
  7. </query>  
  8. </iq>  

6、注册完成后,服务端返回成功这里没有做出任何消息,仅仅只是回复。

[html] view plain copy
 
  1. <iq type="result" id="69Bxy-1" from="hytest240" to="hytest240/9fd61155"/>  

 

IQRegisterHandler

IQRegisterHandler位于org.jivesoftware.openfire.handler中。该类主要处理客户端注册信息。

该类中有两个比较重要方法initialize、handleIQ。接下来看这两个方法。

initialize

该方法主要是做初始化注册模板。这个注册模板就是在上面提到的需要发送给客户端申请注册的深表表单。源码如下:

[java] view plain copy
 
  1. @Override  
  2.     public void initialize(XMPPServer server) {  
  3.         super.initialize(server);  
  4.         userManager = server.getUserManager();  
  5.         rosterManager = server.getRosterManager();  
  6.   
  7.         if (probeResult == null) {  
  8.             // Create the basic element of the probeResult which contains the basic registration  
  9.             // information (e.g. username, passoword and email)  
  10.             probeResult = DocumentHelper.createElement(QName.get("query", "jabber:iq:register"));  
  11.             probeResult.addElement("username");  
  12.             probeResult.addElement("password");  
  13.             probeResult.addElement("email");  
  14.             probeResult.addElement("name");  
  15.   
  16.             // Create the registration form to include in the probeResult. The form will include  
  17.             // the basic information plus name and visibility of name and email.  
  18.             // TODO Future versions could allow plugin modules to add new fields to the form   
  19.             final DataForm registrationForm = new DataForm(DataForm.Type.form);  
  20.             registrationForm.setTitle("XMPP Client Registration");  
  21.             registrationForm.addInstruction("Please provide the following information");  
  22.   
  23.             final FormField fieldForm = registrationForm.addField();  
  24.             fieldForm.setVariable("FORM_TYPE");  
  25.             fieldForm.setType(FormField.Type.hidden);  
  26.             fieldForm.addValue("jabber:iq:register");  
  27.   
  28.             final FormField fieldUser = registrationForm.addField();  
  29.             fieldUser.setVariable("username");  
  30.             fieldUser.setType(FormField.Type.text_single);  
  31.             fieldUser.setLabel("Username");  
  32.             fieldUser.setRequired(true);  
  33.   
  34.             final FormField fieldName = registrationForm.addField();   
  35.             fieldName.setVariable("name");  
  36.             fieldName.setType(FormField.Type.text_single);  
  37.             fieldName.setLabel("Full name");  
  38.             if (UserManager.getUserProvider().isNameRequired()) {  
  39.                 fieldName.setRequired(true);  
  40.             }  
  41.   
  42.             final FormField fieldMail = registrationForm.addField();  
  43.             fieldMail.setVariable("email");  
  44.             fieldMail.setType(FormField.Type.text_single);  
  45.             fieldMail.setLabel("Email");  
  46.             if (UserManager.getUserProvider().isEmailRequired()) {  
  47.                 fieldMail.setRequired(true);  
  48.             }  
  49.   
  50.             final FormField fieldPwd = registrationForm.addField();  
  51.             fieldPwd.setVariable("password");  
  52.             fieldPwd.setType(FormField.Type.text_private);  
  53.             fieldPwd.setLabel("Password");  
  54.             fieldPwd.setRequired(true);  
  55.   
  56.             // Add the registration form to the probe result.  
  57.             probeResult.add(registrationForm.getElement());  
  58.         }  
  59.         // See if in-band registration should be enabled (default is true).  
  60.         registrationEnabled = JiveGlobals.getBooleanProperty("register.inband", true);  
  61.         // See if users can change their passwords (default is true).  
  62.         canChangePassword = JiveGlobals.getBooleanProperty("register.password", true);  
  63.     }  

handleIQ

handleIQ方法,方法有四个步骤。

1、判断用户是否已经登陆。如果在session已经存在该用户发送错误消息反馈客户端:

   PacketError.Condition.internal_server_error

2、获取IQ消息包的消息类型,如果是type=get那就是客户端需要获取申请表了。然

   后,服务端封装这个表单,转成成XMPP消息发送给客户端。

3、当获取到的IQ消息包的消息类型给set(type=set)。那么就是客户点填写完了注册

   表单,发送给服务端了。上面描述注册流程的第4步就是提交表单了。源码就不在

   贴出来了,这个比较简单。只是一些判断校验比较多。

4、当所有的校验都正确,一切注册流程都正常的话。服务端就该返回第6点消息了。

   当然在代码执行过程(业务处理,消息校验等)可能会产生些异常。处理异常的信息

   这里把代码贴出来下,大家也可以自己去看源码:

[java] view plain copy
 
  1. catch (UserAlreadyExistsException e) {  
  2.                 reply = IQ.createResultIQ(packet);  
  3.                 reply.setChildElement(packet.getChildElement().createCopy());  
  4.                 reply.setError(PacketError.Condition.conflict);  
  5.             }  
  6.             catch (UserNotFoundException e) {  
  7.                 reply = IQ.createResultIQ(packet);  
  8.                 reply.setChildElement(packet.getChildElement().createCopy());  
  9.                 reply.setError(PacketError.Condition.bad_request);  
  10.             }  
  11.             catch (StringprepException e) {  
  12.                 // The specified username is not correct according to the stringprep specs  
  13.                 reply = IQ.createResultIQ(packet);  
  14.                 reply.setChildElement(packet.getChildElement().createCopy());  
  15.                 reply.setError(PacketError.Condition.jid_malformed);  
  16.             }  
  17.             catch (IllegalArgumentException e) {  
  18.                 // At least one of the fields passed in is not valid  
  19.                 reply = IQ.createResultIQ(packet);  
  20.                 reply.setChildElement(packet.getChildElement().createCopy());  
  21.                 reply.setError(PacketError.Condition.not_acceptable);  
  22.                 Log.warn(e.getMessage(), e);  
  23.             }  
  24.             catch (UnsupportedOperationException e) {  
  25.                 // The User provider is read-only so this operation is not allowed  
  26.                 reply = IQ.createResultIQ(packet);  
  27.                 reply.setChildElement(packet.getChildElement().createCopy());  
  28.                 reply.setError(PacketError.Condition.not_allowed);  
  29.             }  
  30.             catch (Exception e) {  
  31.                 // Some unexpected error happened so return an internal_server_error  
  32.                 reply = IQ.createResultIQ(packet);  
  33.                 reply.setChildElement(packet.getChildElement().createCopy());  
  34.                 reply.setError(PacketError.Condition.internal_server_error);  
  35.                 Log.error(e.getMessage(), e);  
  36.             }  

这里出现的PacketError这样的消息包错误对象,在以后的源码中会继续写博客,希望大家多多关照... 

注册表单配置

在上面讲解用户注册的流程的时候,相信大家都看到了,用户注册的时候服务端会发送很长的一连串表单要客户端来填写。实际上吗,在openfire官方也不一定确定,世界各地使用注册到底需要哪些属性,所以给出来的注册模板可能会不适合所有的人来使用。那么怎么来修改注册模板呢。

然而在openfire控制管理台,也提供了用户注册表单的配置。在管理台目录:

用户/组->RegistrationProperties这个目录下。截图如下:

技术分享

 

Ok,这里面有很多关于注册的使用相关信息。如下几个:

1、RegistrationSettings

2、RegistrationNotification Contacts

3、WelcomeMessage

4、DefaultGroup

5、Sign-Up PageHeader Text

在本章就,本人则挑几个给大家一起分析下。其余的大家可以自己跟踪下源码。

 

Registration Settings

RegistrationSettings是设置注册配置信息。

技术分享

 

这里有禁用使用email,当然也可以添加使用,其他的属性,比如地址,邮编等。

 

 

Welcome Message

注册完成后,反馈欢迎词等等。

 

Default Group

注册完成后,添加到哪些默认组。关于默认组,以后再说。

 

.......

好了,注册这块就不谈了。

(转)OpenFire源码学习之六:用户注册

标签:bar   tail   ict   server   comm   strong   compress   qname   ros   

原文地址:http://www.cnblogs.com/wangle1001986/p/7161436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!