openfire使用自定义用户表 - nomousewch的专栏 - 博客频道 - CSDN.NET
在最近的项目中使用openfire服务器实现消息推送功能,如果使用openfire自带的用户表,需要维护两张用户表,不是很方便,如果openfire能够直接使用现有系统的用户表就方便多了,而事实上openfire已经为我们考虑到了这一点,我们只需要做一些相关配置就可以了。
- 原理
研究了下openfire的源码,发现实现此点的关键在于data provider的应用,简单来说,就是在程序中需要用到持久化数据(比如用户名、部门名这些,可以储存在数据库中,也可以储存在xml文件或者ldap目录服务中)的地方,用一个Provider作为获取数据的统一接口,针对不同的数据源(可以是数据库的不同表,也可以是nosql、xml等其它数据源)进行相应的实现,比如其中AuthProvider接口(负责用户验证),它有两个不同的实现DefaultAuthProvider和JDBCAuthProvider
,前者是默认的openfire自带表,而后者则是根据我们自定义的数据表,我们将默认的DefaultAuthProvider配置为JDBCAuthProvider ,并添加一些配置文本,就可以进行数据源的切换了。
- 具体配置
进入openfire管理控制台-服务器-服务管理器-系统属性
增加以下键值对,或者在数据库中直接更新数据表
- --设置JDBC连接方式
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcProvider.connectionString',
- 'jdbc:oracle:thin:@10.35.246.178:1521:orcl');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcProvider.driver', 'oracle.jdbc.driver.OracleDriver');
- --设置管理员账号
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('admin.authorizedJIDs', 'admin@localhost');
- --自定义用户认证
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcAuthProvider.useConnectionProvider', 'true');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcAuthProvider.passwordSQL', 'select ''123456'' as pwd from
- t_sys_user t where t.code=?');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcAuthProvider.passwordType', 'plain');
- --自定义用户管理
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcUserProvider.allUsersSQL', 'select code from t_sys_user
- t');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcUserProvider.nameField', 'name');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcUserProvider.searchSQL', 'select code from t_sys_user t
- where code=?');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcUserProvider.useConnectionProvider', 'true');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcUserProvider.emailField', 'email');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcUserProvider.userCountSQL', 'select count(*) from
- t_sys_user t');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcUserProvider.loadUserSQL', 'select t.name,t.email,t.code
- from t_sys_user t where t.code=?');
- insert into OFPROPERTY (NAME, PROPVALUE)
- values ('jdbcUserProvider.usernameField', 'code');
- --使用不同的数据源
- update OFPROPERTY set
- PROPVALUE='org.jivesoftware.openfire.user.JDBCUserProvider'
- where NAME='provider.user.className';
- update OFPROPERTY set
- PROPVALUE='org.jivesoftware.openfire.auth.JDBCAuthProvider'
- where NAME='provider.auth.className';
- commit;
--设置JDBC连接方式 insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcProvider.connectionString', 'jdbc:oracle:thin:@10.35.246.178:1521:orcl'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcProvider.driver', 'oracle.jdbc.driver.OracleDriver'); --设置管理员账号 insert into OFPROPERTY (NAME, PROPVALUE) values ('admin.authorizedJIDs', 'admin@localhost'); --自定义用户认证 insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcAuthProvider.useConnectionProvider', 'true'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcAuthProvider.passwordSQL', 'select ''123456'' as pwd from t_sys_user t where t.code=?'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcAuthProvider.passwordType', 'plain'); --自定义用户管理 insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.allUsersSQL', 'select code from t_sys_user t'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.nameField', 'name'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.searchSQL', 'select code from t_sys_user t where code=?'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.useConnectionProvider', 'true'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.emailField', 'email'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.userCountSQL', 'select count(*) from t_sys_user t'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.loadUserSQL', 'select t.name,t.email,t.code from t_sys_user t where t.code=?'); insert into OFPROPERTY (NAME, PROPVALUE) values ('jdbcUserProvider.usernameField', 'code'); --使用不同的数据源 update OFPROPERTY set PROPVALUE='org.jivesoftware.openfire.user.JDBCUserProvider' where NAME='provider.user.className'; update OFPROPERTY set PROPVALUE='org.jivesoftware.openfire.auth.JDBCAuthProvider' where NAME='provider.auth.className'; commit;
这里只用到了部分验证和用户管理相关的配置,更多的配置需要查看源码来获取