逻辑实现类
package com.itheima.controller;
import org.apache.catalina.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @Auther: wangchao
* @Date: 2018/9/7 10:52
* @Description: 逻辑实现类
*/
@Controller
public class UserController {
@RequestMapping("/login")
public String login(Model model,Session session , String username, String password,String auto, HttpServletRequest request,HttpServletResponse response) {
if ("zhangsan".equals(username) && "123".equals(password)) {
session.setNote("username", username);
if ("on".equals(auto)) {
Cookie cookie = new Cookie("autologin", username + "#" + password);
cookie.setMaxAge(60 * 60 * 24);
response.addCookie(cookie);
return "index";
}
}
model.addAttribute("msg", "用户名密码不匹配");
return "login";
}
@RequestMapping("/loginUI")
public String loginUI(){
return "login.html";
}
@RequestMapping("/indexUI")
public String indexUI(){
return "index.html";
}
}
Filter类
package com.itheima.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.print.Pageable;
import java.io.IOException;
/**
* @Auther: wangchao
* @Date: 2018/9/7 11:32
* @Description: 利用filter过滤来达到自动登陆
*/
@WebFilter("/*")
public class AutoLoginFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String usernameValue = (String) req.getSession().getAttribute("username");
if(usernameValue != null) {
chain.doFilter(request, response);
return ;
}
String path = req.getRequestURI();
System.out.println("path:"+path);
if(path.contains("login")){
chain.doFilter(request, response);
return ;
}
String cookieValue = null;
Cookie[] cookies = req.getCookies();
if(cookies != null) {
for (Cookie cookie : cookies) {
if ("autologin".equals(cookie.getName())){
cookieValue = cookie.getValue();
break;
}
}
}
boolean isLogin = false;
if(cookieValue != null){
String[] split = cookieValue.split("#");
String username = split[0];
String password = split[1];
if("zhangsan".equals(username)&&"123".equals(password)){
HttpSession session = req.getSession();
session.setAttribute("username",username);
isLogin = true;
}
}
if(isLogin){
chain.doFilter(request,response);
}else{
resp.sendRedirect("/loginUI");
}
}
@Override
public void destroy() {
}
}
主页
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>欢迎您, <span th:text="${session.username}" >用户名</span> , 这里是首页</h3>
</body>
</html>
表单页
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<div style="color: red" th:text="${msg}"></div>
<form action="/login" method="post">
<div>
<input type="text" name="username"/>
</div>
<div>
<input type="password" name="password"/>
</div>
<div>
<input type="checkbox" name="auto" />自动登录
</div>
<div>
<input type="submit" value="登录"/>
</div>
</form>
</body>
</html>