com.ruoyi.framework.shiro.session.OnlineSession Java Examples

The following examples show how to use com.ruoyi.framework.shiro.session.OnlineSession. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: supplierShop   Author: guchengwuyue   File: AsyncFactory.java    License: MIT License 6 votes vote down vote up
/**
 * 同步session到数据库
 * 
 * @param session 在线用户会话
 * @return 任务task
 */
public static TimerTask syncSessionToDb(final OnlineSession session)
{
    return new TimerTask()
    {
        @Override
        public void run()
        {
            SysUserOnline online = new SysUserOnline();
            online.setSessionId(String.valueOf(session.getId()));
            online.setDeptName(session.getDeptName());
            online.setLoginName(session.getLoginName());
            online.setStartTimestamp(session.getStartTimestamp());
            online.setLastAccessTime(session.getLastAccessTime());
            online.setExpireTime(session.getTimeout());
            online.setIpaddr(session.getHost());
            online.setLoginLocation(AddressUtils.getRealAddressByIP(session.getHost()));
            online.setBrowser(session.getBrowser());
            online.setOs(session.getOs());
            online.setStatus(session.getStatus());
            SpringUtils.getBean(ISysUserOnlineService.class).saveOnline(online);

        }
    };
}
 
Example #2
Source Project: supplierShop   Author: guchengwuyue   File: SysShiroService.java    License: MIT License 6 votes vote down vote up
public Session createSession(SysUserOnline userOnline)
{
    OnlineSession onlineSession = new OnlineSession();
    if (StringUtils.isNotNull(userOnline))
    {
        onlineSession.setId(userOnline.getSessionId());
        onlineSession.setHost(userOnline.getIpaddr());
        onlineSession.setBrowser(userOnline.getBrowser());
        onlineSession.setOs(userOnline.getOs());
        onlineSession.setDeptName(userOnline.getDeptName());
        onlineSession.setLoginName(userOnline.getLoginName());
        onlineSession.setStartTimestamp(userOnline.getStartTimestamp());
        onlineSession.setLastAccessTime(userOnline.getLastAccessTime());
        onlineSession.setTimeout(userOnline.getExpireTime());
    }
    return onlineSession;
}
 
Example #3
Source Project: supplierShop   Author: guchengwuyue   File: SysUserOnlineController.java    License: MIT License 6 votes vote down vote up
@RequiresPermissions("monitor:online:forceLogout")
@Log(title = "在线用户", businessType = BusinessType.FORCE)
@PostMapping("/forceLogout")
@ResponseBody
public AjaxResult forceLogout(String sessionId)
{
    SysUserOnline online = userOnlineService.selectOnlineById(sessionId);
    if (sessionId.equals(ShiroUtils.getSessionId()))
    {
        return error("当前登陆用户无法强退");
    }
    if (online == null)
    {
        return error("用户已下线");
    }
    OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
    if (onlineSession == null)
    {
        return error("用户已下线");
    }
    onlineSession.setStatus(OnlineStatus.off_line);
    onlineSessionDAO.update(onlineSession);
    online.setStatus(OnlineStatus.off_line);
    userOnlineService.saveOnline(online);
    return success();
}
 
Example #4
Source Project: ruoyiplus   Author: kongshanxuelin   File: AsyncFactory.java    License: MIT License 6 votes vote down vote up
/**
 * 同步session到数据库
 * 
 * @param session 在线用户会话
 * @return 任务task
 */
public static TimerTask syncSessionToDb(final OnlineSession session)
{
    return new TimerTask()
    {
        @Override
        public void run()
        {
            SysUserOnline online = new SysUserOnline();
            online.setSessionId(String.valueOf(session.getId()));
            online.setDeptName(session.getDeptName());
            online.setLoginName(session.getLoginName());
            online.setStartTimestamp(session.getStartTimestamp());
            online.setLastAccessTime(session.getLastAccessTime());
            online.setExpireTime(session.getTimeout());
            online.setIpaddr(session.getHost());
            online.setLoginLocation(AddressUtils.getRealAddressByIP(session.getHost()));
            online.setBrowser(session.getBrowser());
            online.setOs(session.getOs());
            online.setStatus(session.getStatus());
            SpringUtils.getBean(ISysUserOnlineService.class).saveOnline(online);

        }
    };
}
 
Example #5
Source Project: ruoyiplus   Author: kongshanxuelin   File: SysShiroService.java    License: MIT License 6 votes vote down vote up
public Session createSession(SysUserOnline userOnline)
{
    OnlineSession onlineSession = new OnlineSession();
    if (StringUtils.isNotNull(userOnline))
    {
        onlineSession.setId(userOnline.getSessionId());
        onlineSession.setHost(userOnline.getIpaddr());
        onlineSession.setBrowser(userOnline.getBrowser());
        onlineSession.setOs(userOnline.getOs());
        onlineSession.setDeptName(userOnline.getDeptName());
        onlineSession.setLoginName(userOnline.getLoginName());
        onlineSession.setStartTimestamp(userOnline.getStartTimestamp());
        onlineSession.setLastAccessTime(userOnline.getLastAccessTime());
        onlineSession.setTimeout(userOnline.getExpireTime());
    }
    return onlineSession;
}
 
Example #6
Source Project: ruoyiplus   Author: kongshanxuelin   File: SysUserOnlineController.java    License: MIT License 6 votes vote down vote up
@RequiresPermissions("monitor:online:forceLogout")
@Log(title = "在线用户", businessType = BusinessType.FORCE)
@PostMapping("/forceLogout")
@ResponseBody
public AjaxResult forceLogout(String sessionId)
{
    SysUserOnline online = userOnlineService.selectOnlineById(sessionId);
    if (sessionId.equals(ShiroUtils.getSessionId()))
    {
        return error("当前登陆用户无法强退");
    }
    if (online == null)
    {
        return error("用户已下线");
    }
    OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
    if (onlineSession == null)
    {
        return error("用户已下线");
    }
    onlineSession.setStatus(OnlineStatus.off_line);
    online.setStatus(OnlineStatus.off_line);
    userOnlineService.saveOnline(online);
    return success();
}
 
Example #7
Source Project: RuoYi   Author: lerry903   File: AsyncFactory.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 同步session到数据库
 *
 * @param session 在线用户会话
 * @return 任务task
 */
public static TimerTask syncSessionToDb(final OnlineSession session) {
    return new TimerTask() {
        @Override
        public void run() {
            SysUserOnline online = new SysUserOnline();
            online.setSessionId(String.valueOf(session.getId()));
            online.setDeptName(session.getDeptName());
            online.setLoginName(session.getLoginName());
            online.setStartTimestamp(session.getStartTimestamp());
            online.setLastAccessTime(session.getLastAccessTime());
            online.setExpireTime(session.getTimeout());
            online.setIpaddr(session.getHost());
            online.setLoginLocation(AddressUtils.getRealAddressByIp(session.getHost()));
            online.setBrowser(session.getBrowser());
            online.setOs(session.getOs());
            online.setStatus(session.getStatus());
            SpringUtils.getBean(ISysUserOnlineService.class).saveOnline(online);

        }
    };
}
 
Example #8
Source Project: RuoYi   Author: lerry903   File: SysUserOnlineController.java    License: Apache License 2.0 6 votes vote down vote up
private String logout(String sessionId) {
    SysUserOnline online = userOnlineService.selectOnlineById(sessionId);
    if (sessionId.equals(ShiroUtils.getSessionId())) {
        return "当前登陆用户无法强退";
    }
    if (online == null) {
        return "用户已下线";
    }
    OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
    if (onlineSession == null) {
        return "用户已下线";
    }
    onlineSession.setStatus(OnlineStatus.OFF_LINE);
    online.setStatus(OnlineStatus.OFF_LINE);
    userOnlineService.saveOnline(online);
    return null;
}
 
Example #9
Source Project: supplierShop   Author: guchengwuyue   File: OnlineWebSessionManager.java    License: MIT License 5 votes vote down vote up
@Override
public void setAttribute(SessionKey sessionKey, Object attributeKey, Object value) throws InvalidSessionException
{
    super.setAttribute(sessionKey, attributeKey, value);
    if (value != null && needMarkAttributeChanged(attributeKey))
    {
        OnlineSession session = getOnlineSession(sessionKey);
        session.markAttributeChanged();
    }
}
 
Example #10
Source Project: supplierShop   Author: guchengwuyue   File: OnlineWebSessionManager.java    License: MIT License 5 votes vote down vote up
@Override
public Object removeAttribute(SessionKey sessionKey, Object attributeKey) throws InvalidSessionException
{
    Object removed = super.removeAttribute(sessionKey, attributeKey);
    if (removed != null)
    {
        OnlineSession s = getOnlineSession(sessionKey);
        s.markAttributeChanged();
    }

    return removed;
}
 
Example #11
Source Project: supplierShop   Author: guchengwuyue   File: OnlineSessionFilter.java    License: MIT License 5 votes vote down vote up
/**
 * 表示是否允许访问;mappedValue就是[urls]配置中拦截器参数部分,如果允许访问返回true,否则false;
 */
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
        throws Exception
{
    Subject subject = getSubject(request, response);
    if (subject == null || subject.getSession() == null)
    {
        return true;
    }
    Session session = onlineSessionDAO.readSession(subject.getSession().getId());
    if (session != null && session instanceof OnlineSession)
    {
        OnlineSession onlineSession = (OnlineSession) session;
        request.setAttribute(ShiroConstants.ONLINE_SESSION, onlineSession);
        // 把user对象设置进去
        boolean isGuest = onlineSession.getUserId() == null || onlineSession.getUserId() == 0L;
        if (isGuest == true)
        {
            SysUser user = ShiroUtils.getSysUser();
            if (user != null)
            {
                onlineSession.setUserId(user.getUserId());
                onlineSession.setLoginName(user.getLoginName());
	onlineSession.setAvatar(user.getAvatar());
                onlineSession.setDeptName(user.getDept().getDeptName());
                onlineSession.markAttributeChanged();
            }
        }

        if (onlineSession.getStatus() == OnlineStatus.off_line)
        {
            return false;
        }
    }
    return true;
}
 
Example #12
Source Project: supplierShop   Author: guchengwuyue   File: SyncOnlineSessionFilter.java    License: MIT License 5 votes vote down vote up
/**
 * 同步会话数据到DB 一次请求最多同步一次 防止过多处理 需要放到Shiro过滤器之前
 */
@Override
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception
{
    OnlineSession session = (OnlineSession) request.getAttribute(ShiroConstants.ONLINE_SESSION);
    // 如果session stop了 也不同步
    // session停止时间,如果stopTimestamp不为null,则代表已停止
    if (session != null && session.getUserId() != null && session.getStopTimestamp() == null)
    {
        onlineSessionDAO.syncToDb(session);
    }
    return true;
}
 
Example #13
Source Project: supplierShop   Author: guchengwuyue   File: SysUserOnlineController.java    License: MIT License 5 votes vote down vote up
@RequiresPermissions("monitor:online:batchForceLogout")
@Log(title = "在线用户", businessType = BusinessType.FORCE)
@PostMapping("/batchForceLogout")
@ResponseBody
public AjaxResult batchForceLogout(@RequestParam("ids[]") String[] ids)
{
    for (String sessionId : ids)
    {
        SysUserOnline online = userOnlineService.selectOnlineById(sessionId);
        if (online == null)
        {
            return error("用户已下线");
        }
        OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
        if (onlineSession == null)
        {
            return error("用户已下线");
        }
        if (sessionId.equals(ShiroUtils.getSessionId()))
        {
            return error("当前登陆用户无法强退");
        }
        onlineSession.setStatus(OnlineStatus.off_line);
        onlineSessionDAO.update(onlineSession);
        online.setStatus(OnlineStatus.off_line);
        userOnlineService.saveOnline(online);
    }
    return success();
}
 
Example #14
Source Project: ruoyiplus   Author: kongshanxuelin   File: OnlineWebSessionManager.java    License: MIT License 5 votes vote down vote up
@Override
public void setAttribute(SessionKey sessionKey, Object attributeKey, Object value) throws InvalidSessionException
{
    super.setAttribute(sessionKey, attributeKey, value);
    if (value != null && needMarkAttributeChanged(attributeKey))
    {
        OnlineSession s = (OnlineSession) doGetSession(sessionKey);
        s.markAttributeChanged();
    }
}
 
Example #15
Source Project: ruoyiplus   Author: kongshanxuelin   File: OnlineWebSessionManager.java    License: MIT License 5 votes vote down vote up
@Override
public Object removeAttribute(SessionKey sessionKey, Object attributeKey) throws InvalidSessionException
{
    Object removed = super.removeAttribute(sessionKey, attributeKey);
    if (removed != null)
    {
        OnlineSession s = (OnlineSession) doGetSession(sessionKey);
        s.markAttributeChanged();
    }

    return removed;
}
 
Example #16
Source Project: ruoyiplus   Author: kongshanxuelin   File: OnlineSessionFilter.java    License: MIT License 5 votes vote down vote up
/**
 * 表示是否允许访问;mappedValue就是[urls]配置中拦截器参数部分,如果允许访问返回true,否则false;
 */
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
        throws Exception
{
    Subject subject = getSubject(request, response);
    if (subject == null || subject.getSession() == null)
    {
        return true;
    }
    Session session = onlineSessionDAO.readSession(subject.getSession().getId());
    if (session != null && session instanceof OnlineSession)
    {
        OnlineSession onlineSession = (OnlineSession) session;
        request.setAttribute(ShiroConstants.ONLINE_SESSION, onlineSession);
        // 把user对象设置进去
        boolean isGuest = onlineSession.getUserId() == null || onlineSession.getUserId() == 0L;
        if (isGuest == true)
        {
            SysUser user = ShiroUtils.getSysUser();
            if (user != null)
            {
                onlineSession.setUserId(user.getUserId());
                onlineSession.setLoginName(user.getLoginName());
                onlineSession.setDeptName(user.getDept().getDeptName());
                onlineSession.markAttributeChanged();
            }
        }

        if (onlineSession.getStatus() == OnlineStatus.off_line)
        {
            return false;
        }
    }
    return true;
}
 
Example #17
Source Project: ruoyiplus   Author: kongshanxuelin   File: SyncOnlineSessionFilter.java    License: MIT License 5 votes vote down vote up
/**
 * 同步会话数据到DB 一次请求最多同步一次 防止过多处理 需要放到Shiro过滤器之前
 */
@Override
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception
{
    OnlineSession session = (OnlineSession) request.getAttribute(ShiroConstants.ONLINE_SESSION);
    // 如果session stop了 也不同步
    // session停止时间,如果stopTimestamp不为null,则代表已停止
    if (session != null && session.getUserId() != null && session.getStopTimestamp() == null)
    {
        onlineSessionDAO.syncToDb(session);
    }
    return true;
}
 
Example #18
Source Project: ruoyiplus   Author: kongshanxuelin   File: SysUserOnlineController.java    License: MIT License 5 votes vote down vote up
@RequiresPermissions("monitor:online:batchForceLogout")
@Log(title = "在线用户", businessType = BusinessType.FORCE)
@PostMapping("/batchForceLogout")
@ResponseBody
public AjaxResult batchForceLogout(@RequestParam("ids[]") String[] ids)
{
    for (String sessionId : ids)
    {
        SysUserOnline online = userOnlineService.selectOnlineById(sessionId);
        if (online == null)
        {
            return error("用户已下线");
        }
        OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getSessionId());
        if (onlineSession == null)
        {
            return error("用户已下线");
        }
        if (sessionId.equals(ShiroUtils.getSessionId()))
        {
            return error("当前登陆用户无法强退");
        }
        onlineSession.setStatus(OnlineStatus.off_line);
        online.setStatus(OnlineStatus.off_line);
        userOnlineService.saveOnline(online);
    }
    return success();
}
 
Example #19
Source Project: RuoYi   Author: lerry903   File: SysShiroService.java    License: Apache License 2.0 5 votes vote down vote up
private Session createSession(SysUserOnline userOnline){
    OnlineSession onlineSession = new OnlineSession();
    if (ObjectUtil.isNotNull(userOnline)){
        onlineSession.setId(userOnline.getSessionId());
        onlineSession.setHost(userOnline.getIpaddr());
        onlineSession.setBrowser(userOnline.getBrowser());
        onlineSession.setOs(userOnline.getOs());
        onlineSession.setDeptName(userOnline.getDeptName());
        onlineSession.setLoginName(userOnline.getLoginName());
        onlineSession.setStartTimestamp(userOnline.getStartTimestamp());
        onlineSession.setLastAccessTime(userOnline.getLastAccessTime());
        onlineSession.setTimeout(userOnline.getExpireTime());
    }
    return onlineSession;
}
 
Example #20
Source Project: RuoYi   Author: lerry903   File: OnlineWebSessionManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setAttribute(SessionKey sessionKey, Object attributeKey, Object value){
    super.setAttribute(sessionKey, attributeKey, value);
    if (value != null && needMarkAttributeChanged(attributeKey)) {
        OnlineSession s = (OnlineSession) doGetSession(sessionKey);
        s.markAttributeChanged();
    }
}
 
Example #21
Source Project: RuoYi   Author: lerry903   File: OnlineWebSessionManager.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Object removeAttribute(SessionKey sessionKey, Object attributeKey){
    Object removed = super.removeAttribute(sessionKey, attributeKey);
    if (removed != null) {
        OnlineSession s = (OnlineSession) doGetSession(sessionKey);
        s.markAttributeChanged();
    }

    return removed;
}
 
Example #22
Source Project: RuoYi   Author: lerry903   File: OnlineSessionFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 表示是否允许访问;mappedValue就是[urls]配置中拦截器参数部分,如果允许访问返回true,否则false;
 */
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue){
    Subject subject = getSubject(request, response);
    if (subject == null || subject.getSession() == null) {
        return true;
    }
    Session session = onlineSessionDAO.readSession(subject.getSession().getId());
    if (session instanceof OnlineSession) {
        OnlineSession onlineSession = (OnlineSession) session;
        request.setAttribute(ShiroConstants.ONLINE_SESSION, onlineSession);
        // 把user对象设置进去
        boolean isGuest = onlineSession.getUserId() == null || onlineSession.getUserId() == 0L;
        if (isGuest) {
            SysUser user = ShiroUtils.getSysUser();
            if (user != null) {
                onlineSession.setUserId(user.getUserId());
                onlineSession.setLoginName(user.getLoginName());
                onlineSession.setAvatar(user.getAvatar());
                onlineSession.setDeptName(user.getDept().getDeptName());
                onlineSession.markAttributeChanged();
            }
        }

        return onlineSession.getStatus() != OnlineStatus.OFF_LINE;
    }
    return true;
}
 
Example #23
Source Project: RuoYi   Author: lerry903   File: SyncOnlineSessionFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 同步会话数据到DB 一次请求最多同步一次 防止过多处理 需要放到Shiro过滤器之前
 */
@Override
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue){
    OnlineSession session = (OnlineSession) request.getAttribute(ShiroConstants.ONLINE_SESSION);
    // 如果session stop了 也不同步
    // session停止时间,如果stopTimestamp不为null,则代表已停止
    if (session != null && session.getUserId() != null && session.getStopTimestamp() == null) {
        onlineSessionDAO.syncToDb(session);
    }
    return true;
}
 
Example #24
Source Project: supplierShop   Author: guchengwuyue   File: SysShiroService.java    License: MIT License 2 votes vote down vote up
/**
 * 删除会话
 *
 * @param onlineSession 会话信息
 */
public void deleteSession(OnlineSession onlineSession)
{
    onlineService.deleteOnlineById(String.valueOf(onlineSession.getId()));
}
 
Example #25
Source Project: ruoyiplus   Author: kongshanxuelin   File: SysShiroService.java    License: MIT License 2 votes vote down vote up
/**
 * 删除会话
 *
 * @param onlineSession 会话信息
 */
public void deleteSession(OnlineSession onlineSession)
{
    onlineService.deleteOnlineById(String.valueOf(onlineSession.getId()));
}
 
Example #26
Source Project: RuoYi   Author: lerry903   File: SysShiroService.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * 删除会话
 *
 * @param onlineSession 会话信息
 */
public void deleteSession(OnlineSession onlineSession){
    onlineService.deleteOnlineById(String.valueOf(onlineSession.getId()));
}