在用户登录后,可以通过管理员的帐号,剔除登录的一些用户,实现的思路:1、通过用户的Id删除存在数据库里面的Session;2、根据sessionid来删除存在与Ecache中的缓存数据, 完成以上两部,就可以完成手动剔除登录的用户了,当使用的缓存方式不一样,操作的方式也会有所差别,但步骤差不多
前提概要
我这个处理的时候,使用的是数据库进行用户Session的管理,来存储session,最好先看一下前一篇文章:
Shiro之保存Session到数据库中-yellowcong
源码地址
https://gitee.com/yellowcong/shior-dmeo/tree/master/test
环境
服务 | 版本 |
---|---|
数据库 | Mysql |
缓存 | ehcache |
框架 | Spring+SpringMVC+Mybatis |
控制层
这个控制层中,我做的是将数据删除后,返回json字符串到客户端中
package com.yellowcong.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.session.Session;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.yellowcong.shiro.dao.SessionDao;
/**
* 创建日期:2017/12/21<br/>
* 创建时间:11:05:18<br/>
* 创建用户:yellowcong<br/>
* 机能概要:
*/
@Controller
@RequestMapping("/session")
@Scope("prototype")
public class SessionController {
@Autowired
private SessionDao sessionDao;
@Autowired
private EhCacheManager cacheManager;
//cacheManager
/**
* 创建日期:2017/12/21<br/>
* 创建时间:11:06:17<br/>
* 创建用户:yellowcong<br/>
* 机能概要:删除session
* @param username
*/
@RequestMapping(value="/del/{username}",method=RequestMethod.GET)
public void delSession(@PathVariable String username,HttpServletResponse response) {
//获取到当前登录用户的session
List<Session> sessions = this.sessionDao.loadByUserName(username);
if(sessions != null && sessions.size() >0) {
//遍历删除到session
for(Session session :sessions) {
this.removeSession(session);
}
}
//写数据到浏览器
Map<String,Object> map = new HashMap<String, Object>();
map.put("result", true);
map.put("username", username);
map.put("cnt", sessions.size());
this.writeJSON(response, map);
}
/**
* 创建日期:2017/12/21<br/>
* 创建时间:13:59:03<br/>
* 创建用户:yellowcong<br/>
* 机能概要:删除session中的数据
* @param session
*/
private void removeSession(Session session) {
//session id
String sessionid = session.getId().toString();
//删除缓存中的session
this.cacheManager.getCache("shiro-activeSessionCache").remove(sessionid);
//删除数据库中的session
this.sessionDao.delete(session);
}
/**
* 在SpringMvc中获取到Session
* @return
*/
public void writeJSON(HttpServletResponse response,Object object){
try {
//设定编码
response.setCharacterEncoding("UTF-8");
//表示是json类型的数据
response.setContentType("application/json");
//获取PrintWriter 往浏览器端写数据
PrintWriter writer = response.getWriter();
ObjectMapper mapper = new ObjectMapper(); //转换器
//获取到转化后的JSON 数据
String json = mapper.writeValueAsString(object);
//写数据到浏览器
writer.write(json);
//刷新,表示全部写完,把缓存数据都刷出去
writer.flush();
//关闭writer
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
SessionDao
package com.yellowcong.shiro.dao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.ValidatingSession;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import com.yellowcong.shiro.utils.SerializableUtils;
/**
* 创建日期:2017/12/21<br/>
* 创建时间:8:31:04<br/>
* 创建用户:yellowcong<br/>
* 机能概要:用于Session的保存
*/
public class SessionDao extends EnterpriseCacheSessionDAO {
@Autowired
private SessionMapper sessionMapper;
public void delete(Session session) {
//删除session
this.sessionMapper.delete(session.getId().toString());
}
public void update(Session session) throws UnknownSessionException {
//当是ValidatingSession 无效的情况下,直接退出
if(session instanceof ValidatingSession &&
!((ValidatingSession)session).isValid() ) {
return ;
}
//检索到用户名
String username = String.valueOf(session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
//序列化Session
this.sessionMapper.update(session.getId().toString(), SerializableUtils.serializ(session),username);
}
@Override
protected Serializable doCreate(Session session) {
//生成session的id
Serializable sessionId = generateSessionId(session);
//给Session设定id
assignSessionId(session, sessionId);
//插入session 到数据库
this.sessionMapper.insert(session.getId().toString(), SerializableUtils.serializ(session));
return sessionId;
}
/**
* 创建日期:2017/12/21<br/>
* 创建时间:13:56:15<br/>
* 创建用户:yellowcong<br/>
* 机能概要:通过名称来获取用户 Session
* @param username
* @return
*/
public List<Session> loadByUserName(String username) {
//获取session的字符串
List<com.yellowcong.shiro.model.Session> dbSessions = this.sessionMapper.loadByUserName(username);
//判断是否存在用户的情况
if(dbSessions == null || dbSessions.size() == 0) {
return null;
}
List<Session> result = new ArrayList<Session>();
for(com.yellowcong.shiro.model.Session session:dbSessions) {
//加载session数据
String sessionStr = session.getSession();
//将Session的数据串,转化为对象
result.add(SerializableUtils.deserializ(sessionStr));
}
return result;
}
@Override
protected Session doReadSession(Serializable sessionId) {
//获取session的字符串
com.yellowcong.shiro.model.Session dbSession = this.sessionMapper.load(sessionId.toString());
if(dbSession == null) {
return null;
}
//加载session数据
String sessionStr = dbSession.getSession();
return SerializableUtils.deserializ(sessionStr);
}
}
测试
浏览器A
浏览器B
浏览器B删除A的session
删除成功