本文整理汇总了Java中org.apache.shiro.subject.support.DefaultSubjectContext类的典型用法代码示例。如果您正苦于以下问题:Java DefaultSubjectContext类的具体用法?Java DefaultSubjectContext怎么用?Java DefaultSubjectContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DefaultSubjectContext类属于org.apache.shiro.subject.support包,在下文中一共展示了DefaultSubjectContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hasLogin
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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;
}
示例2: doCreate
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的package包/类
@Override
protected Serializable doCreate(Session session) {
Serializable sessionId = session.getId();
if (sessionId == null) {
sessionId = this.generateSessionId(session);
}
this.assignSessionId(session, sessionId);
logger.info("RedisSessionDAO:doCreate=>" + sessionId);
PrincipalCollection existingPrincipals = (PrincipalCollection) session
.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (existingPrincipals == null || CollectionUtils.isEmpty(existingPrincipals)) {
logger.error("PrincipalCollection is null");
return sessionId;
}
doSave(session);
return sessionId;
}
示例3: getOnlineUsers
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的package包/类
@RequestMapping("admin/userManager/userOnlineStore")
public void getOnlineUsers(Model model) {
Iterator<Session> sessions = sessionDao.getActiveSessions().iterator();
ArrayList<OnlineUser> ous = new ArrayList<OnlineUser>();
while (sessions.hasNext()) {
OnlineUser ou = new OnlineUser();
SimpleSession session = (SimpleSession) sessions.next();
ou.setHost(session.getHost());
ou.setId(session.getId().toString());
ou.setLastAccessTime(session.getLastAccessTime());
ou.setStartTime(session.getStartTimestamp());
PrincipalCollection principal = (PrincipalCollection) session
.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (principal != null) {
ShiroUser su = (ShiroUser) principal.getPrimaryPrincipal();
ou.setUserid(su.loginName);
ou.setUsername(su.name);
ou.setLogin(true);
}
ous.add(ou);
}
model.addAttribute("users", ous);
model.addAttribute("total", ous.size());
}
示例4: setAuthProvider
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的package包/类
@Override
public void setAuthProvider(AuthProvider authProvider) {
if (authProvider instanceof ShiroAuthProviderImpl) {
ShiroAuthProviderImpl shiroAuthProvider = (ShiroAuthProviderImpl)authProvider;
this.vertx = shiroAuthProvider.getVertx();
this.securityManager = shiroAuthProvider.getSecurityManager();
// generate the subject back from the provider
SubjectContext subjectContext = new DefaultSubjectContext();
PrincipalCollection coll = new SimplePrincipalCollection(username, shiroAuthProvider.getRealmName());
subjectContext.setPrincipals(coll);
subject = securityManager.createSubject(subjectContext);
} else {
throw new IllegalArgumentException("Not a ShiroAuthProviderImpl");
}
}
示例5: authenticate
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的package包/类
@Override
public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) {
vertx.executeBlocking(fut -> {
SubjectContext subjectContext = new DefaultSubjectContext();
Subject subject = securityManager.createSubject(subjectContext);
String username = authInfo.getString("username");
String password = authInfo.getString("password");
AuthenticationToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);
} catch (AuthenticationException e) {
throw new VertxException(e);
}
fut.complete(new ShiroUser(vertx, securityManager, subject, rolePrefix));
}, resultHandler);
}
示例6: getActiveSessions
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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;
}
示例7: doUpdate
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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();
}
}
}
示例8: doDelete
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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();
}
}
}
示例9: getSessionDTO
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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;
}
示例10: kickoutByAccount
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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;
}
}
}
}
示例11: readSession
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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;
}
示例12: getSimplePrincipalCollectionByUserId
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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;
}
示例13: getActiveSessions
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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;
}
示例14: readSession
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的package包/类
/**
* 重写CachingSessionDAO中readSession方法,如果Session中没有登陆信息就调用doReadSession方法从Redis中重读
* session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null 代表没有登录,登录后Shiro会放入该值
*/
@Override
public Session readSession(@NotNull final 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;
}
示例15: getActiveSessions
import org.apache.shiro.subject.support.DefaultSubjectContext; //导入依赖的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;
}