Springboot项目使用拦截器方法详解
2019-11-02 18:07
2376 查看
1. 创建一个拦截器并实现HandlerInterceptor接口
package com.leiyuan.bs.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // 拦截器 public class MyHandlerInterceptor implements HandlerInterceptor { /** * 拦截(Controller方法调用之前) * * @param request request * @param response response * @param o o * @return 通过与否 * @throws Exception 异常处理 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { // TODO 我这里是通过用户是否登陆进行拦截,我的用户信息存储在session中,名称为userSession,大家可以自行实现 if (request.getSession().getAttribute("userSession") == null) { // 拦截至登陆页面 request.getRequestDispatcher("/user/toLogin").forward(request, response); // false为不通过 return false; } // true为通过 return true; } // 此方法为处理请求之后调用(调用过controller方法之后,跳转视图之前) @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } // 此方法为整个请求结束之后进行调用 @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }
2. 创建一个配置类MyHandlerInterceptorConfig并继承WebMvcConfigurerAdapter类重写addInterceptors(InterceptorRegistry registry)方法
package com.leiyuan.bs; import com.leiyuan.bs.interceptor.MyHandlerInterceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; // 拦截器配置类 @Component public class MyHandlerInterceptorConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { /** * 这里的addPathPatterns("/**")为配置需要拦截的方法“/**”代表所有,而后excludePathPatterns("/user/toLogin")等方法为排除哪些方法不进行 拦截 */ registry.addInterceptor(new MyHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/toLogin").excludePathPatterns ("/user/login").excludePathPatterns("/user/toNewUser").excludePathPatterns("/user/newUser"); super.addInterceptors(registry); } }
3. 启动项目即可看到效果了
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- springboot config 拦截器使用方法实例详解
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- SpringBoot项目中使用AOP的方法
- 使用dubbo+zookeeper+spring boot构建服务的方法详解
- Spring boot中使用ElasticSearch的方法详解
- 详解如何使用MongoDB+Springboot实现分布式ID的方法
- Springboot基础1: 4000 使用idea构建项目实现拦截器
- SpringBoot项目中使用redis缓存的方法步骤
- 详解如何在Spring Boot项目使用参数校验
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
- 详解springboot 使用c3p0数据库连接池的方法
- Spring Boot (番外篇:使用idea创建springboot项目的两种方法)
- springboot中使用redis的方法代码详解
- springboot项目中Timestamp的使用方法
- 【HAVENT原创】使用 Spring Boot 的 AOP 拦截器终止方法执行
- 使用Spring boot开发RestFul 风格项目PUT/DELETE方法不起作用
- 详解使用Spring Boot开发Web项目
- 使用IntelliJ IDEA 创建Spring Boot项目时 显示 connect timed out 解决方法
- 详解使用Jenkins部署Spring Boot项目
- 使用IDEA创建SpringBoot项目的方法步骤