SpringBoot 2.X配置登陆拦截器

前言

在旧版中,通常继承 WebMvcConfigurerAdapter类,但因为2.0后,前者已通过时,在spring boot2.x中,WebMvcConfigurerAdapter被deprecated,虽然继承WebMvcConfigurerAdapter这个类虽然有此便利,但在Spring5.0里面已经deprecated了。 官方文档也说了,WebMvcConfigurer接口如今已经有了默认的空白方法,因此在Springboot2.0(Spring5.0)下更好的作法仍是implements WebMvcConfigurer。css

拦截器

目录

image.png

拦截器

自定义拦截器必须实现HandlerInterceptor,定义一个登陆拦截器,拦截须要登陆的操做,若未登陆则重定向至登陆界面java

package com.cxy.springboot.utils.Interceptor;

import com.cxy.springboot.utils.GlobalConst;
import com.cxy.springboot.utils.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: cxy
 * @Date: 2019/1/10
 * @Description: 拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
        private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
        @Override
        public boolean preHandle(HttpServletRequest request,
                                 HttpServletResponse response, Object handler) throws Exception {

            UserInfo user = (UserInfo)request.getSession().getAttribute(GlobalConst.USER_SESSION_KEY);
            logger.info(request.getRequestURI().toString());
            if (user == null || user.equals(""))  {
                response.sendRedirect("/login");
                logger.info("请先登陆");
                return false;
            }
            return true;
        }

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            logger.info("postHandle...");
        }

        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            logger.info("afterCompletion...");
        }
}
复制代码
  1. preHandle:在业务处理器处理请求以前被调用。预处理,能够进行编码、安全控制、权限校验等处理;
  2. postHandle:在业务处理器处理请求执行完成后,生成视图以前执行。后处理(调用了Service并返回ModelAndView,但未进行页面渲染),有机会修改ModelAndView;
  3. afterCompletion:在DispatcherServlet彻底处理完请求后被调用,可用于清理资源等。返回处理(已经渲染了页面);

注册拦截器

新建类 WebConfigurer.java,addPathPatterns 用来设置拦截路径,excludePathPatterns 用来设置白名单,也就是不须要触发这个拦截器的路径。web

package com.cxy.springboot.utils;

import com.cxy.springboot.utils.Interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @Auther: cxy
 * @Date: 2019/1/10
 * @Description: 在web的配置文件中,实例化登录的拦截器,并添加规则
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/login").excludePathPatterns("/loginSys").excludePathPatterns("/static/**");
    }
}
复制代码

无论哪一个版本,addResourceHandler方法是设置访问路径前缀,addResourceLocations方法设置资源路径,若是你想指定外部的目录也很简单,直接addResourceLocations指定便可,代码以下:spring

registry.addResourceHandler("/static/**").addResourceLocations("file:E:/cxy/");
复制代码

配置静态资源

在application.properties 或 application.yml指定静态资源拦截,要否则静态资源会被拦截。bootstrap

#配置静态资源
spring.mvc.static-path-pattern=/static/**
复制代码

前台静态文件路径配置

<link th:href="@{/static/js/hplus/css/bootstrap.min14ed.css}" rel="stylesheet">
复制代码
相关文章
相关标签/搜索