Commit 71c81774 by liu_xiaoxu

登录超时认证拦截器改为跳转主项目,部署项目时需要在config.properties配置文件里配置跳转路径

parent cbd40924
package com.cc.HandlerInterceptor;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Properties;
import static com.cc.utils.propertiesUtil.propertiesUtil;
/**
* Created by changchao on 2020/3/11.
*/
public class SystemSessionInterceptor implements HandlerInterceptor {
private static final String LOGIN_URL = "/relogin.jsp";
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
@Override
......@@ -31,6 +34,10 @@ public class SystemSessionInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
Properties configProperties=propertiesUtil("config.properties");
Map<String,Object> configMap = new HashedMap();
String loginTimeOutUrl= configProperties.getProperty("loginTimeOutUrl");
//不是ajax请求,则直接跳转页面
HttpSession session = request.getSession(true);
//session中获取用户名信息
......@@ -40,13 +47,16 @@ public class SystemSessionInterceptor implements HandlerInterceptor {
if(request.getHeader("x-requested-with")!=null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
//是ajax请求,则返回个消息给前台
response.setHeader("sessionstatus", "timeout");
response.setHeader("loginTimeOutUrl", loginTimeOutUrl);
}else{
//不是ajax请求,则直接跳转页面
//超时登录
response.sendRedirect(request.getSession().getServletContext().getContextPath() + LOGIN_URL);
return false;
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println ("<script language=javascript>alert('登录超时请重新登录!');window.location='"+loginTimeOutUrl+"'</script>");
// response.sendRedirect("/loginFail");//页面跳转到login登录页面
}
return false;
}
return true;
}
}
\ No newline at end of file
......@@ -120,39 +120,4 @@ public class LoginController {
}
return returnMap;
}
//调用登录后台 返回resultlogin页面,登录成功即自动关闭;
@RequestMapping(value="/relogin",method = {RequestMethod.POST, RequestMethod.GET})
public String retoHome(HttpServletRequest request, @RequestParam("username") String username, @RequestParam("password") String password,ModelMap map){
//Map<String,Object> returnMap=new HashMap<>();
User user=null;
user=(User)request.getSession().getAttribute("user");
if(user==null){//进行登录
try {
User yuser=loginService.queryUserByUsername(username);
if(yuser!=null){
BASE64Encoder encoder = new BASE64Encoder();
String ypassword=encoder.encode(password.getBytes());
if(ypassword.equals(yuser.getPassword())){
user=yuser;
}
}
} catch (Exception e) {
e.printStackTrace();
map.addAttribute("state","error");
map.addAttribute("msg","登录异常!");
return "resultlogin";
}
}
if(user!=null){
request.getSession().setAttribute("user",user);//登录成功生成session
String sid=request.getSession().getId();
map.addAttribute("state","success");
map.addAttribute("msg","登陆成功!");
map.addAttribute("sid",sid);
}else{
map.addAttribute("state","failed");
map.addAttribute("msg","用户名或密码错误!");
}
return "resultlogin";
}
}
......@@ -67,3 +67,5 @@ zhyyPath=http://zhyy.xzxt.nm:7001
main.url=http://www.xzxt.nm
caiji.url=http://anjian.xzxt.nm:9022
zxaj.url=http://127.0.0.1:9101
#登录超时页面跳转路径
loginTimeOutUrl=http://zhyy.xzxt.nm
\ No newline at end of file
......@@ -28,7 +28,7 @@
<mvc:mapping path="/**" />
<!-- 不需要拦截的地址 -->
<mvc:exclude-mapping path="/static/**"/>
<mvc:exclude-mapping path="/relogin"/>
<mvc:exclude-mapping path="/login"/>
<bean class="com.cc.HandlerInterceptor.SystemSessionInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
......
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆界面</title>
<link rel="stylesheet" type="text/css" href="static/rasc/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="static/dy/css/base-login.css">
</head>
<body class="login-body">
<header class="login-header">
<div class="login-title"><img src="static/images/title.png" /></div>
</header>
<div class="lines">
<div class="content">
<div class="left-globe">
<h1 class="title"></h1>
</div>
<div class="right-login">
<div class="login-wrap">
<h2 class="namel">用户登录</h2>
<form action="relogin" method="post">
<p class="tc"><input type="text" placeholder="账号" name="username" class="name" autocomplete="off"/><i class="username-icon zdy-fa"></i></p>
<p class="tc"><input type="password" placeholder="密码" name="password" class="password" autocomplete="off"/><i class="password-icon zdy-fa"></i></p>
<p class="tc" style="margin-top:36px;"><input type="submit" class="btn btn-info" value="登陆"/></p>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录结果</title>
</head>
<body>
<span id="time">登录状态:"${state}"</span><br>
<script type="text/javascript" src="static/easyui-window/js/jquery-1.11.3.js"></script>
<script type="text/javascript">
//定义函数myClose关闭当前窗口
function myClose(){
//将id为time的元素的内容转为整数,保存在变量n中
//var n=parseInt(time.innerHTML);
debugger;
var n=1;
n--;//将n-1
//如果n==0,关闭页面
//否则, 将n+秒钟后自动关闭,再保存回time的内容中
if(n>0){
time.innerHTML=n+"秒钟后自动关闭";
timer=setTimeout(myClose,1000);
}else{
window.location.href="about:blank";
window.close();
}
}
var timer=null;
//当页面加载后,启动周期性定时器,每个1秒执行myClose
window.onload=function(){
var state = "${state}";
if(state=="success"){
debugger;
timer=setTimeout(myClose,1000);
}
}
</script>
</body>
</html>
\ No newline at end of file
......@@ -7,63 +7,9 @@ $.ajaxSetup({
complete: function(xhr,status) {
var sessionStatus = xhr.getResponseHeader('sessionstatus');
if(sessionStatus == 'timeout') {
//var top = getTopWinow();
//var tempwindow=window.open('_blank');
var yes = confirm('由于您长时间没有操作, session已过期, 请重新登录.');
if (yes) {
//tempwindow.location.href = '/relogin.jsp';
openWin( '/xzxt-solr/relogin.jsp','用户登录', '8.5','7.5');
}
var loginTimeOutUrl = xhr.getResponseHeader('loginTimeOutUrl');
alert('登录已经超时, 请重新登录.');
window.open( loginTimeOutUrl);
}
}
});
/**
* 在页面中任何嵌套层次的窗口中获取顶层窗口
* @return 当前页面的顶层窗口对象
*/
function getTopWinow(){
var p = window;
while(p != p.parent){
p = p.parent;
}
return p;
}
//name 弹窗名字
//width 弹窗宽度(比例)
//height 弹窗高度(比例)
var myWindow;
function openWin(url, name, width, height) {
var w_percent = width ? width : 9;//如果参数未传,则取9
var h_percent = height ? height : 8;
var widths = (window.screen.availWidth) * w_percent / 10;
var heights = (window.screen.availHeight) * h_percent / 10;
var iLeft = (window.screen.availWidth - widths) / 2;
var iTop = (window.screen.availHeight - heights) / 2;
//判断是否为IE
var b_version=navigator.appVersion;
var version=b_version.split(";");
if(version[1]){
var trim_Version=version[1].replace(/[ ]/g,"");
}
if(trim_Version == 'MSIE9.0' || trim_Version == 'MSIE10.0'|| trim_Version == 'WOW64' || trim_Version == 'MSIE7.0'){//IE浏览器,直接打开
myWindow = window.open(url, name, 'scrollbars=yes,width=' + widths + ',height=' + heights + ',left=' + iLeft + ',top=' + iTop + '');
myWindow.focus();
}else{//非IE,判断窗口是否存在,若存在,则关闭当前窗口,重新打开
if(myWindow){
myWindow.close();
myWindow = null;
}
myWindow = window.open(url, name, 'scrollbars=yes,width=' + widths + ',height=' + heights + ',left=' + iLeft + ',top=' + iTop + '');
}
//弹窗名字重命名ss
var title = name;
setTimeout(function () {
myWindow.document.title = title;
}, 1000);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment