本文整理匯總了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;
}