1、自定义访问控制拦截器,继承AccessControlFilter,实现以下三个方法。
abstract boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception;
boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception;
abstract boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception;
2、自定义BeamUserFilter
public class GunsUserFilter extends AccessControlFilter {
private String kickoutUrl; //踢出后到的地址
private boolean kickoutAfter = false; //踢出之前登录的/之后登录的用户 默认踢出之前登录的用户
private int maxSession = 1; //同一个帐号最大会话数 默认1
private org.apache.shiro.cache.Cache<String, Deque<Serializable>> cache;
public void setKickoutUrl(String kickoutUrl) {
this.kickoutUrl = kickoutUrl;
}
public void setKickoutAfter(boolean kickoutAfter) {
this.kickoutAfter = kickoutAfter;
}
public void setMaxSession(int maxSession) {
this.maxSession = maxSession;
}
//设置Cache的key的前缀
public void setCacheManager(CacheManager cacheManager) {
this.cache = cacheManager.getCache("shiro_redis_cache");
}
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
return false;
/*if (isLoginRequest(request, response)) {
return true;
} else {
Subject subject = getSubject(request, response);
// If principal is not null, then the user is known and should be allowed access.
return subject.getPrincipal() != null;
}*/
}
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
HttpServletResponse httpServletResponse = WebUtils.toHttp(response);
/**
* 如果是ajax请求则不进行跳转
*/
if (httpServletRequest.getHeader("x-requested-with") != null
&& httpServletRequest.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
httpServletResponse.setHeader("sessionstatus", "timeout");
return false;
} else {
/*-----------------单机用户唯一登陆------------------------------*/
/**
*
* @date 创建时间:2018年3月27日
* 1.读取当前登录用户名,获取在缓存中的sessionId队列
* 2.判断队列的长度,大于最大登录限制的时候,按踢出规则
* 将之前的sessionId中的session域中存入kickout:true,并更新队列缓存
* 3.判断当前登录的session域中的kickout如果为true,
* 想将其做退出登录处理,然后再重定向到踢出登录提示页面
*/
Subject subject = getSubject(request, response);
if(!subject.isAuthenticated() && !subject.isRemembered()) {
//如果没有登录,直接进行之后的流程
return true;
}
Session session = subject.getSession();
ShiroUser user = ShiroKit.getUser();
String username = user.getAccount();
Serializable sessionId = session.getId();
//读取缓存 没有就存入
Deque<Serializable> deque = cache.get(username);
//如果此用户没有session队列,也就是还没有登录过,缓存中没有
//就new一个空队列,不然deque对象为空,会报空指针
if(deque==null){
deque = new LinkedList<Serializable>();
}
//如果队列里没有此sessionId,且用户没有被踢出;放入队列
if(!deque.contains(sessionId) && session.getAttribute("kickout") == null) {
//将sessionId存入队列
deque.push(sessionId);
//将用户的sessionId队列缓存
cache.put(username, deque);
}
//如果队列里的sessionId数超出最大会话数,开始踢人
while(deque.size() > maxSession) {
Serializable kickoutSessionId = null;
if(kickoutAfter) { //如果踢出后者
kickoutSessionId = deque.removeFirst();
//踢出后再更新下缓存队列
cache.put(username, deque);
} else { //否则踢出前者
kickoutSessionId = deque.removeLast();
//踢出后再更新下缓存队列
cache.put(username, deque);
}
try {
//获取被踢出的sessionId的session对象
Session kickoutSession = SecurityUtils.getSecurityManager().getSession(new DefaultSessionKey(kickoutSessionId));
if(kickoutSession != null) {
//设置会话的kickout属性表示踢出了
kickoutSession.setAttribute("kickout", true);
}
} catch (Exception e) {//ignore exception
}
}
//如果被踢出了,直接退出,重定向到踢出后的地址
if ((Boolean)session.getAttribute("kickout")!=null&&(Boolean)session.getAttribute("kickout") == true) {
System.out.println("被踢出");
//会话被踢出了
try {
//退出登录
subject.logout();
} catch (Exception e) { //ignore
}
saveRequest(request);
//重定向
WebUtils.issueRedirect(request, response, kickoutUrl);
return false;
}
return true;
/*--------------------------------------------------------*/
/**
* 第一次点击页面
*/
/*String referer = httpServletRequest.getHeader("Referer");
if (referer == null) {
System.out.println("进来7");
redirectToLogin(request, response);
return false;
} else {
System.out.println("进来8");
//从别的页面跳转过来的
if (ShiroKit.getSession().getAttribute("sessionFlag") == null) {
System.out.println("进来9");
httpServletRequest.setAttribute("tips", "session超时");
httpServletRequest.getRequestDispatcher("/login.html").forward(request, response);
return false;
} else {
System.out.println("进来10");
redirectToLogin(request, response);
return false;
}
}*/
}
}
}
3、ShiroConfig配置,加入以下配置
/**
* 限制同一账号登录同时登录人数控制
* @return
*/
@Bean
public BeamUserFilter beamUserFilter(){
BeamUserFilter beamUserFilter = new BeamUserFilter ();
//使用cacheManager获取相应的cache来缓存用户登录的会话;用于保存用户—会话之间的关系的;
//这里我们还是用之前shiro使用的redisManager()实现的cacheManager()缓存管理
//也可以重新另写一个,重新配置缓存时间之类的自定义缓存属性
beamUserFilter .setCacheManager(getCacheShiroManager(new EhCacheManagerFactoryBean()));
beamUserFilter .setKickoutAfter(false);
//同一个用户最大的会话数,默认1;比如2的意思是同一个用户允许最多同时两个人登录;
beamUserFilter .setMaxSession(1);
//被踢出后重定向到的地址;
beamUserFilter .setKickoutUrl("/login");
return beamUserFilter ;
}