spring中的拦截器
当用户发送请求的时候,有些请求是无效的,这个时候就是要控制他们的请求,比如说用户请求登陆后台,但是他还没有登陆,这样他的请求的地址是无效的,我们要吧这个请求拦截下来,或者给他转发到其他请求上。现在用spring的拦截器控制用户的请求。
现在的需求:用户进入到index页面。实现登陆,或者进入后台,但是如果没有登陆,点击进入后台是要被转发到index界面。如果登陆后,在index界面点击后台可以正常进入后台。
实现思路:通过拦截器实现控制用户的请求url,当用户请求后台的地址的时候,判断是否登陆(可以把登陆信息放到session中)如果登陆就跳转后台,没有登陆就跳转index
-
新建maven项目
-
导入spring-webmvc等依赖
-
添加web框架支持
-
工程结构
-
在web.xml中注册dispatcherServlet的servlet,指定springmvc的配置文件
-
新建springmvc配置文件applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="controller"/>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<!--配置拦截器的请求地址-->
<!--/**包括/下面所有的请求,/*当前路径的请求-->
<mvc:mapping path="/login/**"/>
<bean class="interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
- 编写controller
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
/**
* @Author: nsk
* @Description:
* @Date: create in 2021/4/21 17:51
*/
@Controller
public class LoginController {
@RequestMapping("test")
public String test(){
return "index";
}
@RequestMapping("/login/gotoLogin")
public String gotoLogin(){
return "login";
}
@RequestMapping("/login/login")
public String login(HttpSession session,String name){
if (name.equals("nsk")) {
session.setAttribute("name", name);
return "main";
} else
return "loginerror";
}
@RequestMapping("/login/loginsuccess")
public String afterLoginMain(){
return "main";
}
@RequestMapping("/login/goOut")
public String goOut(HttpSession session){
session.removeAttribute("name");
return "login";
}
}
- 编写拦截器
package interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @Author: nsk
* @Description:
* @Date: create in 2021/4/21 17:49
*/
public class LoginInterceptor implements HandlerInterceptor {
//返回true,通过过滤器
//返回false,被拦截
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("===================拦截=================");
System.out.println(request.getRequestURI());
HttpSession session = request.getSession();
//如果是/login/gotoLogin请求地址的话。放行,因为配置了login下的所有请求都会被拦截,跳转login.jsp
//可以设置拦截什么请求
if (request.getRequestURI().equals("/login/gotoLogin")){
return true;
}
if (request.getRequestURI().equals("/login/login")){
return true;
}
if (session.getAttribute("name") != null){
System.out.println("session:"+session.getAttribute("name"));
return true;
}
System.out.println(request.getRequestURI()+"拦截成功!");
//转发
//request.getRequestDispatcher("/index.jsp").forward(request,response);
return false;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("====================处理后=======================");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("====================清理=======================");
}
}
- jsp/index.jsp
<%--
Created by IntelliJ IDEA.
User: 牛宝宝
Date: 2021/4/21
Time: 17:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1><a href="${pageContext.request.contextPath}/login/gotoLogin">登陆</a></h1>
<h1><a href="${pageContext.request.contextPath}/login/loginsuccess">首页</a></h1>
</body>
</html>
- login.jsp
<%--
Created by IntelliJ IDEA.
User: 牛宝宝
Date: 2021/4/21
Time: 17:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/login/login">
用户名:<input type="text" name="name">
密码: <input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
- loginerror.jsp
<%--
Created by IntelliJ IDEA.
User: 牛宝宝
Date: 2021/4/21
Time: 17:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆</title>
</head>
<body>
<h1>登陆失败请检查账号密码</h1>
<h1><a href="${pageContext.request.contextPath}/index.jsp">返回</a></h1>
</body>
</html>
- main.jsp
<%--
Created by IntelliJ IDEA.
User: 牛宝宝
Date: 2021/4/21
Time: 17:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆后的main</title>
</head>
<body>
<h1>恭喜登陆成功!</h1>
<h1><a href="${pageContext.request.contextPath}/login/goOut">注销</a></h1>
</body>
</html>
- /index.jsp
<%--
Created by IntelliJ IDEA.
User: 牛宝宝
Date: 2021/4/21
Time: 17:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1><a href="${pageContext.request.contextPath}/login/gotoLogin">登陆</a></h1>
<h1><a href="${pageContext.request.contextPath}/login/loginsuccess">首页</a></h1>
</body>
</html>