在springboot项目中使用拦截器
2018-05-24 15:49
267 查看
最近练习了springboot项目,作为java开发人员,拦截器是必不可少的,我们用到的最多的用途就是进行用户登录状态的拦截,日志的拦截等。在此记录一下。
首先springboot项目集成了springmvc框架,我使用到的也是springmvc框架的拦截器。
创建一个URLInterceptor类实现HandlerInterceptor,代码如下:/** * 拦截器拦截请求 */ import com.springboot.first.domain.User; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.view.RedirectView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //重写方法中内容根据具体项目来 public class URLInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { String uri = request.getRequestURI(); if(uri.contains("wechat")||uri.contains("api")||uri.contains("wxuser")){ return true; } User user = (User)request.getSession().getAttribute("user"); if(!uri.contains("login")){ if(user==null){ response.sendRedirect("/user/tologin"); return false; } }else{ if(user!=null){ response.sendRedirect("/main/index"); return false; } } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception { if (modelAndView != null){ if(! (modelAndView.getView() instanceof RedirectView)){ String basePath = request.getContextPath(); //modelAndView.addObject("basePath",basePath); request.setAttribute("basePath",basePath); } } } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }然后将URLInterceptor拦截器添加到springboot的配置中,代码如下:
import com.springboot.first.interceptor.URLInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * Title: * @author: chenmingyue * @date: 2018/3/16 12:01 * Description:配置URLInterceptor拦截器,以及拦截路径 */ @Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addInterceptors(InterceptorRegistry registry) { // addPathPatterns 用于添加拦截规则 // excludePathPatterns 用户排除拦截 registry.addInterceptor(new URLInterceptor()).addPathPatterns("/*/*"); super.addInterceptors(registry); } }在ssm项目中也可通过xml配置URLInterceptor拦截器,具体配置在spring-mvc.xml
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.springboot.first.interceptor.URLInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>阅读更多
相关文章推荐
- spring boot 使用spring AOP实现拦截器
- Spring Boot 项目构建 之 使用 Spring Boot 构建应用(Building an Application with Spring Boot)
- 在springboot项目中使用jsp视图
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
- 如何在Spring Boot项目使用参数校验
- 使用Swagger在SpringBoot项目中管理API文档(使用Oauth2)
- 使用spring boot快速构建spring框架的maven项目
- Spring Boot项目中使用jdbctemplate 操作MYSQL数据库
- 使用Docker发布Spring Boot项目
- 《Gradle教程系列》四.使用gradle搭建SpringBoot项目
- Idea使用Maven创建多模块SpringBoot项目问题记录
- 使用Spring Boot和Gradle创建AngularJS项目
- springboot+angular项目 使用token方式进行权限验证
- SpringBoot+Freemarker 项目ftl中使用include的异常
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
- 使用SpringBoot,优质快速开发Java项目
- 从零学spring boot--集成mybatis--项目--使用注解方式
- 使用spring boot 改造老项目,多个数据源问题
- SpringBoot拦截器的使用