java 登录控制_java登录会话控制 | 学步园

控制系统允许同一个用户同时只开一个会话,设计如下:

1.流程图

0_1277461080joQn.gif

2.表:

create table t_session_info_tab(

logon_idvarchar2(24),--登录用户名

last_update_timeDATE,--最近一次的会话/请求时间

insert_timeDATE,--记录生成记录时间

create_time DATE--登录时间,即会话的创建时间

)

3.详细实现过程:

用户在登录系统时,判断是否有人在用系统:

获取session时间戳:

long sessionTime = request.getSession().getCreationTime();

Date sessionCreateTime = new Date(sessionTime);

获取系统的session设置有效间隔时间:

int matInactiveInterval = request.getSession().getMaxInactiveInterval();

把用户名输入进去,获取到该用户last_update_time和create_time;

把create_time时间转换成yyyy-mm-dd HH:mm:ss格式:

String fomatCreateTimeInDB

=new SimpleDateFormat(“yyyy-mm-dd HH:mm:ss”).format(create_time);

String formatCreateTimeInSession

= new SimpleDateFormat(“yyyy-mm-dd HH:mm:ss”).format(sessionCreateTime);

int interval=sessionCreateTIme.getTime()-last_update_time.getTime();

对比interval和session设置的失效时间间隔matInactiveInterval,如果前者小于后者,

那么对比fomatCreateTimeInDB创建时间和session中的formatCreateTimeInSession时间:

如果相同,则说明该用户没有被其他用户使用,那么就保存数据到DB中,输入参数是:logon_id和create_time.

如果不相同,则说明已经有另外用户在使用,就要给当前登录的客户提示“已有用户在使用系统”的信息.

同时在session中放置是否已经有用户在使用系统的标志user_name_has_used_flag,如果相同就设置为false,即:

request.getSession().setAttribute(“user_name_has_used_flag”,new Boolean(false));

同时在session中也放置关于tab的用户:

request.getSession().setAttribute(“the_same_user_in_tab”,“sameButNotDelete”);

如果不相同,则设置为true,即:

request.getSession().setAttribute(“user_name_has_used_flag”,new Boolean(true));

在session中放置登录时间戳login_session_create_time_flag标志,即:

request.getSession().setAttribute(“login_session_create_time_flag”, sessionCreateTIme);

当user_name_has_used_flag为true时,在footer.jsp页面中给客户一个提示:

window.alert(“用户已在使用中,将强制其下线!”);

对于先登录用户,被强制退出系统的处理:

需要用过滤器来实现,用户在使用系统的过程中,每次请求都要被该过滤器拦截。

首先从系统中获取到登录时的时间戳:

Date sessionCreateTime=

(Date)request.getSession().getAttribute(“login_session_create_time_flag”);

从DB中获取session的创建时间,sessionCreateTimeInDB.

如果sessionCreateTimeInDB为空,则isEqual为true;

如果sessionCreateTimeInDB和sessionCreateTime相等,则isEqual也为true;

否则这个isEqual就是false了,如果isEqual为true,那么用户的请求可以继续进行,否则就要强制用户退出系统,在session中设置一个标志:

httpRequst.getSession().setAttribute(“user_name_will_exit_flag”,new Boolean(true));

在footer.jsp页面中,当user_name_will_exit_flag为true时:

window.alert(“当前用户已重新登录使用本系统,您被强制下线!”);

top.top.location.href=“relogin.do”;

关闭浏览器时,向服务器发送“退出系统”请求,清除服务器端DB中的session信息:

在页面中,利用onunload方法来ajax异步获取用户在退出系统时,向服务器请求的动作:

window.οnunlοad=function(){

send_request(“customerSelfLoginOut.do?userName=”+(window.client_userId?window.client_userId:’’));

}

var http_request=false;

function send_request(url){

http_request=false;

if(window.XMLHttpRequest){

http_request=new XMLHttpRequst();

if(http_request.overrideMimeType){

http_request.overrideMimeType(“text/html”);

}

}

else if(window.ActiveXObject){

try{

http_request=new ActiveXObject(“Xsxml2.XMLHTTP”);

}catch(e){

try{

http_request=new ActiveXObject(“Microsoft.XMLHTTP”);

}catch(e){

}

}

}

if(!http_request){

window.alert(“不能创建XMLHttpRequest对象实例!”);

return false;

}

http_request.open(“GET”,url,true);

http_request.send(null);

}

在服务器端还通过上面提到的过滤器进行处理,当过滤器拦截到是退出系统customerSelfLoginOut.do的请求时,并且the_same_user_in_tab不等于sameButNotDelete标志时,就去DB进行删除session中的信息.

删除信息时,也判断下是否同一个用户的session信息,如果是同一个用户的,那么才可以删除.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值