本文整理汇总了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;
}
示例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();
}
}
示例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);
}
}
示例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;
}
示例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);
}
示例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);
}
}
}
示例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;
}
示例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);
}
}
}
示例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;
}
}
}
}
示例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的会话
}
}
示例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);
}
}
}
示例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;
}
示例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;
}
示例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;
}
示例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());
}