首先先写一个过滤器Filter,不使用注解
package com.liuhaiyang.springboot.filter2;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//过滤器
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("---------您已进入过滤器222----------");
filterChain.doFilter(servletRequest,servletResponse);
}
}
再写一个配置类
package com.liuhaiyang.springboot.config;
import com.liuhaiyang.springboot.filter2.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //定义此类为配置类
public class FilterConfig {
@Bean
public FilterRegistrationBean myFilterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new MyFilter());
//添加过滤路径
filterRegistrationBean.addUrlPatterns("/user/*");
return filterRegistrationBean;
}
}
最后写一个controller类
package com.liuhaiyang.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@RequestMapping("/user/detail")
public @ResponseBody String userDetail(){
return "/user/detail";
}
@RequestMapping("/center")
public @ResponseBody String center(){
return "/center";
}
}
创建拦截器类,实现HandlerInterceptor接口,重写preHandle方法,在该方法中编写业务拦截的规则
public class UserInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//从session中获取user的信息
User user =(User)request.getSession().getAttribute("user");
//判断用户是否登录
if (null==user){
response.sendRedirect(request.getContextPath()+"/user/error");
return false;
}
return true;
}
}
使用@Configuration注解将这个类定义为配置类,实现WebMvcConfigurer接口并且重写addInterceptors方法
@Configuration//定义此类为配置类
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//addPathPatterns拦截的路径
String[] addPathPatterns = {
"/user/**"
};
//excludePathPatterns排除的路径
String[] excludePathPatterns = {
"/user/login","/user/noLg","/user/error"
};
//创建用户拦截器对象并指定其拦截的路径和排除的路径
registry.addInterceptor(new UserInterceptor()).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);
}
}
编写Controller类
@RestController
@RequestMapping("/user")
public class MyController {
//用户登录
@RequestMapping("login")
public Object login(HttpServletRequest request){
//将已经登录的用户信息添加到session中
request.getSession().setAttribute("user",new User(1,"张三",20));
return "Login Success";
}
//不需要登录也能访问的请求
@RequestMapping("/noLg")
public Object noLg(){
return "Everyone Can See";
}
//必须要登录才能访问的请求
@RequestMapping("/mustLg")
public Object mustLg(){
return "Only User Can See";
}
//如果用户未登录访问了需要登录才能访问的请求会跳转到这个错误提示页面
@RequestMapping("/error")
public Object error(){
return "You Must Login";
}
}