|
|
@@ -13,22 +13,19 @@
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
-package me.zhengjie.modules.security.service;
|
|
|
+package me.zhengjie.security.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.corundumstudio.socketio.SocketIOClient;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import me.zhengjie.base.mq.ClientCache;
|
|
|
-import me.zhengjie.modules.security.config.bean.SecurityProperties;
|
|
|
-import me.zhengjie.modules.security.service.dto.JwtUserDto;
|
|
|
-import me.zhengjie.modules.security.service.dto.OnlineUserDto;
|
|
|
+import me.zhengjie.security.config.bean.SecurityProperties;
|
|
|
+import me.zhengjie.security.service.dto.OnlineUserDto;
|
|
|
import me.zhengjie.utils.*;
|
|
|
-
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import com.corundumstudio.socketio.SocketIOClient;
|
|
|
-import com.google.gson.JsonObject;
|
|
|
-
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
@@ -52,33 +49,27 @@ public class OnlineUserService {
|
|
|
|
|
|
/**
|
|
|
* 保存在线用户信息
|
|
|
- *
|
|
|
- * @param jwtUserDto /
|
|
|
- * @param token /
|
|
|
- * @param request /
|
|
|
+ * @param securityContextUser
|
|
|
+ * @param token
|
|
|
+ * @param request
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public void save(JwtUserDto jwtUserDto, String token, HttpServletRequest request) throws Exception {
|
|
|
- String dept = jwtUserDto.getUser().getDept().getName();
|
|
|
- String ip = StringUtils.getIp(request);
|
|
|
- String browser = StringUtils.getBrowser(request);
|
|
|
- String address = StringUtils.getCityInfo(ip);
|
|
|
- OnlineUserDto onlineUserDto = null;
|
|
|
- String onlineToken = properties.getOnlineKey() + token;
|
|
|
- try {
|
|
|
- onlineUserDto = new OnlineUserDto(jwtUserDto.getUser().getRoles(), jwtUserDto.getUsername(), jwtUserDto.getUser().getNickName(), dept,
|
|
|
- browser, ip, address, EncryptUtils.desEncrypt(token), new Date(), jwtUserDto.getUser().getOrgId(),
|
|
|
- onlineToken);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- }
|
|
|
+ public void save(Object securityContextUser, String token, HttpServletRequest request) throws Exception {
|
|
|
+ OnlineUserDto onlineUserDto = (OnlineUserDto) securityContextUser;
|
|
|
+ onlineUserDto.setKey(EncryptUtils.desEncrypt(token));
|
|
|
+ onlineUserDto.setOnlineToken(properties.getOnlineKey() + token);
|
|
|
+ onlineUserDto.setBrowser(StringUtils.getBrowser(request));
|
|
|
+ onlineUserDto.setIp(StringUtils.getIp(request));
|
|
|
+ onlineUserDto.setAddress(StringUtils.getCityInfo(StringUtils.getIp(request)));
|
|
|
+ onlineUserDto.setLoginTime(new Date());
|
|
|
+
|
|
|
// 先查询是否存在该登录用户,如果存在,则直接剔除
|
|
|
- kickOutForUsername(onlineUserDto.getUserName());
|
|
|
+ kickOutForUsername(onlineUserDto.getUsername());
|
|
|
// TODO 这里只是单机处理,后面修改为多机处理
|
|
|
Map<String, HashMap<UUID, SocketIOClient>> socketServers = ClientCache.getWebSocketMap();
|
|
|
// List<String> names = new ArrayList<String>();
|
|
|
for (String webSocketName : socketServers.keySet()) {
|
|
|
- if (StringUtils.isNotBlank(webSocketName) && onlineUserDto.getUserName().equals(webSocketName)) {
|
|
|
+ if (StringUtils.isNotBlank(webSocketName) && onlineUserDto.getUsername().equals(webSocketName)) {
|
|
|
HashMap<UUID, SocketIOClient> userClient = socketServers.get(webSocketName);
|
|
|
userClient.forEach((uuid, socketIOClient) -> {
|
|
|
JsonObject obj = new JsonObject();
|
|
|
@@ -91,7 +82,7 @@ public class OnlineUserService {
|
|
|
// for (String key : names) {
|
|
|
// socketServers.remove(key);
|
|
|
// }
|
|
|
- redisUtils.set(properties.getOnlineKey() + token, onlineUserDto, properties.getTokenValidityInSeconds() / 1000);
|
|
|
+ redisUtils.set(properties.getOnlineKey() + token, JSON.toJSONString(securityContextUser), properties.getTokenValidityInSeconds() / 1000);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -116,20 +107,36 @@ public class OnlineUserService {
|
|
|
public List<OnlineUserDto> getAll(String filter) {
|
|
|
List<String> keys = redisUtils.scan(properties.getOnlineKey() + "*");
|
|
|
Collections.reverse(keys);
|
|
|
- List<OnlineUserDto> onlineUserDtos = new ArrayList<>();
|
|
|
+ List<OnlineUserDto> onlineUserList = new ArrayList<>();
|
|
|
for (String key : keys) {
|
|
|
- OnlineUserDto onlineUserDto = (OnlineUserDto) redisUtils.get(key);
|
|
|
+ OnlineUserDto onlineUserDto = JSON.parseObject((String) redisUtils.get(key), OnlineUserDto.class);
|
|
|
// onlineUserDto.setToken(key);
|
|
|
if (StringUtils.isNotBlank(filter)) {
|
|
|
if (onlineUserDto.toString().contains(filter)) {
|
|
|
- onlineUserDtos.add(onlineUserDto);
|
|
|
+ onlineUserList.add(onlineUserDto);
|
|
|
}
|
|
|
} else {
|
|
|
- onlineUserDtos.add(onlineUserDto);
|
|
|
+ onlineUserList.add(onlineUserDto);
|
|
|
}
|
|
|
}
|
|
|
- onlineUserDtos.sort((o1, o2) -> o2.getLoginTime().compareTo(o1.getLoginTime()));
|
|
|
- return onlineUserDtos;
|
|
|
+ onlineUserList.sort((o1, o2) -> o2.getLoginTime().compareTo(o1.getLoginTime()));
|
|
|
+ return onlineUserList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询全部登录用户
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<Object> getAllLoginUser() {
|
|
|
+ List<String> keys = redisUtils.scan(properties.getOnlineKey() + "*");
|
|
|
+ Collections.reverse(keys);
|
|
|
+ List<Object> securityContextUsers = new ArrayList<>();
|
|
|
+ for (String key : keys) {
|
|
|
+ securityContextUsers.add(redisUtils.get(key));
|
|
|
+
|
|
|
+ }
|
|
|
+ return securityContextUsers;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -144,7 +151,7 @@ public class OnlineUserService {
|
|
|
|
|
|
/**
|
|
|
* 退出登录
|
|
|
- *
|
|
|
+ *
|
|
|
* @param token /
|
|
|
*/
|
|
|
public void logout(String token) {
|
|
|
@@ -163,7 +170,7 @@ public class OnlineUserService {
|
|
|
List<Map<String, Object>> list = new ArrayList<>();
|
|
|
for (OnlineUserDto user : all) {
|
|
|
Map<String, Object> map = new LinkedHashMap<>();
|
|
|
- map.put("用户名", user.getUserName());
|
|
|
+ map.put("用户名", user.getUsername());
|
|
|
map.put("部门", user.getDept());
|
|
|
map.put("登录IP", user.getIp());
|
|
|
map.put("登录地点", user.getAddress());
|
|
|
@@ -180,8 +187,8 @@ public class OnlineUserService {
|
|
|
* @param key /
|
|
|
* @return /
|
|
|
*/
|
|
|
- public OnlineUserDto getOne(String key) {
|
|
|
- return (OnlineUserDto) redisUtils.get(key);
|
|
|
+ public String getOne(String key) {
|
|
|
+ return (String) redisUtils.get(key);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -195,12 +202,10 @@ public class OnlineUserService {
|
|
|
return;
|
|
|
}
|
|
|
for (OnlineUserDto onlineUserDto : onlineUserDtos) {
|
|
|
- if (onlineUserDto.getUserName().equals(userName)) {
|
|
|
+ if (onlineUserDto.getUsername().equals(userName)) {
|
|
|
try {
|
|
|
String token = EncryptUtils.desDecrypt(onlineUserDto.getKey());
|
|
|
- if (StringUtils.isNotBlank(igoreToken) && !igoreToken.equals(token)) {
|
|
|
- this.kickOut(token);
|
|
|
- } else if (StringUtils.isBlank(igoreToken)) {
|
|
|
+ if (StringUtils.isBlank(igoreToken) || !igoreToken.equals(token)) {
|
|
|
this.kickOut(token);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
@@ -219,7 +224,7 @@ public class OnlineUserService {
|
|
|
public void kickOutForUsername(String username) throws Exception {
|
|
|
List<OnlineUserDto> onlineUsers = getAll(username);
|
|
|
for (OnlineUserDto onlineUser : onlineUsers) {
|
|
|
- if (onlineUser.getUserName().equals(username)) {
|
|
|
+ if (onlineUser.getUsername().equals(username)) {
|
|
|
String token = EncryptUtils.desDecrypt(onlineUser.getKey());
|
|
|
kickOut(token);
|
|
|
}
|