登录认证拦截实现:
1. 定义一个登录控制的Control类,用于实现login和logout
2. 定义login的jsp页面
3. 定义login的拦截器LoginInterceptor
拦截中中判断
a. 如果是登录的动作,则放行
如果不是登录的动作,则判断session中是否存在用户等信息,并做相应的校验,如果校验通过,则放行,否则不放行
<!-- 拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.itcast.ssm.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
用户控制类
package cn.itcast.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
/**
* @author 作者 duoduo
* @version 1.0
* @date 创建时间:2017/3/26 12:49
*/
@Controller
public class LoginControl {
// 登陆
@RequestMapping("/login")
public String login(HttpSession session, String username, String password)
throws Exception {
// 调用service进行用户身份验证
// ...
// 在session中保存用户身份信息
session.setAttribute("username", username);
// 重定向到商品列表页面
return "redirect:/items/queryItems.action";
}
// 退出
@RequestMapping("/logout")
public String logout(HttpSession session) throws Exception {
// 清除session
session.invalidate();
// 重定向到商品列表页面
return "redirect:/items/queryItems.action";
}
}
用户登录JSP
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
Created by IntelliJ IDEA.
User: Pengjinjin
Date: 2017/3/26
Time: 12:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login.action" method="post">
<p>First name: <input type="text" name="username" /></p>
<p>Last name: <input type="text" name="lname" /></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
用户拦截器
package cn.itcast.ssm.interceptor;
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;
/**
* @author 作者 duoduo
* @version 1.0
* @date 创建时间:2017/3/26 12:54
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//判断请求的URL是否包含Login.action
String url = request.getRequestURI();
System.out.println("======================");
System.out.println("url:"+url);
//如果是登录提交地址,则放行
if (url.indexOf("login.action")>=0){
return true;
}
//判断session中是否存在用户名
HttpSession session = request.getSession();
String userName = (String) session.getAttribute("username");
System.out.println("username :"+userName);
if(userName!=null && !userName.equals("")){
return true;
}
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 {
}
}