Spring 学习

SpringBoot 2.x 自定义拦截器并解决静态资源访问被拦

2018-08-22  本文已影响698人  i玄离

说明:

在自己写一个spring boot demo的时候,想着根据session的过期时间来返回登录页面,在自定义完拦截器以后功能可以实现,但是静态资源却无法访问,通过查找文档发现spring boot 1.x和2.x是不一样,所以特别记录一下,留给后面的同学避免踩坑.

1.x和2.x的静态资源访问区别

项目的目录

}O31{R6DQ}AQZLRQR0WVU`D.png

自定义拦截器

/**
 * UserSecurityInterceptor
 * Created with IntelliJ IDEA.
 * Author: yangyongkang
 * Date: 2018/8/22
 * Time: 14:20
 */
@Component
public class UserSecurityInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate<String, Serializable> redisCacheTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserModel info = (UserModel) redisCacheTemplate.opsForValue().get(request.getSession().getId());
        if (info == null || StringUtils.isEmpty(info)) {
            response.sendRedirect(request.getContextPath() + "/view/login");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }
}

配置访问路径及静态资源

/**
 * 登陆拦截控制类
 * Created with IntelliJ IDEA.
 * Author: yangyongkang
 * Date: 2018/8/22
 * Time: 14:17
 */
@Configuration
public class WebMvcConfig implements  WebMvcConfigurer {
    @Autowired
    private UserSecurityInterceptor securityInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration addInterceptor = registry.addInterceptor(securityInterceptor);
        // 排除配置
        addInterceptor.excludePathPatterns("/error");
        addInterceptor.excludePathPatterns("/static/**");//排除静态资源
        addInterceptor.excludePathPatterns("/view/login");
        addInterceptor.excludePathPatterns("/login/check");
        // 拦截配置
        addInterceptor.addPathPatterns("/**");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");//
    }
}

页面加载方式

//第一种在thymeleaf模版内部
    <link type="text/css" th:href="@{/static/css/page/bootstrap.min.css}" rel="stylesheet">
    <link type="text/css" th:href="@{/static/css/page/font-awesome.css}" rel="stylesheet">
    <link type="text/css" th:href="@{/static/css/page/animate.css}" rel="stylesheet">
    <link type="text/css" th:href="@{/static/css/page/style.css}" rel="stylesheet">
    <link type="text/css" th:href="@{/static/css/page/login.css}" rel="stylesheet">
    
//第二种
    <link href="/static/css/page/bootstrap.min.css" rel="stylesheet">
    <link href="/static/css/page/font-awesome.css" rel="stylesheet">
    <link href="/static/css/page/animate.css" rel="stylesheet">
    <link href="/static/css/page/style.css" rel="stylesheet">

最后特别注意一下

implements WebMvcConfigurer : 
//不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
@EnableWebMvc + implements WebMvcConfigurer :
// 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
extends WebMvcConfigurationSupport :
//会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
extends DelegatingWebMvcConfiguration :
//会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
上一篇 下一篇

猜你喜欢

热点阅读