本文整理汇总了Java中org.apache.shiro.session.Session.getAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java Session.getAttribute方法的具体用法?Java Session.getAttribute怎么用?Java Session.getAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.shiro.session.Session
的用法示例。
在下文中一共展示了Session.getAttribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveSession
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
/** 保存session */
private void saveSession(String account) {
// 踢出用户
SysSession record = new SysSession();
record.setAccount(account);
List<?> sessionIds = sysSessionService.querySessionIdByAccount(record);
if (sessionIds != null) {
for (Object sessionId : sessionIds) {
record.setSessionId((String) sessionId);
sysSessionService.deleteBySessionId(record);
sessionRepository.delete((String) sessionId);
sessionRepository.cleanupExpiredSessions();
}
}
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
record.setSessionId(session.getId().toString());
String host = (String) session.getAttribute("HOST");
record.setIp(StringUtils.isBlank(host) ? session.getHost() : host);
record.setStartTime(session.getStartTimestamp());
sysSessionService.update(record);
}
示例2: isAccessAllowed
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
@Override
protected boolean isAccessAllowed(ServletRequest request,
ServletResponse response, Object mappedValue) throws Exception {
HttpServletRequest httpRequest = ((HttpServletRequest)request);
String url = httpRequest.getRequestURI();
if(url.startsWith(ShiroUtils.OPENAPI)){
return Boolean.TRUE;
}
Subject subject = getSubject(request, response);
Session session = subject.getSession();
SessionStatus sessionStatus = (SessionStatus) session.getAttribute(UserSessionServiceImpl.SESSION_STATUS);
if (null != sessionStatus && !sessionStatus.isOnline()) {
return Boolean.FALSE;
}
return Boolean.TRUE;
}
示例3: hasLogin
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
@Override
public boolean hasLogin(String account) {
try {
if (Strings.isNullOrEmpty(account)) return true;
Collection<Session> sessions = sessionDAO.getActiveSessions();
for(Session session : sessions){
Object obj = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (null != obj) {
if (account.equals(obj.toString()) && session.getAttribute("kickout") == null) {
return true;
}
}
}
} catch(Exception ex) {
ex.printStackTrace();
}
return false;
}
示例4: 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);
}
}
示例5: isAccessAllowed
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
Session session = getSubject(request, response).getSession(false);
if(session == null) {
return true;
}
boolean forceout = session.getAttribute("FORCE_LOGOUT") == null;
return forceout;
}
示例6: doUpdate
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
protected void doUpdate(Session session) {
if(session != null){
try {
Object principalObj = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if(principalObj != null){
String principal = principalObj.toString();
String cacheName = getActiveSessionsCacheName();
//在cache中建立 key=principal <-> value=sessionId的映射关系,使得根据某个人的会话id清除其会话信息(强制其下线)成为可能
getCacheManager().getCache(cacheName).put(principal, session.getId());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例7: doDelete
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
protected void doDelete(Session session) {
if(session != null){
try {
Object principalObj = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if(principalObj != null){
String principal = principalObj.toString();
String cacheName = getActiveSessionsCacheName();
//删除cache中建立 key=principal <-> value=sessionId的映射关系
getCacheManager().getCache(cacheName).remove(principal);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例8: getCurrentUser
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
/** 获取当前用户 */
public static final Long getCurrentUser() {
Subject currentUser = SecurityUtils.getSubject();
if (null != currentUser) {
try {
Session session = currentUser.getSession();
if (null != session) {
return (Long) session.getAttribute(Constants.CURRENT_USER);
}
} catch (InvalidSessionException e) {
logger.error(e);
}
}
return null;
}
示例9: getCurrentUser
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
/** 获取当前用户 */
public static final Long getCurrentUser() {
Subject currentUser = SecurityUtils.getSubject();
if (null != currentUser) {
try {
Session session = currentUser.getSession();
if (null != session) {
return (Long) session.getAttribute(Constants.CURRENT_USER);
}
} catch (InvalidSessionException e) {
logger.error(e);
}
}
return null;
}
示例10: onlineUserlist
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
/**
* 当前在线用户列表
*/
@RequiresPermissions("adminOnlineUserList")
@RequestMapping("onlineUserList")
public String onlineUserlist(HttpServletRequest request,Model model){
Collection<Session> sessions = sessionDAO.getActiveSessions();
List<User> userList = new ArrayList<User>();
//key:user.id, value:lasttime,即用户最后上线的时间。
Map<Integer, Integer> userMap = new HashMap<Integer, Integer>();
Iterator<Session> it = sessions.iterator();
while(it.hasNext()){
Session session = (Session) it.next();
SimplePrincipalCollection s = (SimplePrincipalCollection) session.getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY");
if(s != null){
ActiveUser activityUser = (ActiveUser) s.getPrimaryPrincipal();
if(activityUser != null){
User user = activityUser.getUser();
if(userMap.get(user.getId()) == null || user.getLasttime() - userMap.get(user.getId()) > 0){
userList.add(user);
userMap.put(user.getId(), user.getLasttime());
}
}
}
}
model.addAttribute("list", userList);
return "iw/admin/user/onlineUserList";
}
示例11: getSessionDTO
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
/**
* 从session中获取UserOnline对象
*
* @param session
* @return
*/
private UserOnlineDTO getSessionDTO(Session session){
if (null == session) {
return null;
}
Object obj = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (null == obj) {
return null;
}
//存储session
UserOnlineDTO userDto = new UserOnlineDTO();
// 登录账号
userDto.setLoginAccount(obj.toString());
//最后一次和系统交互的时间
userDto.setLastAccess(session.getLastAccessTime());
// 开始时间
userDto.setStartTime(session.getStartTimestamp());
//主机的ip地址
userDto.setIp(session.getHost());
//session ID
userDto.setSessionId(session.getId().toString());
//回话到期 ttl(ms)
userDto.setTimeout(session.getTimeout());
//session创建时间
userDto.setStartTime(session.getStartTimestamp());
return userDto;
}
示例12: kickoutByAccount
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
@Override
public void kickoutByAccount(String account) {
if (Strings.isNullOrEmpty(account)) return;
Collection<Session> sessions = sessionDAO.getActiveSessions();
if (sessions.size() <= 0) return;
System.out.println("kickoutByAccount sessions size is :" + sessions.size());
for(Session session : sessions){
Object obj = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (obj != null) {
String tempName = obj.toString();
if (account.equals(tempName)) {
// 会话已失效 但在线列表仍可获取Session会话对象
session.setAttribute("kickout", true); // 标记为已下线
session.setTimeout(0L); //设置session立即失效,即将其踢出系统break;
// session.stop(); //销毁Shiro的会话
// 记录日志
LoginLog log = new LoginLog();
log.setUserId(account);
log.setLoginType((short) 1);
log.setLoginDesc("账号异地登录,被迫强制下线");
log.setIpInfoCountry(null);
log.setIpInfoRegion(null);
log.setIpInfoCity(null);
log.setIpInfoIsp(null);
log.setLoginIp(RequestUtil.getAddr(RequestUtil.getRequest()));
log.setLoginTime(new Timestamp(new Date().getTime()));
// 保存退出日志
loginLogMapper.insert(log);
break;
}
}
}
}
示例13: readSession
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
/**
* 如果session中没有登录信息就调用doReadSession方法从Redis中重读
* session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null 代表没有登录,登录后Shiro会放入该值
* @param sessionId
* @return
* @throws UnknownSessionException
*/
@Override
public Session readSession(Serializable sessionId) throws UnknownSessionException{
Session session = getCachedSession(sessionId);
if(session == null || session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
session = this.doReadSession(sessionId);
if(session == null){
throw new UnknownSessionException("There is no session with id [" + sessionId + "]");
}else {
cache(session,session.getId());
}
}
return session;
}
示例14: getSimplePrincipalCollectionByUserId
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
/**
* 根据ID查询 SimplePrincipalCollection
*
* @param userIds
* 用户ID
* @return
*/
public List<SimplePrincipalCollection> getSimplePrincipalCollectionByUserId(
Long... userIds) {
// 把userIds 转成Set,好判断
Set<Long> idset = new TreeSet<Long>(Arrays.asList(userIds));
// 获取所有session
Collection<Session> sessions = shiroSessionDao.getActiveSessions();
// 定义返回
List<SimplePrincipalCollection> list = new ArrayList<SimplePrincipalCollection>();
for (Session session : sessions) {
// 获取SimplePrincipalCollection
Object obj = session
.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (null != obj && obj instanceof SimplePrincipalCollection) {
// 强转
SimplePrincipalCollection spc = (SimplePrincipalCollection) obj;
// 判断用户,匹配用户ID。
obj = spc.getPrimaryPrincipal();
if (null != obj && obj instanceof UserEntity) {
UserEntity user = (UserEntity) obj;
// 比较用户ID,符合即加入集合
if (idset.contains(user.getId())) {
list.add(spc);
}
}
}
}
return list;
}
示例15: getActiveSessions
import org.apache.shiro.session.Session; //导入方法依赖的package包/类
/**
* 获取活动会话
* @param includeLeave 是否包括离线(最后访问时间大于3分钟为离线会话)
* @param principal 根据登录者对象获取活动会话
* @param filterSession 不为空,则过滤掉(不包含)这个会话。
* @return
*/
@Override
public Collection<Session> getActiveSessions(boolean includeLeave, Object principal, Session filterSession) {
// 如果包括离线,并无登录者条件。
if (includeLeave && principal == null){
return getActiveSessions();
}
Set<Session> sessions = Sets.newHashSet();
for (Session session : getActiveSessions()){
boolean isActiveSession = false;
// 不包括离线并符合最后访问时间小于等于3分钟条件。
if (includeLeave || DateUtils.pastMinutes(session.getLastAccessTime()) <= 3){
isActiveSession = true;
}
// 符合登陆者条件。
if (principal != null){
PrincipalCollection pc = (PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (principal.toString().equals(pc != null ? pc.getPrimaryPrincipal().toString() : StringUtils.EMPTY)){
isActiveSession = true;
}
}
// 过滤掉的SESSION
if (filterSession != null && filterSession.getId().equals(session.getId())){
isActiveSession = false;
}
if (isActiveSession){
sessions.add(session);
}
}
return sessions;
}