SpringBoot 2.X配置登录拦截器

前言

在旧版中,一般继承 WebMvcConfigurerAdapter类,但由于2.0后,前者已经过时,在spring boot2.x中,WebMvcConfigurerAdapter被deprecated,虽然继承WebMvcConfigurerAdapter这个类虽然有此便利,但在Spring5.0里面已经deprecated了。 官方文档也说了,WebMvcConfigurer接口现在已经有了默认的空白方法,所以在Springboot2.0(Spring5.0)下更好的做法还是implements WebMvcConfigurer。

拦截器

目录

拦截器

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

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 用来设置白名单,也就是不需要触发这个拦截器的路径。

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指定即可,代码如下:

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

配置静态资源

在application.properties 或 application.yml指定静态资源拦截,要不然静态资源会被拦截。

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

前台静态文件路径配置

<link th:href="@{/static/js/hplus/css/bootstrap.min14ed.css}" rel="stylesheet">
复制代码

转载于:https://juejin.im/post/5c3707e46fb9a049f74665ee

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值