Przeglądaj źródła

显示在线、离线、踢离三种session状态,踢离后提示并跳转登录页

shuzheng 9 lat temu
rodzic
commit
9a5f4305c3

+ 7 - 0
zheng-upms/zheng-upms-server/src/main/java/com/zheng/upms/server/controller/SSOController.java

@@ -7,6 +7,8 @@ import com.zheng.upms.common.constant.UpmsResultConstant;
 import com.zheng.upms.dao.model.UpmsSystemExample;
 import com.zheng.upms.rpc.api.UpmsSystemService;
 import com.zheng.upms.rpc.api.UpmsUserService;
+import com.zheng.upms.server.shiro.UpmsSession;
+import com.zheng.upms.server.shiro.UpmsSessionDao;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.apache.commons.lang.BooleanUtils;
@@ -60,6 +62,9 @@ public class SSOController extends BaseController {
 	@Autowired
 	UpmsUserService upmsUserService;
 
+	@Autowired
+	UpmsSessionDao upmsSessionDao;
+
 	@ApiOperation(value = "认证中心首页")
 	@RequestMapping(value = "/index", method = RequestMethod.GET)
 	public String index(HttpServletRequest request) throws Exception {
@@ -140,6 +145,8 @@ public class SSOController extends BaseController {
 		}
 		// serverSessionId
 		String serverSessionId = subject.getSession().getId().toString();
+		// 更新session状态
+		upmsSessionDao.updateStatus(serverSessionId, UpmsSession.OnlineStatus.on_line);
 		// 默认验证帐号密码正确,创建token
 		String token = UUID.randomUUID().toString();
 		// 全局会话sessionId

+ 1 - 1
zheng-upms/zheng-upms-server/src/main/java/com/zheng/upms/server/shiro/UpmsSession.java

@@ -25,7 +25,7 @@ public class UpmsSession extends SimpleSession {
     private String userAgent;
 
     // 在线状态
-    private OnlineStatus status = OnlineStatus.on_line;
+    private OnlineStatus status = OnlineStatus.off_line;
 
     public String getUserAgent() {
         return userAgent;

+ 16 - 0
zheng-upms/zheng-upms-server/src/main/java/com/zheng/upms/server/shiro/UpmsSessionDao.java

@@ -61,6 +61,10 @@ public class UpmsSessionDao extends EnterpriseCacheSessionDAO {
     @Override
     protected void doUpdate(Session session) {
         // 更新session的最后一次访问时间
+        UpmsSession upmsSession = (UpmsSession) session;
+        UpmsSession cacheUpmsSession = (UpmsSession) doReadSession(session.getId());
+        upmsSession.setStatus(cacheUpmsSession.getStatus());
+        upmsSession.setAttribute("FORCE_LOGOUT", cacheUpmsSession.getAttribute("FORCE_LOGOUT"));
         super.doUpdate(session);
         RedisUtil.set((ZHENG_UPMS_SHIRO_SESSION_ID + "_" + session.getId()).getBytes(), sessionToByte(session), (int) session.getTimeout() / 1000);
         _log.debug("[UpmsSessionDao]redis中更新session: sessionId={}, seconds={}", session.getId(), (int) session.getTimeout() / 1000);
@@ -162,6 +166,18 @@ public class UpmsSessionDao extends EnterpriseCacheSessionDAO {
         return sessionIds.length;
     }
 
+    /**
+     * 更改在线状态
+     * @param sessionId
+     * @param onlineStatus
+     */
+    public void updateStatus(Serializable sessionId, UpmsSession.OnlineStatus onlineStatus) {
+        UpmsSession session = (UpmsSession) doReadSession(sessionId);
+        session.setStatus(onlineStatus);
+        super.doUpdate(session);
+        RedisUtil.set((ZHENG_UPMS_SHIRO_SESSION_ID + "_" + session.getId()).getBytes(), sessionToByte(session), (int) session.getTimeout() / 1000);
+    }
+
     // 把Object对象转化为byte保存到redis中
     public byte[] sessionToByte(Object session) {
         ByteArrayOutputStream bo = new ByteArrayOutputStream();

+ 13 - 1
zheng-upms/zheng-upms-server/src/main/webapp/WEB-INF/jsp/manage/session/index.jsp

@@ -56,10 +56,22 @@ $(function() {
 			{field: 'expired', title: '是否过期', align: 'center'},
 			{field: 'host', title: '访问者IP', align: 'center'},
 			{field: 'userAgent', title: '用户标识', align: 'center'},
-			{field: 'status', title: '状态', align: 'center'}
+			{field: 'status', title: '状态', align: 'center', formatter: 'statusFormatter'}
 		]
 	});
 });
+// 格式化状态
+function statusFormatter(value, row, index) {
+	if (value == 'on_line') {
+		return '<span class="label label-success">在线</span>';
+	}
+	if (value == 'off_line') {
+		return '<span class="label label-default">离线</span>';
+	}
+	if (value == 'force_logout') {
+		return '<span class="label label-danger">踢离</span>';
+	}
+}
 // 强制退出
 var forceoutDialog;
 function forceoutAction() {

+ 4 - 0
zheng-upms/zheng-upms-server/src/main/webapp/WEB-INF/jsp/sso/login.jsp

@@ -94,6 +94,10 @@
             }
         });
     }
+    <c:if test="${param.forceLogout == 1}">
+    alert('您已被强制下线!');
+    top.location.href = '${basePath}/sso/login';
+    </c:if>
 </script>
 </body>
 </html>