登录验证
本功能基于拦截器实现。
UserController
// 用户登录
@RequestMapping("/tologin.action")
public String tologin() {
return "login";
}
@RequestMapping("/login")
public String login(User user, HttpSession session) {
List<User> users = userService.findByName(user.getName());
String password = user.getPassword();
if (!users.isEmpty()) {
// 用户名正确
String realPassword = users.get(0).getPassword();
if (password.equals(realPassword)) {
// 密码正确 -> 登录成功
session.setAttribute("USER_SESSION", users.get(0));
return "main";
} else {
// 密码错误 -> 登录失败
session.setAttribute("msg", "用户名或密码错误,请重新登录!");
return "login";
}
}
// 用户名错误
session.setAttribute("msg", "用户名或密码错误,请重新登录!");
return "login";
}
LoginInterator
package com.how2java.tmall.interator;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.how2java.tmall.pojo.User;
public class LoginInterator implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String url=request.getRequestURI();
if(url.indexOf("/login")>=0)
return true;
HttpSession session=request.getSession();
User user=(User)session.getAttribute("USER_SESSION");
if(user!=null)
return true;
request.setAttribute("msg", "您还没有登录,请先登录!");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
}
三天免验证登录
本功能基于Cookie实现。
设置Cookie的有效期(时间单位:秒)
cookie.setMaxAge(32460*60);
UserController
@RequestMapping("/login")
public String login(User user, HttpSession session,HttpServletResponse response) {
List<User> users = userService.findByName(user.getName());
String password = user.getPassword();
if (!users.isEmpty()) {
// 用户名正确
String realPassword = users.get(0).getPassword();
if (password.equals(realPassword)) {
// 密码正确 -> 登录成功
session.setAttribute("USER_SESSION", users.get(0));
Cookie cookie=new Cookie("user", users.get(0).getId()+"-"+user.getName());
cookie.setMaxAge(3*24*60*60);
response.addCookie(cookie);
return "main";
} else {
// 密码错误 -> 登录失败
session.setAttribute("msg", "用户名或密码错误,请重新登录!");
return "login";
}
}
// 用户名错误
session.setAttribute("msg", "用户名或密码错误,请重新登录!");
return "login";
}
LoginInterator
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
Cookie[] cookies=request.getCookies();
Boolean iscook=false;
if(cookies!=null) {
for(Cookie cookie:cookies) {
// 存在user
if("user".equals(cookie.getName())) {
// 设置登录用户
HttpSession session=request.getSession();
String string=cookie.getValue();
String[] strings=string.split("-");
User user=new User();
user.setId(Integer.valueOf(strings[0]));
user.setName(strings[1]);
session.setAttribute("USER_SESSION", user);
// 重定向
response.sendRedirect("/tmall_ssm_pagehelper_generator/home");
iscook=true;
}
}
// 存在Cookies,但没有"user"
if(iscook==false)
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
}else {
request.setAttribute("msg", "您还没有登录,请先登录!");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
}
return false;
}