【配置】SpringBoot2.3.0添加拦截器并排除静态资源
2020-06-28 05:08
1376 查看
- 前言: springboot版本:2.3.0
- thymeleaf版本:3.0.11
- Jq和Bootstrap用的webjars
-
实现HandlerInterceptor接口
package com.live.config; import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyHandlerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //测试所用 *_* String login_user = (String) request.getSession().getAttribute("login_user"); if (!StringUtils.isEmpty(login_user) &&login_user.equals("admin")) { return true; } else { response.sendRedirect("index"); return false; } } }
-
添加拦截器
需要排除掉静态资源
-
直接写static目录下的xxx文件,如:
/asserts/**
; -
如果写
/static/
** 会有以下问题 - 资源被解释为 样式表,但使用 MIME 类型 text/plain 进行了传输
如果用的webjars:
/webjars/**
目录结构
package com.live.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyViewConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyHandlerInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/login","/" ,"/asserts/**","/webjars/**"); } }
完!
相关文章推荐
- 解决Spring boot2.0+配置拦截器拦截静态资源的问题
- springboot、springcloud之静态资源路径的配置
- spring boot之spring mvc常用配置-静态资源映射(3)
- springboot下配置resource路径下的静态资源
- spring-boot配置外部静态资源的方法
- Springboot 之 静态资源路径配置
- spring boot 配置静态资源
- Spring Boot 添加拦截器的配置方式
- 爬山的蜗牛旅程:二、springboot配置静态资源和跨域请求
- SpringBoot静态资源处理 springBoot配置参数火推05
- Springboot 静态资源路径配置的两种方法
- SpringBoot部署到服务器Tomcat添加server.context-path后静态资源、请求等404
- Springboot中静态资源和拦截器处理
- spring-boot 加载本地静态资源文件路径配置
- SpringBoot系列七、静态资源文件配置
- SpringBoot(四):静态资源的访问和配置
- spring boot 静态资源和拦截器
- Spring Boot 学习笔记3——静态资源和拦截器
- Spring Boot -- 关于创建了springmvc的配置类导致静态资源访问失效解决方法
- spring-boot 加载本地静态资源文件路径配置