好像叫单点登录吧?(自用)

乱七八糟的整理~~

同一个账户被第二个浏览器登录,第一个浏览器的账号在做其他操作时被强制下线。


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());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值