5、通过服务器登录后,查询用户的Token。
这是我们最希望看到的方案。通过构建一个Servlet或者Web Service,只要提供用户名,就可以通过查询获取Token,这种方式也让你担心其安全性,但让我们放心的是并不是所有运行该代码的客户端,都允许得到Token,该客户端需要得到服务器端的的信任。
配置如下:
打开Sametime 服务器的stconfig.nsf

配置CommunityConnectivity的Community Trusted IPS,增加Sametime信任的服务器IP

完整代码如下:
(需要StComm.jar包支持)
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import com.lotus.sametime.core.comparch.*; import com.lotus.sametime.core.types.*; import com.lotus.sametime.community.*; import com.lotus.sametime.core.util.connection.*; import com.lotus.sametime.token.SATokenService; import com.lotus.sametime.token.Token; import com.lotus.sametime.token.TokenEvent; import com.lotus.sametime.token.TokenServiceListener; /* * * 实现一个Servlet,通过Servelt来查询用户的Token,也可以实现Web Service来获取登录用户的Token。 */ public class SametimeServlet extends HttpServlet implements LoginListener, TokenServiceListener { /* * * * */ public void serviceAvailable(TokenEvent arg0) { } // Sametime 的Session private STSession m_session; // 作为应用来登录服务器,而不是使用用户名和密码来登录服务器 private ServerAppService m_saService; //设置sametime服务器 private String host="testsametime.csvw.com"; // 使用这个服务生成Token private SATokenService m_tokenService; //查询的用户名 private STUser m_user; //查询生成的Token private Token m_token; private String m_userName; private boolean success;//表明是否成功登录服务器 public void init(ServletConfig config) throws ServletException { super.init(config); // Init Sametime log("initialize sametime"); initSametime(); // Wait until initialization complete try { synchronized (this) { wait(); } } catch (InterruptedException e) { } log(">> Sametime servlet was initialized successfully"); } /** * 初始化Sametime服务器信息 */ void initSametime() { success=false; // Create the session, load components and start it try { m_session = new STSession("" + this); loadComponents(); m_session.start(); } catch (DuplicateObjectException e) { e.printStackTrace(); } m_saService = (ServerAppService) m_session .getCompApi(ServerAppService.COMP_NAME); m_tokenService = (SATokenService) m_session .getCompApi(SATokenService.COMP_NAME); m_tokenService.addTokenServiceListener(this); // 作为应用登录服务器r loginToServer(host); } private void loadComponents() { String[] compNames = { "com.lotus.sametime.community.STBase", "com.lotus.sametime.token.SATokenComp" }; m_session.loadComponents(compNames); } void loginToServer(String serverName) { m_saService.addLoginListener(this); //设置登录Sametime类似是应用的方式 short loginType = STUserInstance.LT_SERVER_APP; //创建一个与Sametime服务器的连接 Connection[] connections = { new SocketConnection(8082, 17000)}; m_saService.setConnectivity(connections); log(">> login to sametime server name is " + serverName); //以应用的方式登录Sametime服务器,而且不注销 m_saService.loginAsServerApp(serverName, loginType, "SametimeServlet", null); } public void loggedIn(LoginEvent event) { log(">> loggedIn to Sametime"); success=true; synchronized (this) { log(">> wake up call"); notify(); } } public void loggedOut(LoginEvent event) { log("***** loggedOut from Sametime reason = " + event.getReason()); synchronized (this) { log(">> wake up call"); notify(); } } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req, res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { m_userName = req.getParameter("username"); log(">> do post user name is " + m_userName); //判断服务器是否登录成功,登陆成功才可以执行查询操作 if(success) { //对UID需要根据DN组装 m_user = new STUser(new STId(m_userName, m_userName), "", ""); //请求生成Token m_tokenService.generateToken(m_user); synchronized (this) { try { log(">> waiting..."); //等待服务器返回Token ,10秒超时 wait(10000); log(">> waked up..."); } catch (InterruptedException e) { } } } log(">> creating response..."); //返回得到的登录名和Token createResponse(res); } public void tokenGenerated(TokenEvent event) { log(">> token generated"); //成功获取Token m_token = event.getToken(); synchronized (this) { log(">> wake up call"); //通知完成Token生成 notify(); } } public void generateTokenFailed(TokenEvent event) { log("***** Storage request failed = " + event.getReason()); synchronized (this) { log(">> wake up call"); notify(); } } private void createResponse(HttpServletResponse res) throws IOException { PrintWriter pw = res.getWriter(); if(success) { //在 pw.print(m_token.getLoginName()+"|"); pw.print(m_token.getTokenString()); } } /* * 释放资源 */ public void destroy() { m_session.stop(); m_session.unloadSession(); super.destroy(); } }
这个Servlet会通过我们访问如下URL:
http://localhost:8083/servlet/ SametimeServlet?username=uid=zhangshan,cn=users,dc=your.com,dc=com
返回:
uid=zhangshan,cn=users,dc=your.com,dc=com|(DDFDFdFDDGGDGDGDG)
注:登录名和Token,并且使用”|”分开