public Response reLogin() throws IOException { // initTestToken(); // tk = "tk00000013"; UserToken userToken = TokenManager.getInstance().getToken(tk); if (userToken == null && tk != null) { OfflineUserToken offlineUserToken = OfflineTokenManager.getInstance().getToken(tk); // 离线不超过一个小时可以再次登录 if (offlineUserToken != null && System.currentTimeMillis() - offlineUserToken.getLogoutTime() < 12 * 60 * 60 * 1000) { userToken = new UserToken(); userToken.setUserId(offlineUserToken.getUserId()); userToken.setPartnerUserId(offlineUserToken.getPartnerUserId()); userToken.setServerId(offlineUserToken.getServerId()); userToken.setPartnerId(offlineUserToken.getPartnerId()); userToken.setToken(offlineUserToken.getToken()); TokenManager.getInstance().setToken(userToken.getToken(), userToken); // 从离线那清掉 OfflineTokenManager.getInstance().removeToken(tk); } } if (userToken == null) { throw new ServiceException( LOGIN_NOT_LOGIN, "userToken为空[" + tk + ":" + getUserId() + ":" + pid + "]"); } // UserToken userToken = checkUserLogin(tk, getUserId(), pid, sid); if (userToken != null) { // 暂时屏蔽 kickoutOtherSession(userToken); UserSessionManager.getInstance() .setUserSession(userToken.getUserId(), request.getSession().getSid()); Session session = ServerConnectorMgr.getInstance().getServerSession("logic"); if (session == null) { LOG.info("无法获取服务器连接session"); } InetSocketAddress address = (InetSocketAddress) request.getSession().getChannel().getRemoteAddress(); Message msg = getProxyMessage(); msg.setAct("User.reLogin"); msg.setAttribate("ut", ut); msg.setAttribate("tk", tk); msg.setAttribate("ip", address.getHostName()); msg.setAttribate("uid", userToken.getUserId()); request.getSession().setAttribute("userToken", userToken); session.send(msg); } return null; }
/** * 根据token判断用户是否有登陆 * * @param token 用户登陆令牌 * @param userId 用户ID * @param partnerId 合作厂商ID * @param serverId 服务器ID * @return */ private UserToken checkUserLogin(String token, String userId, String partnerId, String serverId) { if (Config.isTest) { UserToken userToken = new UserToken(); userToken.setPartnerId(partnerId); userToken.setUserId(userId); userToken.setToken(token); userToken.setServerId(serverId); userToken.setPartnerId(partnerId); return userToken; } if (StringUtils.isBlank(token) || StringUtils.isBlank(userId) || StringUtils.isBlank(partnerId) || StringUtils.isBlank(serverId)) { throw new ServiceException( ServiceReturnCode.PARAM_ERROR, "参数不正确[" + token + ":" + userId + ":" + partnerId + "]"); } UserToken userToken = TokenManager.getInstance().getToken(token); if (userToken == null) { throw new ServiceException( LOGIN_NOT_LOGIN, "userToken为空[" + token + ":" + userId + ":" + partnerId + "]"); } if (!userId.equals(userToken.getUserId()) || !partnerId.equals(userToken.getPartnerId()) || !serverId.equals(userToken.getServerId())) { throw new ServiceException( LOGIN_NOT_LOGIN, "userToken与缓存不匹配[" + token + ":" + userId + ":" + partnerId + "]"); } return userToken; }
public static void createUserToken() { UserToken ut = new UserToken(); ut.setToken("tktktktktk"); ut.setPartnerUserId("110"); ut.setPartnerId("119"); ut.setServerId("s1"); ut.setUserId("5349499dea96497db4a2e203ef064b15"); TokenManager.getInstance().setToken(ut.getToken(), ut); }
/** * 用户退出,清除userToken、Session并断开连接 * * @return */ public Response logout() { Session session = request.getSession(); UserToken userToken = (UserToken) session.getAttribute("userToken"); // Session logicSession = // ServerConnectorMgr.getInstance().getServerSession("logic"); // if (logicSession != null) { // // 发送退出消息到逻辑服 // Message msg = response.getMessage(); // msg.setAct("User.logout"); // msg.setAttribate("uid", userToken.getUserId()); // try { // logicSession.send(msg); // } catch (IOException e) { // LOG.error(e.getMessage(), e); // } // } // 清楚userToken和session TokenManager.getInstance().removeToken(userToken.getToken()); DefaultSessionManager.getInstance().closeSession(session); return null; }
private void initTestToken() { // TODO will delete Map<String, String> m = new HashMap<String, String>(); m.put("5349499dea96497db4a2e203ef064b15", "tktktktktk"); m.put("bc268cfae4ca46509b79ff849478c2fa", "tk00000001"); m.put("43be66888ff04acca0aaf05a74de3a96", "tk00000002"); m.put("4d5da244bdf94eca848e77db5b269e13", "tk00000003"); m.put("2412841fe4854062b08c56a1b3b1e30b", "tk00000004"); m.put("49ff94dbe9ac4b3f953a1870718cb32b", "tk00000005"); m.put("b5d3b0daae9d43b3bce7dffe7a758233", "tk00000006"); m.put("711615315fbd495a84bebb3f6f0d01da", "tk00000007"); m.put("f9d72437a9d4475aa282c614a7c4644e", "tk00000008"); m.put("559750ae595e4e27a60052ec64cdfc39", "tk00000009"); m.put("2c50ee50fe04461eaed9f12e28806aa5", "tk00000010"); m.put("1085d7bf3cd74d0db3c70d2e7a5052e0", "tk00000011"); m.put("10da636e2fe646c79b0635e93dfdc656", "tk00000012"); m.put("78449f8a35dc4f779dc2cd895ae158f3", "tk00000013"); m.put("ed3973846f24479f96a68a73d3e80cb2", "tk00000014"); m.put("87bfeee8803547a1b2be534845896678", "tk00000015"); m.put("91c86ecd3e994b91b9dd2de196923cf7", "tk00000016"); m.put("1c748c057b45496c886c1a8ac561b086", "tk00000017"); m.put("5b682917bba04a96a2231bd5d19846e1", "tk00000019"); m.put("5b682917bba04a96a2231bd5d19846e2", "tk00000020"); m.put("5b682917bba04a96a2231bd5d19846e3", "tk00000021"); m.put("5b682917bba04a96a2231bd5d19846e4", "tk00000022"); m.put("5b682917bba04a96a2231bd5d19846e5", "tk00000023"); m.put("5b682917bba04a96a2231bd5d19846e6", "tk00000024"); m.put("5b682917bba04a96a2231bd5d19846e7", "tk00000025"); m.put("5b682917bba04a96a2231bd5d19846e8", "tk00000026"); m.put("5b682917bba04a96a2231bd5d19846e9", "tk00000027"); m.put("5b682917bba04a96a2231bd5d19846ea", "tk00000028"); m.put("5b682917bba04a96a2231bd5d19846eb", "tk00000029"); m.put("5b682917bba04a96a2231bd5d19846ec", "tk00000030"); m.put("5b682917bba04a96a2231bd5d19846ed", "tk00000031"); m.put("5b682917bba04a96a2231bd5d19846a0", "tk00000032"); m.put("5b682917bba04a96a2231bd5d19846a1", "tk00000033"); m.put("5b682917bba04a96a2231bd5d19846a2", "tk00000034"); m.put("5b682917bba04a96a2231bd5d19846a3", "tk00000035"); m.put("5b682917bba04a96a2231bd5d19846a4", "tk00000036"); m.put("5b682917bba04a96a2231bd5d19846a5", "tk00000037"); m.put("5b682917bba04a96a2231bd5d19846a6", "tk00000038"); m.put("5b682917bba04a96a2231bd5d19846a7", "tk00000039"); m.put("5b682917bba04a96a2231bd5d19846a8", "tk00000040"); m.put("5b682917bba04a96a2231bd5d19846a9", "tk00000041"); m.put("5b682917bba04a96a2231bd5d19846aa", "tk00000042"); m.put("5b682917bba04a96a2231bd5d19846b1", "tk00000043"); m.put("5b682917bba04a96a2231bd5d19846b2", "tk00000044"); m.put("5b682917bba04a96a2231bd5d19846b3", "tk00000045"); m.put("5b682917bba04a96a2231bd5d19846b4", "tk00000046"); m.put("5b682917bba04a96a2231bd5d19846b5", "tk00000047"); m.put("5b682917bba04a96a2231bd5d19846b6", "tk00000048"); m.put("5b682917bba04a96a2231bd5d19846b7", "tk00000049"); m.put("6bf4204a7dc84bed81d60ef652756818", "tk00000050"); m.put("66aea118d8f44899a513a15afcc66666", "tk00000051"); m.put("5b682917bba04a96a2231bd5d19846b1", "tk00000051"); m.put("5b682917bba04a96a2231bd5d19846b2", "tk00000052"); m.put("5b682917bba04a96a2231bd5d19846b3", "tk00000053"); m.put("5b682917bba04a96a2231bd5d19846b4", "tk00000054"); m.put("5b682917bba04a96a2231bd5d19846b5", "tk00000055"); m.put("5b682917bba04a96a2231bd5d19846b6", "tk00000056"); m.put("5b682917bba04a96a2231bd5d19846b7", "tk00000057"); m.put("5b682917bba04a96a2231bd5d19846b8", "tk00000058"); m.put("5b682917bba04a96a2231bd5d19846b9", "tk00000059"); m.put("5b682917bba04a96a2231bd5d19846c0", "tk00000060"); // m.put(key, value); // m.put(key, value); for (Entry<String, String> entry : m.entrySet()) { String token = entry.getValue(); String uid = entry.getKey(); UserToken ut = new UserToken(); ut.setToken(token); ut.setPartnerId(pid); ut.setServerId(sid); ut.setUserId(uid); TokenManager.getInstance().setToken(ut.getToken(), ut); } }