當前位置: 首頁>>代碼示例>>Java>>正文


Java Session.setAttribute方法代碼示例

本文整理匯總了Java中org.apache.shiro.session.Session.setAttribute方法的典型用法代碼示例。如果您正苦於以下問題:Java Session.setAttribute方法的具體用法?Java Session.setAttribute怎麽用?Java Session.setAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.shiro.session.Session的用法示例。


在下文中一共展示了Session.setAttribute方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: 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;
}
 
開發者ID:ChinaLHR,項目名稱:JavaQuarkBBS,代碼行數:28,代碼來源:MyShiroRealm.java

示例2: 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();
    }
}
 
開發者ID:ZhuXS,項目名稱:Spring-Shiro-Spark,代碼行數:24,代碼來源:ShiroRealm.java

示例3: 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);
    }
}
 
開發者ID:wjggwm,項目名稱:webside,代碼行數:23,代碼來源:RedisShiroSessionRepository.java

示例4: 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;
}
 
開發者ID:ju5t1nhhh,項目名稱:CMSdemo,代碼行數:19,代碼來源:MyShiroRealm.java

示例5: saveRequest

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
protected void saveRequest(ServletRequest request, String backUrl,
		String fallbackUrl) {
	Subject subject = SecurityUtils.getSubject();
	Session session = subject.getSession();
	HttpServletRequest httpRequest = WebUtils.toHttp(request);
	session.setAttribute("authc.fallbackUrl", fallbackUrl);
	SavedRequest savedRequest = new Saved2Request(httpRequest, backUrl);
	session.setAttribute(WebUtils.SAVED_REQUEST_KEY, savedRequest);
}
 
開發者ID:jiangzongyao,項目名稱:kettle_support_kettle8.0,代碼行數:10,代碼來源:Authentication2Filter.java

示例6: setSession

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
/**
 * 將一些數據放到ShiroSession中,以便於其它地方使用
 * 比如Controller,使用時直接用HttpSession.getAttribute(key)就可以取到
 */
public static final void setSession(Object key, Object value) {
	Subject currentUser = SecurityUtils.getSubject();
	if (null != currentUser) {
		Session session = currentUser.getSession();
		if (null != session) {
			session.setAttribute(key, value);
		}
	}
}
 
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:14,代碼來源:WebUtil.java

示例7: isAccessAllowed

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
    Subject subject = getSubject(request, response);
    Session session = subject.getSession();
    // 判斷請求類型
    String upmsType = PropertiesFileUtil.getInstance("zheng-upms-client").get("zheng.upms.type");
    session.setAttribute(UpmsConstant.UPMS_TYPE, upmsType);
    if ("client".equals(upmsType)) {
        return validateClient(request, response);
    }
    if ("server".equals(upmsType)) {
        return subject.isAuthenticated();
    }
    return false;
}
 
開發者ID:ChangyiHuang,項目名稱:shuzheng,代碼行數:16,代碼來源:UpmsAuthenticationFilter.java

示例8: setSession

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
/**
 * 將一些數據放到ShiroSession中,以便於其它地方使用
 * 
 * @see 比如Controller,使用時直接用HttpSession.getAttribute(key)就可以取到
 */
public static final void setSession(Object key, Object value) {
	Subject currentUser = SecurityUtils.getSubject();
	if (null != currentUser) {
		Session session = currentUser.getSession();
		if (null != session) {
			session.setAttribute(key, value);
		}
	}
}
 
開發者ID:guokezheng,項目名稱:automat,代碼行數:15,代碼來源:WebUtil.java

示例9: 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;
			}
		}
	}
}
 
開發者ID:butter-fly,項目名稱:belling-admin,代碼行數:36,代碼來源:UserOnlineServiceImpl.java

示例10: kickoutBySessionId

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
@Override
public void kickoutBySessionId(String sessionId) {
	Session session = getSessionBysessionId(sessionId);
	if (null != session) {
		session.setAttribute("kickout", true); // 標記為已下線
		session.setTimeout(0L); //設置session立即失效,即將其踢出係統break;
		// session.stop(); //銷毀Shiro的會話
	}
}
 
開發者ID:butter-fly,項目名稱:belling-admin,代碼行數:10,代碼來源:UserOnlineServiceImpl.java

示例11: setSession

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
/**
 * 
 * @param key
 * @param value
 */
private void setSession(Object key, Object value){
	Subject subject = SecurityUtils.getSubject();
	if(subject != null){
		Session session = subject.getSession();
		if(session != null){
			session.setAttribute(key, value);
		}
	}
}
 
開發者ID:xujeff,項目名稱:tianti,代碼行數:15,代碼來源:ShiroDBRealm.java

示例12: kickoutUser

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
/**
 * 
 */
public boolean kickoutUser(String sessionId) {
	boolean flag = true;
	try {
		String[] sessionIds = null;
		if (sessionId.indexOf(",") == -1) {
			sessionIds = new String[] { sessionId };
		} else {
			sessionIds = sessionId.split(",");
		}
		for (String id : sessionIds) {
			// 獲取用戶Session
			Session session = shiroSessionDao.readSession(id);
			SessionStatus sessionStatus = new SessionStatus();
			// 是否踢出 true:有效,false:踢出。
			sessionStatus.setStatus(Boolean.FALSE);
			// 更新Session
			session.setAttribute(SESSION_STATUS, sessionStatus);
			shiroSessionDao.update(session);
		}
	} catch (Exception e) {
		flag = false;
		logger.error("踢出用戶:{} 異常:{}", sessionId, e);
		throw new ServiceException(e);
	}
	return flag;
}
 
開發者ID:wjggwm,項目名稱:webside,代碼行數:30,代碼來源:UserSessionServiceImpl.java

示例13: isAccessAllowed

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
@Override
protected boolean isAccessAllowed(ServletRequest req, ServletResponse resp, Object mappedValue) {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;
    // 是登錄頁麵 則直接返回true
    if (isLoginRequest(request, response)) {
        return true;
    }
    // 獲取主題
    Subject subject = getSubject(request, response);
    //如果用戶【已認證】 直接跳過
    if (subject.isAuthenticated()) {
        return true;
    }
    // 如果用戶【未認證】 但是用戶使用【記住我】功能
    if (subject.isRemembered()) {
        // 獲取rememberMe的用戶名稱
        Object principal = subject.getPrincipal();
        if (null != principal) {
            //從session中 獲取登錄用戶信息
            Session session = subject.getSession(true);
            MMSnsCommonUserEntity commonUser = (MMSnsCommonUserEntity) session.getAttribute(MMSnsCommonUserEntity.MMSNS_COMMON_USER);
            //如果session不保存用戶信息;則從數據庫中獲取
            if (commonUser == null) {
                List<MMSnsCommonUserEntity> commonUsers = commonUserService.getCommonUserByCondition(principal.toString(), principal.toString(),principal.toString(),null,null);
                if (commonUser != null && commonUsers.size() > 0) {
                    session.setAttribute(MMSnsCommonUserEntity.MMSNS_COMMON_USER, commonUsers.get(0));
                }
            }
            return true;
        }
    }
    return false;
}
 
開發者ID:babymm,項目名稱:mmsns,代碼行數:35,代碼來源:MMSnsRememberAuthenticationFilter.java

示例14: isAccessAllowed

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
    Subject subject = getSubject(request, response);
    Session session = subject.getSession();
    // 判斷請求類型
    String upmsType = PropertiesFileUtil.getInstance("lambo-upms-client").get("lambo.upms.type");
    session.setAttribute(UpmsConstant.UPMS_TYPE, upmsType);
    if ("client".equals(upmsType)) {
        return validateClient(request, response);
    }
    if ("server".equals(upmsType)) {
        return subject.isAuthenticated();
    }
    return false;
}
 
開發者ID:sunzhen086,項目名稱:lambo,代碼行數:16,代碼來源:UpmsAuthenticationFilter.java

示例15: onStart

import org.apache.shiro.session.Session; //導入方法依賴的package包/類
@Override
public void onStart(Session session) {
    session.setAttribute(Constants.WEBTHEME, "default");
    logger.info("創建了一個Session連接:[" + session.getId() + "]");
    redisTemplate.opsForSet().add(Constants.ALLUSER_NUMBER, session.getId());
}
 
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:7,代碼來源:SessionListener.java


注:本文中的org.apache.shiro.session.Session.setAttribute方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。