乱七八糟的整理~~
同一个账户被第二个浏览器登录,第一个浏览器的账号在做其他操作时被强制下线。
Filter:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.chy.www.code.model.User;
/**
* Servlet Filter implementation class CheckTwoUserFilter
*/
public class CheckMoreUserFilter implements Filter {
CheckMoreUser checkTwoUser = null;
HashMap<String, String> userMap = null;
/**
* Default constructor.
*/
public CheckMoreUserFilter() {
// TODO Auto-generated constructor stub
System.out.println("构造..");
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
System.out.println("销毁。。");
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request; //转HttpServletRequest
String requestURI = req.getRequestURI(); //获取请求地址
checkTwoUser = CheckMoreUser.getInstance();
HttpSession session = req.getSession();
String sessionId = session.getId(); //session地址
User user = null;
if ("/cadreadmin/index/login.do".equals(requestURI)) { //点击登录时把用户名和sessionId存入userMap
chain.doFilter(request, response);
String flag = (String) session.getAttribute("flag");
if (flag != null && flag.equals("0")) {
user = (User) session.getAttribute("user");
checkTwoUser.putUser(user.getUserName(), sessionId);
return;
}
return;
} else if ("/cadreadmin/index/code.do".equals(requestURI) == false
&& "/cadreadmin/index/gologin.do".equals(requestURI) == false) { //验证码和去登录页面时不过滤
//乱码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=TUF-8");
userMap = checkTwoUser.getUserMap();
user = (User) session.getAttribute("user");
if (null == user || "".equals(user)) {
// user为空 未登录
PrintWriter out = response.getWriter();
StringBuilder builder = new StringBuilder();
builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
builder.append("alert(\"您的账号未登录或长时间未操作(超过24小时),请从新登录!\");");
builder.append("window.top.location.href=\"");
builder.append("" + req.getContextPath()
+ "/index.jsp\";</script>");
out.print(builder.toString());
out.close();
} else if (sessionId.equals(userMap.get(user.getUserName())) == false) {
//当前user与sessionId不符 账号别处登录
PrintWriter out = response.getWriter();
StringBuilder builder = new StringBuilder();
builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
builder.append("alert(\"您的账号已在别处登录,请重新登录!\");");
builder.append("window.top.location.href=\"");
builder.append("" + req.getContextPath()
+ "/index.jsp\";</script>");
out.print(builder.toString());
out.close();
}
chain.doFilter(request, response);
return;
}
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init..");
}
}
普通java类:
import java.util.HashMap;
public class CheckMoreUser{
public static CheckMoreUser checkTwoUser = new CheckMoreUser();
private HashMap<String, String> userMap = new HashMap<String, String>();
private CheckMoreUser() {
super();
// TODO Auto-generated constructor stub
}
public static CheckMoreUser getInstance(){
return checkTwoUser;
}
public void putUser(String userName,String sessionId){
userMap.put(userName, sessionId);
}
public void putOutUser(String userName){
userMap.remove(userName);
}
public HashMap<String, String> getUserMap(){
return userMap;
}
public void deleteUser(String userName){
userMap.remove(userName);
}
}
安全退出:
CheckMoreUser.getInstance().deleteUser(user.getUserName());