1. 拦截器 29
拦截器是SpringMVC中一种对象,能拦截器对Controller的请求。
拦截器框架中有系统的拦截器, 还可以自定义拦截器。 实现对请求预先处理。
2. springmvc实现自定义拦截器: 29
2.1 创建类实现SpringMVC框架的HandlerInterceptor接口
public interface HandlerInterceptor {
default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return true;
}
default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
2.2 需在SpringMVC的配置文件中,声明拦截器
<mvc:interceptors>
<mvc:interceptor>
<mvc:path="url" />
<bean class="拦截器类全限定名称"/>
</mvc:interceptor>
</mvc:interceptors>
3. springboot实现拦截器 30-31
需求,拦截登录请求
自定义拦截器LoginInterceptor
package com.bjpowernode.web;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* springboot自定义拦截器 30
*/
public class LoginInterceptor implements HandlerInterceptor {
/**
*
* @param request
* @param response
* @param handler 被拦截器的控制器对象
* @return boolean
* true: 请求能被Controller处理
* false: 请求被截断
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("执行了LoginInterceptor的preHandle");
return true;
}
}
注册自定义拦截器MyAppConfig
package com.bjpowernode.config;
import com.bjpowernode.web.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//注册拦截器 30
//springboot自定义拦截器 30
@Configuration //这个类当作xml文件使用
public class MyAppConfig implements WebMvcConfigurer {
//添加拦截器对象, 注入到容器中
@Override
public void addInterceptors(InterceptorRegistry registry) {
//创建拦截器对象
LoginInterceptor interceptor = new LoginInterceptor();
//指定拦截的请求uri地址
String path []= {"/user/**"};
//指定不拦截的地址
String excludePath [] = {"/user/login"};
registry.addInterceptor(interceptor)//将我们自定义的拦截器添加进InterceptorRegistry
.addPathPatterns(path)//这个是拦截谁
.excludePathPatterns(excludePath);//这个是不拦截谁
}
}
控制器类BootController
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//springboot自定义拦截器 31
@Controller
public class BootController {
@RequestMapping("/user/account")
@ResponseBody
public String userAccount(){
return "访问user/account地址";
}
@RequestMapping("/user/login")
@ResponseBody
public String userLogin(){
return "访问user/login地址";
}
}
测试Course6Application
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Course6Application {
public static void main(String[] args) {
SpringApplication.run(Course6Application.class, args);
}
}
拦截器执行
拦截器不执行
4. 使用servlet 32
ServletRegistrationBean 用来做在 servlet 3.0+容器中注册 servlet 的功能,但更具有
SpringBean 友好性。
在SpringBoot框架中使用Servlet对象。
4.1 使用步骤:
1. 创建Servlet类。 创建类继承HttpServlet
2. 注册Servlet ,让框架能找到Servlet
4.2 例子:32-33
代码在course6_1中
1.创建自定义Servlet
MyServlet
package com.bjpowernode.web;
import javax.servlet.ServletException;
import javax.servlet
import javax.servlet
import javax.servlet
import java.io.IOException;
import java.io.PrintWriter;
//springboot使用servlet 32
//创建Servlet类
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//使用HttpServletResponse输出数据,应答结果
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("===执行的是Servlet==");
out.flush();
out.close();
}
}
注册Servlet
WebApplictionConfig
package com.bjpowernode.config;
import com.bjpowernode.web.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebApplictionConfig {
//定义方法, 注册Servlet对象
@Bean
public ServletRegistrationBean servletRegistrationBean(){
//有参构造
//第一个参数是 Servlet对象, 第二个是url地址
//public ServletRegistrationBean(T servlet, String... urlMappings)
// ServletRegistrationBean bean =
// new ServletRegistrationBean( new MyServlet(),"/myservlet");
//无参构造 33
ServletRegistrationBean bean = new ServletRegistrationBean();
bean.setServlet( new MyServlet());
bean.addUrlMappings("/login","/test"); //
return bean;
}
}