1、session登陆验证拦截器
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* session拦截器
*/
@Component
public class SessionInterceptor implements HandlerInterceptor {
/**
* 在进入Handler方法执行之前执行本方法
*
* @return true:执行下一个拦截器,直到所有拦截器都执行完,再执行被拦截的Controller
* false:从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (request.getSession().getAttribute(SessionKeyConstant.USER_INFO) != null) {
return true;
}
// 针对ajax请求处理
if (request.getHeader("x-requested-with") != null) {
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
response.setHeader("url", basePath + "/login/sessionTimeout");
} else {
// 如果超时,重定向到登录页面
request.getRequestDispatcher("/login/sessionTimeout").forward(request, response);
}
return false;
}
/**
* 在进入Handler方法之后,返回ModelAndView之前执行
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
/**
* 在Handler方法执行完之后执行
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
2、记住密码,n天免登录
本项目中用cookie保存密码
(1)jsp 一段java代码,找到本地存放的cookie内容,设置到页面上
(2)后台存放到cookie,失效时间3天
<%@ include file="basic.jsp" %>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link href="<%=basePath %>css/bootstrap.min.css" rel="stylesheet" />
<link href="<%=basePath %>css/bootstrap-responsive.min.css" rel="stylesheet" />
<link href="<%=basePath %>css/font-awesome.css" rel="stylesheet" />
<link href="<%=basePath %>css/adminia.css" rel="stylesheet" />
<link href="<%=basePath %>css/adminia-responsive.css" rel="stylesheet" />
<link href="<%=basePath %>css/pages/login.css" rel="stylesheet" />
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<%
String name = "";
String password = "";
//获取当前站点的所有Cookie
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length != 0) {
//对cookies中的数据进行遍历,找到用户名、密码的数据
for (int i = 0; i < cookies.length; i++) {
if ("name".equals(cookies[i].getName())) {
name = cookies[i].getValue();
} else if ("password".equals(cookies[i].getName())) {
password = cookies[i].getValue();
}
}
}
%>
<style type="text/css">
.error{
color:red;
}
</style>
<body>
<div class="navbar navbar-fixed-top" style="background: #1186CB;">
<div class="container" style="width: 97%;">
<a class="brand" href="./">需求管理平台</a>
<div class="nav-collapse">
<ul class="nav pull-right">
<li class="">
</li>
</ul>
</div> <!-- /nav-collapse -->
</div> <!-- /container -->
</div> <!-- /navbar -->
<div id="login-container">
<div id="login-header">
<h3>登录</h3>
</div> <!-- /login-header -->
<div id="login-content" class="clearfix">
<form action="<%=basePath%>login/login" method="post" />
<fieldset>
<div class="control-group">
<label class="control-label" for="username">用户名</label>
<div class="controls">
<input type="text" name="name" class="" id="name" value="<%=name%>" required/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">密码</label>
<div class="controls">
<input type="password" name="password" class="" id="password" value="<%=password%>" required/>
</div>
</div>
<div class="control-group">
<label class="control-label" for="password" style="color: red;">${pageCode.msg }</label>
</div>
</fieldset>
<div id="remember-me" class="pull-left">
<input type="checkbox" name="remeberFlag" id="remember" value="1" />
<label id="remember-label" for="remember">记住密码</label>
</div>
<div class="pull-right">
<button type="submit" class="btn btn-primary btn-large">
登录
</button>
</div>
</form>
</div> <!-- /login-content -->
</div> <!-- /login-wrapper -->
<!-- 放在最后,网页会加载快点 -->
<script src="<%=basePath %>js/jquery-1.7.2.min.js"></script>
<script src="<%=basePath %>js/bootstrap.js"></script>
<!-- 引入表单验证框架 jquery-validation -->
<script src="<%=basePath%>jquery-validation/jquery.validate.min.js"></script>
<script src="<%=basePath%>jquery-validation/localization/messages_zh.js"></script>
</body>
</html>
// 登录,记住密码
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(User user, String remeberFlag, Map<String, Object> model,
HttpServletResponse response) {
User userLogin = loginService.login(user);
if(userLogin != null) {
logger.info("是否记住密码:" + remeberFlag);
if("1".equals(remeberFlag)) {
//创建两个Cookie对象
Cookie nameCookie = new Cookie("name", user.getName());
//设置Cookie的有效期为3天
nameCookie.setMaxAge(60 * 60 * 24 * 3);
Cookie pwdCookie = new Cookie("password", user.getPassword());
pwdCookie.setMaxAge(60 * 60 * 24 * 3);
response.addCookie(nameCookie);
response.addCookie(pwdCookie);
}
// 查询该用户对应的功能菜单
List<Function> functionList = loginService.findFunctionByUserId(userLogin.getUserid());
// 用户信息,对应的菜单存到session
session.setAttribute(SessionKeyConstant.USER_INFO, userLogin);
session.setAttribute(userLogin.getUserid() + SessionKeyConstant.USER_FUNCTION, functionList);
// 跳转到 functionid中的第一条记录
return "redirect:/" + functionList.get(0).getUrl();
} else {
// 停在login页面,提示信息
model.put(PageCodeEnum.KEY, PageCodeEnum.USERNAMEPASSWORDWRONG);
return "/login";
}
}
网友评论