本文整理汇总了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);
}
}