@Controller
@RequestMapping("index")
public class IndexController {
/**
* 去后台首页
* @param request
* @return
*/
@RequestMapping("")
public String toIndex(HttpServletRequest request){
return "index";
}
}
/**
*登录拦截器
*/
public class LoginInterceptor implements HandlerInterceptor{
@Override
public boolean preHandler(HttpServletRequest request,HttpServletResponse response,Object handler)
throws Exception{
Logger log = LoggerFactory.getLogger(LoginInterceptor.class);
//1. 获取请求地址
String uri = request.getRequestUri("adminlogin");
// 2. 该地址若存在 则放行
if(uri.indexOf("adminlogin") >= 0 || uri.indexOf("login") >= 0){
retrun true;
}
// 3. 获取会话对象session
HttpSession session = request.getSession();
// 4.通过会话对象session获取标记ADMIN
User user = (User)session.setAttribute("ADMIN");
// 5. 通过ADMIN获取到的User对象、进行判断是否存在
if(user != null){
//表示已登录、不再拦截
log.info("表示已登录、不再拦截");
return true;
}
// 6. 已拦截
request.setAttribute("msg","尚未进行登录");
log.info("表示未登录、已拦截");
response.sendRedirect(request.getConetxtPath + "/adminlogin");
return false;
}
}
@Controller
public class UserController {
@RequestMapping("login")
public String login(User user, HttpServletRequest request){
User u = userService.userLogin(user);
System.out.println("u=" + u);
// 判断登录是不是管理员
if (u.getType() == 1){
// 确定是管理员
request.getSession().setAttribute("ADMIN",u);
// 重定向到IndexContrller中的index方法。进入到后台首页
return "redirect:/index";
}else if (u.getType() == 0){
// 确定是 普通用户
request.getSession().setAttribute("USER",u);
// 重定向到IndexContrller中的index/index方法。进入到前台首页
return "redirect:/index/index";
}else{
// 确定是 普通用户
request.setAttribute("msg","您的账户或密码有误,请检查后登录!");
return "redirect:/adminlogin";
}
}
}
@Configuration
public class LoginConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
// 拦截路径: 项目下所有拦截
registration.addPathPatterns("/**");
// 不拦截路径:
registration.excludePathPatterns("/login,/**/*.html","/**/*.css","/**/*.png","/**/*.js","/**/*.jpg");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("index.html").setViewName("index");
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LOGIN</title>
<link rel="stylesheet" th:href="@{/static/component/style/components.css}" href="../static/component/style/components.css">
<link rel="stylesheet" th:href="@{/static/css/bootstrap.css}" href="../static/css/bootstrap.css">
<link rel="stylesheet" th:href="@{/static/css/plugins.css}" href="../static/css/plugins.css">
<link rel="stylesheet" th:href="@{/static/css/main.css}" href="../static/css/main.css">
<link rel="stylesheet" th:href="@{/static/css/themes.css}" href="../static/css/themes.css">
<script th:src="@{/static/component/js/JQuery2.1.4.js}"></script>
<script th:src="@{/static/component/plugins/layer/layer.js}"></script>
</head>
<body>
<div id="login">
<form action="/login" method="post">
<div class="center">
<dl>
<dt><i class="gi gi-leaf"></i>Lover's · <span> BookHouse</span></dt>
<dd><span><i class="fa fa-fw fa-user"></i></span><input type="text" name="usercode" placeholder="请输入账号"></dd>
<dd><span><i class="fa fa-fw fa-lock"></i></span><input type="password" name="password" placeholder="请输入密码"></dd>
<dd><button type="submit">登录</button></dd>
</dl>
</div>
</form>
</div>
</body>
</html>