当前位置: 首页>>代码示例>>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;未经允许,请勿转载。