Java Code Examples for org.apache.shiro.session.Session#getId()
The following examples show how to use
org.apache.shiro.session.Session#getId() .
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 File: JedisSessionDAO.java From Shop-for-JavaWeb with MIT License | 6 votes |
@Override public void delete(Session session) { if (session == null || session.getId() == null) { return; } Jedis jedis = null; try { jedis = JedisUtils.getResource(); jedis.hdel(JedisUtils.getBytesKey(sessionKeyPrefix), JedisUtils.getBytesKey(session.getId().toString())); jedis.del(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId())); logger.debug("delete {} ", session.getId()); } catch (Exception e) { logger.error("delete {} ", session.getId(), e); } finally { JedisUtils.returnResource(jedis); } }
Example 2
Source File: JedisSessionDAO.java From easyweb with Apache License 2.0 | 6 votes |
@Override public void delete(Session session) { if (session == null || session.getId() == null) { return; } Jedis jedis = null; try { jedis = JedisUtils.getResource(); jedis.hdel(JedisUtils.getBytesKey(sessionKeyPrefix), JedisUtils.getBytesKey(session.getId().toString())); jedis.del(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId())); logger.debug("delete {} ", session.getId()); } catch (Exception e) { logger.error("delete {} ", session.getId(), e); } finally { JedisUtils.returnResource(jedis); } }
Example 3
Source File: JedisSessionDAO.java From NutzSite with Apache License 2.0 | 6 votes |
@Override public void delete(Session session) { if (session == null || session.getId() == null) { return; } Jedis jedis = null; try { jedis = jedisAgent.getResource(); jedis.hdel(JedisUtils.getBytesKey(sessionKeyPrefix), JedisUtils.getBytesKey(session.getId().toString())); jedis.del(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId())); logger.debug("delete {} ", session.getId()); } catch (Exception e) { logger.error("delete {} ", session.getId(), e); } finally { Streams.safeClose(jedis); } }
Example 4
Source File: ValidateUserFilter.java From civism-sso with Apache License 2.0 | 6 votes |
@Override protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) { boolean existSession = SecurityUtils.getSubject().isAuthenticated(); if (!existSession) { return false; } else { Session session = SecurityUtils.getSubject().getSession(false); if (session != null) { Serializable id = session.getId(); if (id != null) { if (redisClient.get((String) id) != null) { return true; } } } return false; } }
Example 5
Source File: DefaultWebSessionManager.java From nano-framework with Apache License 2.0 | 6 votes |
@Override protected void onStart(final Session session, final SessionContext context) { if (!WebUtils.isHttp(context)) { LOGGER.debug("SessionContext argument is not HTTP compatible or does not have an HTTP request/response " + "pair. No session ID cookie will be set."); return; } final HttpServletRequest request = WebUtils.getHttpRequest(context); final HttpServletResponse response = WebUtils.getHttpResponse(context); if (isSessionIdCookieEnabled()) { final Serializable sessionId = session.getId(); storeSessionId(sessionId, request, response); } else { LOGGER.debug("Session ID cookie is disabled. No cookie has been set for new session with id {}", session.getId()); } request.removeAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_ID_SOURCE); request.setAttribute(ShiroHttpServletRequest.REFERENCED_SESSION_IS_NEW, Boolean.TRUE); }
Example 6
Source File: ShiroSessionDAO.java From phone with Apache License 2.0 | 6 votes |
@Override protected Serializable doCreate(Session session) { logger.trace("shiro create session start"); super.doCreate(session); //先本地缓存,再存redis RedisClientSupport jedis = SpringBeanUtil.getRedisClientSupport(); if (jedis != null) { Serializable sessionId = generateSessionId(session); logger.trace("cache by jedis,and sessionId is {}",sessionId); assignSessionId(session, sessionId); String key = RedisKeyConfig.getShiroSessionCacheKey(sessionId); String value = SerializableUtils.serialize(session); try { jedis.putValue(key, value,session.getTimeout()/1000,TimeUnit.SECONDS); } catch (InvalidSessionException | CacheAccessException e) { } } return session.getId(); }
Example 7
Source File: CacheSessionDAO.java From NutzSite with Apache License 2.0 | 6 votes |
@Override protected void doUpdate(Session session) { if (session == null || session.getId() == null) { return; } HttpServletRequest request = Mvcs.getReq(); if (request != null){ String uri = Mvcs.getReq().getRequestURI(); // 如果是静态文件,则不更新SESSION if (isStaticFile(uri)){ return; } } super.doUpdate(session); logger.debug("update {} {}", session.getId(), request != null ? request.getRequestURI() : ""); }
Example 8
Source File: RedisSessionDAO.java From nano-framework with Apache License 2.0 | 6 votes |
@Override protected Serializable doCreate(Session session) { initRedisClient(); Serializable sessionId = generateSessionId(session); assignSessionId(session, sessionId); RedisClient client = sessions.values().iterator().next(); switch (persistType) { case SET: String id; client.set((id = sessionName + sessionId), SerializableUtils.encode(session)); client.expire(id, sessionExpire); break; case HSET: client.hset(sessionName, (String) sessionId, SerializableUtils.encode(session)); break; } return session.getId(); }
Example 9
Source File: LimitedMemorySessionDAO.java From centraldogma with Apache License 2.0 | 5 votes |
@Override public Serializable create(Session session) { final SimpleSession simpleSession = ensureSimpleSession(session); final String id = sessionIdGenerator.get(); simpleSession.setId(id); cache.put(id, simpleSession); return session.getId(); }
Example 10
Source File: CacheSessionDAO.java From Shop-for-JavaWeb with MIT License | 5 votes |
@Override protected void doUpdate(Session session) { if (session == null || session.getId() == null) { return; } HttpServletRequest request = Servlets.getRequest(); if (request != null){ String uri = request.getServletPath(); // 如果是静态文件,则不更新SESSION if (Servlets.isStaticFile(uri)){ return; } // 如果是视图文件,则不更新SESSION if (StringUtils.startsWith(uri, Global.getConfig("web.view.prefix")) && StringUtils.endsWith(uri, Global.getConfig("web.view.suffix"))){ return; } // 手动控制不更新SESSION String updateSession = request.getParameter("updateSession"); if (Global.FALSE.equals(updateSession) || Global.NO.equals(updateSession)){ return; } } super.doUpdate(session); logger.debug("update {} {}", session.getId(), request != null ? request.getRequestURI() : ""); }
Example 11
Source File: ShiroRedisSessionDao.java From jee-universal-bms with Apache License 2.0 | 5 votes |
/** * save session * @param session * @throws UnknownSessionException */ private void saveSession(Session session) throws UnknownSessionException{ if(session == null || session.getId() == null){ logger.error("session or session id is null"); return; } byte[] key = getByteKey(session.getId()); byte[] value = SerializationUtils.serialize((Serializable) session); session.setTimeout(sessionTimeout); this.redisManager.set(key, value, sessionTimeout); }
Example 12
Source File: CacheSessionDAO.java From easyweb with Apache License 2.0 | 5 votes |
@Override protected void doDelete(Session session) { if (session == null || session.getId() == null) { return; } super.doDelete(session); logger.debug("delete {} ", session.getId()); }
Example 13
Source File: RedisSessionDAO.java From mumu with Apache License 2.0 | 5 votes |
@Override public void delete(Session session) { logger.info("delete session "+session.toString()); if(session == null || session.getId() == null){ logger.error("session or session id is null"); return; } jedisClient.del(this.getByteKey(session.getId())); }
Example 14
Source File: CacheSessionDAO.java From Shop-for-JavaWeb with MIT License | 5 votes |
@Override protected Serializable doCreate(Session session) { HttpServletRequest request = Servlets.getRequest(); if (request != null){ String uri = request.getServletPath(); // 如果是静态文件,则不创建SESSION if (Servlets.isStaticFile(uri)){ return null; } } super.doCreate(session); logger.debug("doCreate {} {}", session, request != null ? request.getRequestURI() : ""); return session.getId(); }
Example 15
Source File: RedisSessionDAO.java From shiro-redis with MIT License | 5 votes |
@Override public void delete(Session session) { if (session == null || session.getId() == null) { logger.error("session or session id is null"); return; } try { redisManager.del(keySerializer.serialize(getRedisSessionKey(session.getId()))); } catch (SerializationException e) { logger.error("delete session error. session id=" + session.getId()); } }
Example 16
Source File: RedisCacheSessionDAO.java From civism-sso with Apache License 2.0 | 5 votes |
@Override protected void doUpdate(Session session) { if (session == null || session.getId() == null) { throw new NullPointerException("session is empty"); } super.doUpdate(session); redisClient.set(SsoConstants.REDIS_KEY + session.getId(), SerializeUtil.serialize(session)); }
Example 17
Source File: CacheSessionDAO.java From NutzSite with Apache License 2.0 | 5 votes |
@Override protected Serializable doCreate(Session session) { HttpServletRequest request = Mvcs.getReq(); if (request != null){ String uri = Mvcs.getReq().getRequestURI(); // 如果是静态文件,则不创建SESSION if (isStaticFile(uri)){ return null; } } super.doCreate(session); logger.debug("doCreate {} {}", session, request != null ? request.getRequestURI() : ""); return session.getId(); }
Example 18
Source File: KeepOneUserFilter.java From jsets-shiro-spring-boot-starter with Apache License 2.0 | 4 votes |
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { Subject subject = getSubject(request, response); if (!subject.isAuthenticated() && !subject.isRemembered()) { return this.respondLogin(request, response); } String account = (String) subject.getPrincipal(); String loginedSessionId = this.cacheDelegator.getKeepUser(account); Session loginedSession = null; Session currentSession = subject.getSession(); String currentSessionId = (String) currentSession.getId(); if(currentSessionId.equals(loginedSessionId)) { return true; } else if (Strings.isNullOrEmpty(loginedSessionId)){ this.cacheDelegator.putKeepUser(account, currentSessionId); return true; } else if (null==currentSession.getAttribute(ShiroProperties.ATTRIBUTE_SESSION_KICKOUT)) { this.cacheDelegator.putKeepUser(account, currentSessionId); try{ loginedSession = this.sessionManager.getSession(new DefaultSessionKey(loginedSessionId)); if(null != loginedSession){ loginedSession.setAttribute(ShiroProperties.ATTRIBUTE_SESSION_KICKOUT,Boolean.TRUE); } } catch(SessionException e){ LOGGER.warn(e.getMessage()); } } if (null!=currentSession.getAttribute(ShiroProperties.ATTRIBUTE_SESSION_KICKOUT)) { subject.logout(); String loginedHost = ""; Date loginedTime = null; if(null != loginedSession){ loginedHost = loginedSession.getHost(); loginedTime = loginedSession.getStartTimestamp(); } this.authListenerManager.onKeepOneKickout(request, account, loginedHost, loginedTime); return this.respondRedirect(request, response,this.properties.getKickoutUrl()); } return true; }
Example 19
Source File: JedisSessionDAO.java From NutzSite with Apache License 2.0 | 4 votes |
@Override public void update(Session session) throws UnknownSessionException { if (session == null || session.getId() == null) { return; } HttpServletRequest request = Mvcs.getReq(); if (request != null){ String uri = request.getServletPath(); // 如果是静态文件,则不更新SESSION if (isStaticFile(uri)){ return; } // 手动控制不更新SESSION // if (Global.NO.equals(request.getParameter("updateSession"))){ // return; // } } Jedis jedis = null; try { jedis = jedisAgent.getResource(); // 获取登录者编号 PrincipalCollection pc = (PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY); String principalId = pc != null ? pc.getPrimaryPrincipal().toString() : StringUtils.EMPTY; jedis.hset(sessionKeyPrefix, session.getId().toString(), principalId + "|" + session.getTimeout() + "|" + session.getLastAccessTime().getTime()); jedis.set(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId()), JedisUtils.toBytes(session)); // 设置超期时间 int timeoutSeconds = (int)(session.getTimeout() / 1000); jedis.expire((sessionKeyPrefix + session.getId()), timeoutSeconds); logger.debug("update {} {}", session.getId(), request != null ? request.getRequestURI() : ""); } catch (Exception e) { logger.error("update {} {}", session.getId(), request != null ? request.getRequestURI() : "", e); } finally { Streams.safeClose(jedis); } }
Example 20
Source File: JedisSessionDAO.java From Shop-for-JavaWeb with MIT License | 4 votes |
@Override public void update(Session session) throws UnknownSessionException { if (session == null || session.getId() == null) { return; } HttpServletRequest request = Servlets.getRequest(); if (request != null){ String uri = request.getServletPath(); // 如果是静态文件,则不更新SESSION if (Servlets.isStaticFile(uri)){ return; } // 如果是视图文件,则不更新SESSION if (StringUtils.startsWith(uri, Global.getConfig("web.view.prefix")) && StringUtils.endsWith(uri, Global.getConfig("web.view.suffix"))){ return; } // 手动控制不更新SESSION if (Global.NO.equals(request.getParameter("updateSession"))){ return; } } Jedis jedis = null; try { jedis = JedisUtils.getResource(); // 获取登录者编号 PrincipalCollection pc = (PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY); String principalId = pc != null ? pc.getPrimaryPrincipal().toString() : StringUtils.EMPTY; jedis.hset(sessionKeyPrefix, session.getId().toString(), principalId + "|" + session.getTimeout() + "|" + session.getLastAccessTime().getTime()); jedis.set(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId()), JedisUtils.toBytes(session)); // 设置超期时间 int timeoutSeconds = (int)(session.getTimeout() / 1000); jedis.expire((sessionKeyPrefix + session.getId()), timeoutSeconds); logger.debug("update {} {}", session.getId(), request != null ? request.getRequestURI() : ""); } catch (Exception e) { logger.error("update {} {}", session.getId(), request != null ? request.getRequestURI() : "", e); } finally { JedisUtils.returnResource(jedis); } }