导读

Tomcat对于J2EE或Java web开发者而言绝不陌生,但说到Realm,可能有些人不太清楚甚至没有听说过,那么到底什么是Realm?简单一句话就是:Realm是Tomcat中为web应用程序提供访问认证和角色管理的机制。配置了Realm,你就不需要在程序中写web应用登陆验证代码,不需要费力的管理用户角色,甚至不需要你自己写登陆界面。因此,使用Realm可以减轻开发者不少编程和管理负担。下面从几个方面简单介绍Tomcat Realm,为Realm学习者提供一个入门级教程。

目录

正文

1. 什么是Realm?

Realm,中文可以翻译为“域”,是一个存储用户名,密码以及和用户名相关联的角色的”数据库”,用户名和密码用来验证用户对一个或多个web应用程序的有效性。你可以将Realm看做Unix系统里的group组概念,因为访问应用程序中特定资源的权限是被授予了拥有特殊角色的用户,而不是相关的用户名。通过用户名相关联,一个用户可以有任意数量的角色。

尽管Servlet规范描述了一个可以让应用程序声明它们安全性要求(在web.xml部署描述符里)的机制,但是并没有的API来定义一个基于servlet容器和其相关用户角色之间的接口。然而在许多情况下,最好能把一个servlet容器和那些已经存在的认证数据库或机制“连接”起来。因此,Tomcat定义了一个Java接口(org.apache.catalina.Realm),它可以通过"插件"的形式来实现这种连接。

因此,可以通过现有数据库里的用户名、密码以及角色来配置Tomcat,从而来支持容器管理的安全性(container managed security)。如果你使用一个网络程序,而这个程序里包括了一个或多个元素,以及一个定义用户怎样认证他们自己的元素,那你就需要设置这些Realm。

总结:说的简单点就是Realm类似于Unix里面的group。在Unix中,一个group对应着系统的一组资源,某个group不能访问不属于它的资源。Tomcat用Realm来将不同的应用(类似系统资源)赋给不同的用户(类似group),没有权限的用户则不能访问相关的应用。

2. 如何配置使用Tomcat自带的Realm?

Tomcat 7中提供了六种标准Realm,用来支持与各个认证信息来源的连接:
* JDBCRealm - 通过JDBC驱动来访问贮存在关系数据库里的认证信息。
* DataSourceRealm - 通过一个叫做JNDI JDBC 的数据源(DataSource)来访问贮存在关系数据库里的认证信息。
* UserDatabaseRealm - 通过一个叫做UserDatabase JNDI 的数据源来访问认证信息,该数据源通过XML文件(conf/tomcat-users.xml)来进行备份使用。
* JNDIRealm - 通过JNDI provider来访问贮存在基于LDAP(轻量级目录访问协议)的目录服务器里的认证信息。
* MemoryRealm - 访问贮存在电脑内存里的认证信息,它是通过一个XML文件(conf/tomcat-users.xml)来进行初始化的。
* JAASRealm - 使用 Java Authentication & Authorization Service (JAAS)访问认证信息。

在使用标准Realm之前,弄懂怎样配置一个Realm是很重要的。通常,你需要把一个XML元素加入到你的conf/server.xml配置文件中,它看起来像这样:

  1. <Realm className="... class name for this implementation"
  2. ... other attributes for this implementation .../>

元素可以被套嵌在下列任何一个Container元素里面。这个Realm元素所处的位置直接影响到这个Realm的作用范围(比如,哪些web应用程序会共享相同的认证信息):

在元素里边 - 这个域(Realm)将会被所有虚拟主机上的所有网络程序共享,除非它被嵌套在下级 或元素里的Realm元素覆盖。

在元素里边 - 这个域(Realm)将会被该虚拟主机上所有的网络程序所共享,除非它被嵌套在下级元素里的Realm元素覆盖。

在元素里边 - 这个域(Realm)只被该网络程序使用。

如何使用各个标准Realm也很简单,官方文档也讲的非常详细,具体可以参考我下面给出的几个参考资料。下面重点讲如何配置使用我们自定义的Realm。

3. 如何配置使用我们自定义的Realm?

虽然Tomcat自带的这六种Realm大部分情况下都能满足我们的需求,但也有特殊需求Tomcat不能满足的时候,比如我最近的一个需求就是:**我的用户和密码信息存储在LDAP中,但用户角色却存储在关系数据库(PostgreSQL)中,那么如何认证呢?**

我们知道Tomcat自带的JNDIRealm可以实现LDAP认证,JDBCRealm可以实现关系数据库认证,那么我们可不可以首先通过LDAP认证,认证通过后,到数据库中读取角色信息呢?答案是肯定的,就是自定义Realm实现我们的需求。我们所需要做的就是:

  • 实现org.apache.catalina.Realm接口;
  • 把编译过的Realm放到 $CATALINA_HOME/lib里边;
  • 像上面配置标准realm一样在server.xml文件中声明你的realm;
  • 在MBeans描述符里声明你的realm。

下面我具体的以我自己的需求作为例子向大家演示如何自定义Realm并成功配置使用。

需求:自定义一个Realm,使得能够像JNDIRealm一样可以实现LDAP认证,又像JDBCRealm一样可以从数据库中读取我们用户的角色信息进行认证。

3.1 实现org.apache.catalina.Realm接口

从需求上看似乎我们可以将Tomcat自带的JNDIRealm和JDBCRealm结合起来,各取所需,形成我们自己的Realm。是的,的确可以这样,因此我们首先需要下载Tomcat的源码,找到这两个Realm的具体实现代码,基本看懂后,提取出我们所需要的部分进行重构形成自己的Realm。比如我自定义的Realm中所需要的实例变量有以下这些:
```java
// -----------------------------------------------Directory Server Instance Variables

  1. /**
  2. * The type of authentication to use.
  3. */
  4. protected String authentication = null;
  5. /**
  6. * The connection username for the directory server we will contact.
  7. */
  8. protected String ldapConnectionName = null;
  9. /**
  10. * The connection password for the directory server we will contact.
  11. */
  12. protected String ldapConnectionPassword = null;
  13. /**
  14. * The connection URL for the directory server we will contact.
  15. */
  16. protected String ldapConnectionURL = null;
  17. /**
  18. * The directory context linking us to our directory server.
  19. */
  20. protected DirContext context = null;
  21. /**
  22. * The JNDI context factory used to acquire our InitialContext. By
  23. * default, assumes use of an LDAP server using the standard JNDI LDAP
  24. * provider.
  25. */
  26. protected String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory";
  27. /**
  28. * How aliases should be dereferenced during search operations.
  29. */
  30. protected String derefAliases = null;
  31. /**
  32. * Constant that holds the name of the environment property for specifying
  33. * the manner in which aliases should be dereferenced.
  34. */
  35. public final static String DEREF_ALIASES = "java.naming.ldap.derefAliases";
  36. /**
  37. * The protocol that will be used in the communication with the
  38. * directory server.
  39. */
  40. protected String protocol = null;
  41. /**
  42. * Should we ignore PartialResultExceptions when iterating over NamingEnumerations?
  43. * Microsoft Active Directory often returns referrals, which lead
  44. * to PartialResultExceptions. Unfortunately there's no stable way to detect,
  45. * if the Exceptions really come from an AD referral.
  46. * Set to true to ignore PartialResultExceptions.
  47. */
  48. protected boolean adCompat = false;
  49. /**
  50. * How should we handle referrals? Microsoft Active Directory often returns
  51. * referrals. If you need to follow them set referrals to "follow".
  52. * Caution: if your DNS is not part of AD, the LDAP client lib might try
  53. * to resolve your domain name in DNS to find another LDAP server.
  54. */
  55. protected String referrals = null;
  56. /**
  57. * The base element for user searches.
  58. */
  59. protected String userBase = "";
  60. /**
  61. * The message format used to search for a user, with "{0}" marking
  62. * the spot where the username goes.
  63. */
  64. protected String userSearch = null;
  65. /**
  66. * The MessageFormat object associated with the current
  67. * <code>userSearch</code>.
  68. */
  69. protected MessageFormat userSearchFormat = null;
  70. /**
  71. * Should we search the entire subtree for matching users?
  72. */
  73. protected boolean userSubtree = false;
  74. /**
  75. * The attribute name used to retrieve the user password.
  76. */
  77. protected String userPassword = null;
  78. /**
  79. * A string of LDAP user patterns or paths, ":"-separated
  80. * These will be used to form the distinguished name of a
  81. * user, with "{0}" marking the spot where the specified username
  82. * goes.
  83. * This is similar to userPattern, but allows for multiple searches
  84. * for a user.
  85. */
  86. protected String[] userPatternArray = null;
  87. /**
  88. * The message format used to form the distinguished name of a
  89. * user, with "{0}" marking the spot where the specified username
  90. * goes.
  91. */
  92. protected String ldapUserPattern = null;
  93. /**
  94. * An array of MessageFormat objects associated with the current
  95. * <code>userPatternArray</code>.
  96. */
  97. protected MessageFormat[] userPatternFormatArray = null;
  98. /**
  99. * An alternate URL, to which, we should connect if ldapConnectionURL fails.
  100. */
  101. protected String ldapAlternateURL;
  102. /**
  103. * The number of connection attempts. If greater than zero we use the
  104. * alternate url.
  105. */
  106. protected int connectionAttempt = 0;
  107. /**
  108. * The timeout, in milliseconds, to use when trying to create a connection
  109. * to the directory. The default is 5000 (5 seconds).
  110. */
  111. protected String connectionTimeout = "5000";
  112. // --------------------------------------------------JDBC Instance Variables
  113. /**
  114. * The connection username to use when trying to connect to the database.
  115. */
  116. protected String jdbcConnectionName = null;
  117. /**
  118. * The connection password to use when trying to connect to the database.
  119. */
  120. protected String jdbcConnectionPassword = null;
  121. /**
  122. * The connection URL to use when trying to connect to the database.
  123. */
  124. protected String jdbcConnectionURL = null;
  125. /**
  126. * The connection to the database.
  127. */
  128. protected Connection dbConnection = null;
  129. /**
  130. * Instance of the JDBC Driver class we use as a connection factory.
  131. */
  132. protected Driver driver = null;
  133. /**
  134. * The JDBC driver name to use.
  135. */
  136. protected String jdbcDriverName = null;
  137. /**
  138. * The PreparedStatement to use for identifying the roles for
  139. * a specified user.
  140. */
  141. protected PreparedStatement preparedRoles = null;
  142. /**
  143. * The string manager for this package.
  144. */
  145. protected static final StringManager sm =
  146. StringManager.getManager(Constants.Package);
  147. /**
  148. * The column in the user role table that names a role
  149. */
  150. protected String roleNameCol = null;
  151. /**
  152. * The column in the user role table that holds the user's name
  153. */
  154. protected String userNameCol = null;
  155. /**
  156. * The table that holds the relation between user's and roles
  157. */
  158. protected String userRoleTable = null;
  159. /**
  160. * Descriptive information about this Realm implementation.
  161. */
  162. protected static final String info = "XXXXXX";
  163. /**
  164. * Descriptive information about this Realm implementation.
  165. */
  166. protected static final String name = "XXXRealm";

  1. 可以看出,将JNDIRealm中不需要的role信息去掉,加上JDBCRealm中获取用户role所需要的信息即可。
  2. 然后就是修改JNDIRealm中的认证方法authenticate()为我们自己认证所需要的,也就是将通过LDAP获取role信息的部分改成使用JDBC连接数据库查询获得。代码不是很复杂但有两千多行,这里就不贴出来了,有需要的可以在下面回复邮箱,我可以发送给你们。
  3. <h3 id="3.2">3.2 Realm编译成.class文件</h3>
  4. 写好自定义Realm过后,就需要编译了,建议单独建个包编译出.class文件,注意只需要.class文件,而该class文件所依赖的Tomcat相关jar包不需要,为什么?因为 $CATALINA_HOME/lib里边已经有了。
  5. <h3 id="3.3">3.3 MBeans描述符里声明你的realm</h3>
  6. 什么是MBeans描述符?[这里](https://tomcat.apache.org/tomcat-7.0-doc/mbeans-descriptor-howto.html)有详细的介绍,简单说就是Tomcat使用JMX MBeans技术来实现Tomcat的远程监控和管理,在每个package下面都必须有一个MBeans描述符配置文件,叫做:mbeans-descriptor.xml,如果你没有给自定义的组件定义该配置文件,就会抛出"ManagedBean is not found"异常。
  7. mbeans-descriptor.xml文件的格式如下:
  8. ```java
  9. <mbean name="XXXRealm"
  10. description="Custom XXXRealm..."
  11. domain="Catalina"
  12. group="Realm"
  13. type="com.myfirm.mypackage.XXXRealm">
  14. <attribute name="className"
  15. description="Fully qualified class name of the managed object"
  16. type="java.lang.String"
  17. writeable="false"/>
  18. <attribute name="debug"
  19. description="The debugging detail level for this component"
  20. type="int"/>
  21. ...
  22. </mbean>

具体的可用参考Tomcat源码中realm包下的mbeans文件。该配置文件十分重要,里面的attribute元素直接对应自定义Realm源码中对应的实例变量字段,也就是我上面贴出来的代码,不过并不是每个实例变量都要添加进来,添加的都是一些重要的需要我们自己在server.xml文件中指明的属性(后面讲),比如JDBC 驱动、数据库用户名、密码、URL等等,这里的attribute名必须与代码中的变量名完全一致,不能出错,否则读取不到相应的值。

下面贴出因上面我的需求所定义的mbeans-descriptor.xml文件:

  1. <?xml version="1.0"?>
  2. <mbeans-descriptors>
  3. <mbean name="CoralXRRealm"
  4. description="Implementation of Realm that works with a directory server accessed via the Java Naming and Directory Interface (JNDI) APIs and JDBC supported database"
  5. domain="Catalina"
  6. group="Realm"
  7. type="org.opencoral.xreport.realm.CoralXRRealm">
  8. <attribute name="className"
  9. description="Fully qualified class name of the managed object"
  10. type="java.lang.String"
  11. writeable="false"/>
  12. <attribute name="ldapConnectionName"
  13. description="The connection username for the directory server we will contact"
  14. type="java.lang.String"/>
  15. <attribute name="ldapConnectionPassword"
  16. description="The connection password for the directory server we will contact"
  17. type="java.lang.String"/>
  18. <attribute name="ldapConnectionURL"
  19. description="The connection URL for the directory server we will contact"
  20. type="java.lang.String"/>
  21. <attribute name="contextFactory"
  22. description="The JNDI context factory for this Realm"
  23. type="java.lang.String"/>
  24. <attribute name="digest"
  25. description="Digest algorithm used in storing passwords in a non-plaintext format"
  26. type="java.lang.String"/>
  27. <attribute name="userBase"
  28. description="The base element for user searches"
  29. type="java.lang.String"/>
  30. <attribute name="userPassword"
  31. description="The attribute name used to retrieve the user password"
  32. type="java.lang.String"/>
  33. <attribute name="ldapUserPattern"
  34. description="The message format used to select a user"
  35. type="java.lang.String"/>
  36. <attribute name="userSearch"
  37. description="The message format used to search for a user"
  38. type="java.lang.String"/>
  39. <attribute name="userSubtree"
  40. description="Should we search the entire subtree for matching users?"
  41. type="boolean"/>
  42. <attribute name="jdbcConnectionName"
  43. description="The connection username to use when trying to connect to the database"
  44. type="java.lang.String"/>
  45. <attribute name="jdbcConnectionPassword"
  46. description="The connection URL to use when trying to connect to the database"
  47. type="java.lang.String"/>
  48. <attribute name="jdbcConnectionURL"
  49. description="The connection URL to use when trying to connect to the database"
  50. type="java.lang.String"/>
  51. <attribute name="jdbcDriverName"
  52. description="The JDBC driver to use"
  53. type="java.lang.String"/>
  54. <attribute name="roleNameCol"
  55. description="The column in the user role table that names a role"
  56. type="java.lang.String"/>
  57. <attribute name="userNameCol"
  58. description="The column in the user role table that holds the user's username"
  59. type="java.lang.String"/>
  60. <attribute name="userRoleTable"
  61. description="The table that holds the relation between user's and roles"
  62. type="java.lang.String"/>
  63. <operation name="start" description="Start" impact="ACTION" returnType="void" />
  64. <operation name="stop" description="Stop" impact="ACTION" returnType="void" />
  65. <operation name="init" description="Init" impact="ACTION" returnType="void" />
  66. <operation name="destroy" description="Destroy" impact="ACTION" returnType="void" />
  67. </mbean>
  68. </mbeans-descriptors>

可以看到与我上面贴出的代码重要实例变量一一对应。

3.4 将Realm编译后的文件打成jar包

具体是:将Realm编译后的.class文件和mbeans-descriptor.xml文件打成jar包放到 $CATALINA_HOME/lib里边。

注意:class文件还是在package里面,不能单独拿出来打包,我们将mbeans-descriptor.xml文件放到.class文件同一目录下,比如我自定义的Realm(比如就叫CustomRealm.java)所在包为:com.ustc.realm.CustomRealm.java,那么.class和mbeans配置文件目录应该为:

|-- com

|-- ustc

|-- realm

|-- CustomRealm.class

|-- mbeans-descriptor.xml

然后命令行进入到com根目录下,使用下面命令打包:

  1. jar cvf customrealm.jar .

customrealm.jar是你自己取的jar名称,第二个参数点.不能丢了,表示对当前的目录进行打包。打包成功后,将customrealm.jar放到$CATALINA_HOME/lib里边即可。

3.5 像配置标准realm一样在server.xml文件中声明你的realm

这个步骤非常关键,打开conf/server.xml文件,搜索Realm,你会看到Tomcat配置文件中自带的Realm声明:
```java

```
当然这个Realm是无效的,因为没有配置完整,只是作为一个示例告诉你要在这里重新配置你自己的Realm,我们将这段Realm声明注释掉,然后声明我们自己的Realm,怎么声明?这要看你的需求了,Tomcat官方文档中有每个标准Realm的详细配置声明,比如JNDIRealm你可以按下面格式声明:

  1. <Realm className="org.apache.catalina.realm.JNDIRealm"
  2. connectionURL="ldap://localhost:389"
  3. userPattern="uid={0},ou=people,dc=mycompany,dc=com"
  4. roleBase="ou=groups,dc=mycompany,dc=com"
  5. roleName="cn"
  6. roleSearch="(uniqueMember={0})"
  7. />

注:这是有关LDAP服务器的配置,关于LDAP如何使用可以查询相关资料,不在本文讨论范围内。

而JDBCRealm你可以这样声明:

  1. <Realm className="org.apache.catalina.realm.JDBCRealm"
  2. driverName="org.gjt.mm.mysql.Driver"
  3. connectionURL="jdbc:mysql://localhost/authority?user=dbuser&password=dbpass"
  4. userTable="users" userNameCol="user_name" userCredCol="user_pass"
  5. userRoleTable="user_roles" roleNameCol="role_name"/>

如果你是使用Tomcat自带的标准Realm,那么只需要修改上面对应的属性值即可。如果是自定义Realm呢?那么我们也需要自定义Realm的声明,以我上面的需求为例,自定义的Realm声明如下:

  1. <Realm className="com.ustc.realm.CustomRealm"
  2. ldapConnectionURL="ldap://server ip:389"
  3. ldapUserPattern="uid={0},ou=people,dc=mycompany"
  4. jdbcDriverName="org.postgresql.Driver"
  5. jdbcConnectionURL="jdbc:postgresql://dbserver ip:port"
  6. jdbcConnectionName="xxx"
  7. jdbcConnectionPassword="xxx" digest="MD5"
  8. userRoleTable="user_roles"
  9. userNameCol="user_name"
  10. roleNameCol="role_name" />

其实就是上面的两个标准Realm声明结合起来,各取所需,这里需要注意两个问题:

  • Realm声明里面的字段名必须与Realm源码及mbeans-descriptor.xml文件中的字段名对应,三者必须一致,否则就读取不到我们在这里设置的具体值;
  • Realm声明里面不能加注释语句,否则会报错。

OK,到此为止,我们自定义Realm的编写与配置就完成了。接下来就是测试了,重启Tomcat,进入登录界面试试吧。

4. Realm的优点

* 安全:对于每个现有的Realm实现里,用户的密码(默认情况下)以明文形式被贮存。在许多环境中,这是不理想的,因为任何人看见了认证数据都可以收集足够信息成功登录,冒充其他用户。为了避免这个问题,标准的实现支持digesting用户密码的概念。这被贮存的密码是被加密后的(以一种不易被转换回去的形式),但是Realm实现还是可以用它来认证。当一个标准的realm通过取得贮存的密码并把它与用户提供的密码值作比较来认证时,你可通过在你的元素上指定digest属性选择digested密码。这个属性的值必须是java.security.MessageDigest class (SHA, MD2, or MD5)支持的digest 算法之一。当你选择这一选项,贮存在Realm里的密码内容必须是这个密码的明文形式,然后被指定的运算法则来加密。当这个Realm的authenticate()方法被调用,用户指定的(明文)密码被相同的运算法来加密,它的结果与Realm返回的值作比较。如果两个值对等的话,就意味着原始密码的明文版与用户提供的一样,所以这个用户就被认证了。
* 调试方便:每个Realm排错和异常信息将由与这个realm的容器(Context, Host,或 Engine)相关的日志配置记录下来,方便我们调试。

参考资料

如何自定义Tomcat Realm实现我们的用户认证需求的更多相关文章

  1. Shrio02 Realm作用、自定义简洁Realm、Realm实现类使用

    1 Realm简介 1.1 Realm作用 shiro最终是通过Realm获取安全数据的(如用户.角色.权限),也就是说认证或者授权都会通过Realm进行数据操作 1.2 Realm接口 1.2.1 ...

  2. Django自定义用户认证

    自定义一个用户认证 详细参考官方文档: https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#django.contrib.au ...

  3. spring 启动找不到shiro中 自定义的realm对应的class问题

    干巴巴盯着项目半天,启动了好多次,每次都是sping报错找不到shiro配置文件中自定义的realm对应的class文件,明明有的,就是找不到. 后来将eclipse对应的jdk1.7 更新为1.8 ...

  4. CMDB资产管理系统开发【day25】:Django 自定义用户认证

    官方文档:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#substituting-a-custom-user-mode ...

  5. 使用django实现自定义用户认证

    参考资料:https://docs.djangoproject.com/en/1.10/topics/auth/customizing/    直接拉到最后看栗子啦 django自定义用户认证(使用自 ...

  6. YII2中自定义用户认证模型,完成登陆和注册

    有些时候我们需要自已定义用户类,操作自已建的用户表,来完成登陆和注册功能. 用户表结构如下,当然可以根据自已的需要添加或删除: CREATE TABLE `tb_user` ( `id` int(11 ...

  7. 项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据

    1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据         2 菜单数据添加 2.1 使用c ...

  8. Django的用户认证组件,自定义分页

    一.用户认证组件 1.auth模块 from django.conrtrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1)authen ...

  9. jq自定义下拉菜单,当用户点击非自身元素(下拉菜单)本身时关闭下拉菜单

    jq自定义下拉菜单,当用户点击非自身元素(下拉菜单)本身时关闭下拉菜单 截图: 代码如下: //关闭用户菜单 $(document).mousedown(function(e){ var _con = ...

随机推荐

  1. Ng第十三课:聚类(Clustering)

    13.1  无监督学习:简介 13.2 K-均值算法 13.3  优化目标 13.4  随机初始化 13.5  选择聚类数 13.1  无监督学习:简介 在这个视频中,将开始介绍聚类算法.这将是一个激 ...

  2. 第一章javascript词法结构笔记摘要

    语法介绍 javascript最流行的脚本语言,用于web和HTML,服务器.pc.移动端 轻量级语言,可以插入HTML页面,由浏览器按编写顺序执行 一.字符集 用Unicode字符集编写,是ASCI ...

  3. (简单匹配)Card Game Cheater -- hdu --1528

    http://acm.hdu.edu.cn/showproblem.php?pid=1528 Card Game Cheater Time Limit: 2000/1000 MS (Java/Othe ...

  4. python常用算法题

    一: #计算平均数,中位数和众数 #coding:utf-8 #计算平均数,中位数和众数 import random lst = [random.randint(0,10) for i in rang ...

  5. RxSwift学习笔记5:Binder

    使用 Binder 创建观察者 //Observable序列(每隔1秒钟发出一个索引数) let scheduleObservable = Observable<Int>.interval ...

  6. GetFileOpenName()、GetFilesavename

    GetFileOpenName() 功能显示打开文件对话框,让用户选择要打开的文件. 语法:GetFileOpenName(title,pathname,filename{,extension{,fi ...

  7. 初次见识结构体与map的区别

    题目  http://vjudge.net/contest/view.action?cid=51142#problem/G 自己做的结构体 #include <iostream>#incl ...

  8. Python 高级编程——单例模式

    单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. 在 Py ...

  9. .NET高级代码审计(第四课) JavaScriptSerializer反序列化漏洞

    0X00 前言 在.NET处理 Ajax应用的时候,通常序列化功能由JavaScriptSerializer类提供,它是.NET2.0之后内部实现的序列化功能的类,位于命名空间System.Web.S ...

  10. 使用Squid部署代理服务

    Squid是Linux系统中最为流行的一款高性能代理服务软件,通常用作Web网站的前置缓存服务,能够代替用户向网站服务器请求页面数据并进行缓存.简单来说,Squid服务程序会按照收到的用户请求向网站源 ...