完成会话管理
This commit is contained in:
parent
e8ed66af80
commit
e0b4fba32b
|
@ -0,0 +1,69 @@
|
|||
package com.zheng.upms.server.controller.manage;
|
||||
|
||||
import com.baidu.unbiz.fluentvalidator.ComplexResult;
|
||||
import com.baidu.unbiz.fluentvalidator.FluentValidator;
|
||||
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
|
||||
import com.zheng.common.base.BaseController;
|
||||
import com.zheng.common.validator.LengthValidator;
|
||||
import com.zheng.upms.common.constant.UpmsResult;
|
||||
import com.zheng.upms.common.constant.UpmsResultConstant;
|
||||
import com.zheng.upms.dao.model.UpmsSystem;
|
||||
import com.zheng.upms.dao.model.UpmsSystemExample;
|
||||
import com.zheng.upms.rpc.api.UpmsSystemService;
|
||||
import com.zheng.upms.server.shiro.UpmsSessionDao;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 会话管理controller
|
||||
* Created by shuzheng on 2017/2/28.
|
||||
*/
|
||||
@Controller
|
||||
@Api(value = "系统管理", description = "系统管理")
|
||||
@RequestMapping("/manage/session")
|
||||
public class UpmsSessionController extends BaseController {
|
||||
|
||||
private static Logger _log = LoggerFactory.getLogger(UpmsSessionController.class);
|
||||
|
||||
@Autowired
|
||||
private UpmsSessionDao sessionDAO;
|
||||
|
||||
@ApiOperation(value = "系统首页")
|
||||
@RequiresPermissions("upms:session:read")
|
||||
@RequestMapping(value = "/index", method = RequestMethod.GET)
|
||||
public String index() {
|
||||
return "/manage/session/index";
|
||||
}
|
||||
|
||||
@ApiOperation(value = "系统列表")
|
||||
@RequiresPermissions("upms:session:read")
|
||||
@RequestMapping(value = "/list", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Object list(
|
||||
@RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
|
||||
@RequestParam(required = false, defaultValue = "10", value = "limit") int limit) {
|
||||
return sessionDAO.getActiveSessions(offset, limit);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "删除系统")
|
||||
@RequiresPermissions("upms:session:delete")
|
||||
@RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public Object delete(@PathVariable("ids") String ids) {
|
||||
int count = sessionDAO.deleteByPrimaryKeys(ids);
|
||||
return new UpmsResult(UpmsResultConstant.SUCCESS, count);
|
||||
}
|
||||
|
||||
}
|
|
@ -9,7 +9,7 @@ import org.apache.shiro.session.mgt.SimpleSession;
|
|||
public class UpmsSession extends SimpleSession {
|
||||
|
||||
public static enum OnlineStatus {
|
||||
on_line("在线"), hidden("隐身"), force_logout("强制退出");
|
||||
on_line("在线"), off_line("离线"), force_logout("强制退出");
|
||||
private final String info;
|
||||
|
||||
private OnlineStatus(String info) {
|
||||
|
@ -27,9 +27,6 @@ public class UpmsSession extends SimpleSession {
|
|||
// 在线状态
|
||||
private OnlineStatus status = OnlineStatus.on_line;
|
||||
|
||||
// 用户登录时系统IP
|
||||
private String systemHost;
|
||||
|
||||
public String getUserAgent() {
|
||||
return userAgent;
|
||||
}
|
||||
|
@ -46,12 +43,4 @@ public class UpmsSession extends SimpleSession {
|
|||
this.status = status;
|
||||
}
|
||||
|
||||
public String getSystemHost() {
|
||||
return systemHost;
|
||||
}
|
||||
|
||||
public void setSystemHost(String systemHost) {
|
||||
this.systemHost = systemHost;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.zheng.upms.server.shiro;
|
||||
|
||||
import com.zheng.common.util.RedisUtil;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.session.Session;
|
||||
import org.apache.shiro.session.mgt.SimpleSession;
|
||||
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
|
||||
|
@ -9,8 +10,7 @@ import org.slf4j.LoggerFactory;
|
|||
import redis.clients.jedis.Jedis;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 基于redis的sessionDao,缓存共享session
|
||||
|
@ -99,13 +99,76 @@ public class UpmsSessionDao extends EnterpriseCacheSessionDAO {
|
|||
|
||||
/**
|
||||
* 获取会话列表
|
||||
* @param page
|
||||
* @param size
|
||||
* @param offset
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
List<Session> getActiveSessions(int page, int size) {
|
||||
// TODO
|
||||
return null;
|
||||
public Map getActiveSessions(int offset, int limit) {
|
||||
Map sessions = new HashMap();
|
||||
|
||||
Jedis jedis = RedisUtil.getJedis();
|
||||
// 获取在线会话总数
|
||||
long total = jedis.llen(ZHENG_UPMS_SHIRO_SESSION_IDS);
|
||||
// 获取当前页会话详情
|
||||
List<String> ids = jedis.lrange(ZHENG_UPMS_SHIRO_SESSION_IDS, offset, (offset + limit - 1));
|
||||
List<Session> rows = new ArrayList<>();
|
||||
for (String id : ids) {
|
||||
byte[] bytes = RedisUtil.get(id.getBytes());
|
||||
if(null != bytes && bytes.length > 0){
|
||||
Session session = (Session) byteToSession(bytes);
|
||||
rows.add(session);
|
||||
}
|
||||
}
|
||||
jedis.close();
|
||||
sessions.put("total", total);
|
||||
sessions.put("rows", rows);
|
||||
return sessions;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会话
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
public int deleteByPrimaryKeys(String ids) {
|
||||
String[] sessionIds = ids.split(",");
|
||||
for (String serverSessionId : sessionIds) {
|
||||
// 清空所有注册的局部会话和token
|
||||
// 当前全局会话token
|
||||
String token = RedisUtil.get(ZHENG_UPMS_SERVER_SESSION_ID + "_" + serverSessionId);
|
||||
// 清除全局会话
|
||||
RedisUtil.remove(ZHENG_UPMS_SERVER_SESSION_ID + "_" + serverSessionId);
|
||||
// 清除token校验值
|
||||
RedisUtil.remove(ZHENG_UPMS_SERVER_TOKEN + "_" + token);
|
||||
// 清除所有局部会话
|
||||
Jedis jedis = RedisUtil.getJedis();
|
||||
Set<String> clientSessionIds = jedis.smembers(ZHENG_UPMS_CLIENT_SESSION_IDS + "_" + token);
|
||||
for (String clientSessionId : clientSessionIds) {
|
||||
jedis.del(ZHENG_UPMS_CLIENT_SESSION_ID + "_" + clientSessionId);
|
||||
jedis.srem(ZHENG_UPMS_CLIENT_SESSION_IDS + "_" + token, clientSessionId);
|
||||
}
|
||||
_log.debug("当前token={},对应的注册系统个数:{}个", token, jedis.scard(ZHENG_UPMS_CLIENT_SESSION_IDS + "_" + token));
|
||||
jedis.close();
|
||||
|
||||
byte[] bytes = RedisUtil.get(serverSessionId.getBytes());
|
||||
if(null != bytes && bytes.length > 0){
|
||||
UpmsSession session = (UpmsSession) byteToSession(bytes);
|
||||
// session.setStatus(UpmsSession.OnlineStatus.force_logout);
|
||||
// super.doUpdate(session);
|
||||
// RedisUtil.set(session.getId().toString().getBytes(), sessionToByte(session));
|
||||
// 删除session
|
||||
super.doDelete(session);
|
||||
RedisUtil.remove(sessionToByte(ZHENG_UPMS_SHIRO_SESSION_ID + "_" + serverSessionId));
|
||||
_log.debug("[UpmsSessionDao]redis中删除session: sessionId={}", session.getId());
|
||||
|
||||
// 维护会话id列表,提供会话分页管理
|
||||
jedis = RedisUtil.getJedis();
|
||||
jedis.lrem(ZHENG_UPMS_SHIRO_SESSION_IDS, 1, serverSessionId);
|
||||
jedis.close();
|
||||
// TODO logout
|
||||
}
|
||||
}
|
||||
return sessionIds.length;
|
||||
}
|
||||
|
||||
// 把Object对象转化为byte保存到redis中
|
||||
|
|
|
@ -22,7 +22,6 @@ public class UpmsSessionFactory implements SessionFactory {
|
|||
if (request != null) {
|
||||
session.setHost(request.getRemoteAddr());
|
||||
session.setUserAgent(request.getHeader("User-Agent"));
|
||||
session.setSystemHost(request.getLocalAddr() + ":" + request.getLocalPort());
|
||||
}
|
||||
}
|
||||
return session;
|
||||
|
|
|
@ -0,0 +1,164 @@
|
|||
<%@ page contentType="text/html; charset=utf-8"%>
|
||||
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
|
||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
|
||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
|
||||
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
|
||||
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
|
||||
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
|
||||
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
|
||||
<!DOCTYPE HTML>
|
||||
<html lang="zh-cn">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>会话管理</title>
|
||||
<jsp:include page="/resources/inc/head.jsp" flush="true"/>
|
||||
</head>
|
||||
<body>
|
||||
<div id="main">
|
||||
<div id="toolbar">
|
||||
<shiro:hasPermission name="upms:session:delete"><a class="waves-effect waves-button" href="javascript:;" onclick="deleteAction()"><i class="zmdi zmdi-comment-alt-text"></i> 删除会话</a></shiro:hasPermission>
|
||||
</div>
|
||||
<table id="table"></table>
|
||||
</div>
|
||||
<jsp:include page="/resources/inc/footer.jsp" flush="true"/>
|
||||
<script>
|
||||
var $table = $('#table');
|
||||
$(function() {
|
||||
// bootstrap table初始化
|
||||
$table.bootstrapTable({
|
||||
url: '${basePath}/manage/session/list',
|
||||
height: getHeight(),
|
||||
striped: true,
|
||||
search: true,
|
||||
showRefresh: true,
|
||||
showColumns: true,
|
||||
minimumCountColumns: 2,
|
||||
clickToSelect: true,
|
||||
detailView: true,
|
||||
detailFormatter: 'detailFormatter',
|
||||
pagination: true,
|
||||
paginationLoop: false,
|
||||
sidePagination: 'server',
|
||||
silentSort: false,
|
||||
smartDisplay: false,
|
||||
escape: true,
|
||||
searchOnEnterKey: true,
|
||||
idField: 'id',
|
||||
maintainSelected: true,
|
||||
toolbar: '#toolbar',
|
||||
columns: [
|
||||
{field: 'ck', checkbox: true},
|
||||
{field: 'id', title: '编号', sortable: true, align: 'center'},
|
||||
{field: 'startTimestamp', title: '创建时间', sortable: true, align: 'center'},
|
||||
{field: 'lastAccessTime', title: '最后访问时间'},
|
||||
{field: 'expired', title: '是否过期', align: 'center'},
|
||||
{field: 'host', title: '访问者IP', align: 'center'},
|
||||
{field: 'userAgent', title: '用户标识', align: 'center'},
|
||||
{field: 'status', title: '状态', align: 'center'}
|
||||
]
|
||||
});
|
||||
});
|
||||
// 删除
|
||||
var deleteDialog;
|
||||
function deleteAction() {
|
||||
var rows = $table.bootstrapTable('getSelections');
|
||||
if (rows.length == 0) {
|
||||
$.confirm({
|
||||
title: false,
|
||||
content: '请至少选择一条记录!',
|
||||
autoClose: 'cancel|3000',
|
||||
backgroundDismiss: true,
|
||||
buttons: {
|
||||
cancel: {
|
||||
text: '取消',
|
||||
btnClass: 'waves-effect waves-button'
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
deleteDialog = $.confirm({
|
||||
type: 'red',
|
||||
animationSpeed: 300,
|
||||
title: false,
|
||||
content: '确认删除该会话吗?',
|
||||
buttons: {
|
||||
confirm: {
|
||||
text: '确认',
|
||||
btnClass: 'waves-effect waves-button',
|
||||
action: function () {
|
||||
var ids = new Array();
|
||||
for (var i in rows) {
|
||||
ids.push(rows[i].id);
|
||||
}
|
||||
$.ajax({
|
||||
type: 'get',
|
||||
url: '${basePath}/manage/session/delete/' + ids.join(","),
|
||||
success: function(result) {
|
||||
if (result.code != 1) {
|
||||
if (result.data instanceof Array) {
|
||||
$.each(result.data, function(index, value) {
|
||||
$.confirm({
|
||||
theme: 'dark',
|
||||
animation: 'rotateX',
|
||||
closeAnimation: 'rotateX',
|
||||
title: false,
|
||||
content: value.errorMsg,
|
||||
buttons: {
|
||||
confirm: {
|
||||
text: '确认',
|
||||
btnClass: 'waves-effect waves-button waves-light'
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
$.confirm({
|
||||
theme: 'dark',
|
||||
animation: 'rotateX',
|
||||
closeAnimation: 'rotateX',
|
||||
title: false,
|
||||
content: result.data.errorMsg,
|
||||
buttons: {
|
||||
confirm: {
|
||||
text: '确认',
|
||||
btnClass: 'waves-effect waves-button waves-light'
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
deleteDialog.close();
|
||||
$table.bootstrapTable('refresh');
|
||||
}
|
||||
},
|
||||
error: function(XMLHttpRequest, textStatus, errorThrown) {
|
||||
$.confirm({
|
||||
theme: 'dark',
|
||||
animation: 'rotateX',
|
||||
closeAnimation: 'rotateX',
|
||||
title: false,
|
||||
content: textStatus,
|
||||
buttons: {
|
||||
confirm: {
|
||||
text: '确认',
|
||||
btnClass: 'waves-effect waves-button waves-light'
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
cancel: {
|
||||
text: '取消',
|
||||
btnClass: 'waves-effect waves-button'
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue