本文整理匯總了Java中org.apache.shiro.session.Session類的典型用法代碼示例。如果您正苦於以下問題:Java Session類的具體用法?Java Session怎麽用?Java Session使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Session類屬於org.apache.shiro.session包,在下文中一共展示了Session類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getActiveSessions
import org.apache.shiro.session.Session; //導入依賴的package包/類
/**
* 獲取當前所有活躍用戶
*/
@Override
public Collection<Session> getActiveSessions(){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Set<String> keys = jedis.keys(prefix + "*");
if(CollectionUtils.isEmpty(keys)){
return null;
}
List<String> values = jedis.mget(keys.toArray(new String[keys.size()]));
return SerializeUtils.deserializeFromStrings(values);
} catch (Exception e){
logger.warn("統計Session信息失敗", e);
} finally {
jedis.close();
}
return null;
}
示例2: getSession
import org.apache.shiro.session.Session; //導入依賴的package包/類
public static Session getSession(){
try{
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession(false);
if (session == null){
session = subject.getSession();
}
if (session != null){
return session;
}
// subject.logout();
}catch (InvalidSessionException e){
}
return null;
}
示例3: validateSessions
import org.apache.shiro.session.Session; //導入依賴的package包/類
@Override
public void validateSessions() {
Collection<?> activeSessions = getActiveSessions();
if (null != activeSessions && !activeSessions.isEmpty()) {
for (Iterator<?> i$ = activeSessions.iterator(); i$.hasNext();) {
Session session = (Session) i$.next();
try {
SessionKey key = new DefaultSessionKey(session.getId());
validate(session, key);
} catch (InvalidSessionException e) {
if (null != cacheManager) {
SimpleSession s = (SimpleSession) session;
if (null != s.getAttribute(Constant.SESSION_ID))
cacheManager.getCache(null).remove(s.getAttribute(Constant.SESSION_ID));
}
}
}
}
}
示例4: doUpdate
import org.apache.shiro.session.Session; //導入依賴的package包/類
@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() : "");
}
示例5: doGetAuthenticationInfo
import org.apache.shiro.session.Session; //導入依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//獲取用戶的輸入的賬號.
String username = (String)token.getPrincipal();
User user = userService.findByUserId(username);
if(user==null) throw new UnknownAccountException();
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
user, //用戶
user.getPassword(), //密碼
ByteSource.Util.bytes(username),
getName() //realm name
);
// 當驗證都通過後,把用戶信息放在session裏
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("userSession", user);
session.setAttribute("userSessionId", user.getLoginId());
return authenticationInfo;
}
示例6: getActiveSessions
import org.apache.shiro.session.Session; //導入依賴的package包/類
/**
* 獲取會話列表
* @param offset
* @param limit
* @return
*/
public Map getActiveSessions(int offset, int limit) {
Map sessions = new HashMap();
Jedis jedis = RedisUtil.getJedis();
// 獲取在線會話總數
long total = jedis.llen(ZHENG_UPMS_SERVER_SESSION_IDS);
// 獲取當前頁會話詳情
List<String> ids = jedis.lrange(ZHENG_UPMS_SERVER_SESSION_IDS, offset, (offset + limit - 1));
List<Session> rows = new ArrayList<>();
for (String id : ids) {
String session = RedisUtil.get(ZHENG_UPMS_SHIRO_SESSION_ID + "_" + id);
// 過濾redis過期session
if (null == session) {
RedisUtil.lrem(ZHENG_UPMS_SERVER_SESSION_IDS, 1, id);
total = total - 1;
continue;
}
rows.add(SerializableUtil.deserialize(session));
}
jedis.close();
sessions.put("total", total);
sessions.put("rows", rows);
return sessions;
}
示例7: delete
import org.apache.shiro.session.Session; //導入依賴的package包/類
@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);
}
}
示例8: getAllSessions
import org.apache.shiro.session.Session; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public Collection<Session> getAllSessions() {
Collection<Session> sessions = new ArrayList<Session>(0);
try {
Object object = cacheManager.getCache(this.activeSessionsCacheName).getNativeCache();
if(object instanceof Ehcache)
{
Ehcache ehcache = (Ehcache)object;
List<Serializable> keys = ehcache.getKeysWithExpiryCheck();
if (!CollectionUtils.isEmpty(keys)) {
for (Serializable key : keys) {
sessions.add(getSession(key));
}
}
}
} catch (Exception e) {
logger.error("獲取全部session異常", e);
}
return sessions;
}
示例9: doGetAuthenticationInfo
import org.apache.shiro.session.Session; //導入依賴的package包/類
/**
* 認證
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//獲取用戶的輸入的賬號.
String username = (String)token.getPrincipal();
AdminUser user = adminUserService.findByUserName(username);
if(user==null) throw new UnknownAccountException();
if (0==user.getEnable()) {
throw new LockedAccountException(); // 帳號鎖定
}
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
user.getId(), //用戶
user.getPassword(), //密碼
ByteSource.Util.bytes(username),
getName() //realm name
);
// 把用戶信息放在session裏
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("AdminSession", user);
session.setAttribute("AdminSessionId", user.getId());
return authenticationInfo;
}
示例10: doUpdate
import org.apache.shiro.session.Session; //導入依賴的package包/類
@Override
protected void doUpdate(Session session) {
// 如果會話過期/停止 沒必要再更新了
if(session instanceof ValidatingSession && !((ValidatingSession)session).isValid()) {
return;
}
// 更新session的最後一次訪問時間
UpmsSession upmsSession = (UpmsSession) session;
UpmsSession cacheUpmsSession = (UpmsSession) doReadSession(session.getId());
if (null != cacheUpmsSession) {
upmsSession.setStatus(cacheUpmsSession.getStatus());
upmsSession.setAttribute("FORCE_LOGOUT", cacheUpmsSession.getAttribute("FORCE_LOGOUT"));
}
RedisUtil.set(ZHENG_UPMS_SHIRO_SESSION_ID + "_" + session.getId(), SerializableUtil.serialize(session), (int) session.getTimeout() / 1000);
// 更新ZHENG_UPMS_SERVER_SESSION_ID、ZHENG_UPMS_SERVER_CODE過期時間 TODO
LOGGER.debug("doUpdate >>>>> sessionId={}", session.getId());
}
示例11: doReadSession
import org.apache.shiro.session.Session; //導入依賴的package包/類
protected Session doReadSession(Serializable sessionId) {
byte[] sessionKey = buildRedisSessionKey(sessionId);
RedisConnection redisConnection = getRedisConnection();
try {
byte[] value = redisConnection.get(sessionKey);
if (value == null) {
return null;
}
Session session = SerializeUtil.deserialize(value, SimpleSession.class);
return session;
} finally {
redisConnection.close();
}
}
示例12: doGetAuthenticationInfo
import org.apache.shiro.session.Session; //導入依賴的package包/類
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
String userName = token.getUsername();
User user = userDao.findUserByUsername(userName);
UserDto userDto = convertToDto(user);
if(user != null){
//登陸成功
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute("user",userDto);
session.setAttribute("id",user.getId());
session.setAttribute("username",user.getUsername());
session.setAttribute("name",user.getName());
return new SimpleAuthenticationInfo(
userName, //用戶
user.getPassword(), //密碼
getName() //realm name
);
} else {
throw new UnknownAccountException();
}
}
示例13: getActiveSessions
import org.apache.shiro.session.Session; //導入依賴的package包/類
/**
* 獲取會話列表
* @param offset
* @param limit
* @return
*/
public Map getActiveSessions(int offset, int limit) {
Map sessions = new HashMap();
Jedis jedis = RedisUtil.getJedis();
// 獲取在線會話總數
long total = jedis.llen(LAMBO_UPMS_SERVER_SESSION_IDS);
// 獲取當前頁會話詳情
List<String> ids = jedis.lrange(LAMBO_UPMS_SERVER_SESSION_IDS, offset, (offset + limit - 1));
List<Session> rows = new ArrayList<>();
for (String id : ids) {
String session = RedisUtil.get(LAMBO_UPMS_SHIRO_SESSION_ID + "_" + id);
// 過濾redis過期session
if (null == session) {
RedisUtil.lrem(LAMBO_UPMS_SERVER_SESSION_IDS, 1, id);
total = total - 1;
continue;
}
rows.add(SerializableUtil.deserialize(session));
}
jedis.close();
sessions.put("total", total);
sessions.put("rows", rows);
return sessions;
}
示例14: saveSession
import org.apache.shiro.session.Session; //導入依賴的package包/類
@Override
public void saveSession(Session session) {
if (session == null || session.getId() == null)
throw new NullPointerException("session is empty");
try {
byte[] key = generateRedisSessionKey(session.getId()).getBytes();
//不存在才添加。
if(null == session.getAttribute(UserSessionServiceImpl.SESSION_STATUS)){
//session狀態
SessionStatus sessionStatus = new SessionStatus();
session.setAttribute(UserSessionServiceImpl.SESSION_STATUS, sessionStatus);
}
byte[] value = SerializeUtil.serialize(session);
long sessionTimeOut = session.getTimeout() / 1000;
Long expireTime = sessionTimeOut + SESSION_VAL_TIME_SPAN + (5 * 60);
getRedisManager().saveValueByKey(DB_INDEX, key, value, expireTime.intValue());
} catch (Exception e) {
logger.error("save session error,id:"+session.getId(), e);
}
}
示例15: update
import org.apache.shiro.session.Session; //導入依賴的package包/類
@Override
public void update(Session session) {
final String sessionId = ensureStringSessionId(session);
final Path oldPath = sessionId2Path(sessionId);
if (!Files.exists(oldPath)) {
throw new UnknownSessionException(sessionId);
}
try {
final Path newPath = Files.createTempFile(tmpDir, null, null);
Files.write(newPath, serialize(session));
Files.move(newPath, oldPath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new SerializationException(e);
}
}