SpringBoot实现自定义拦截器

最近在写一个很简单的实习演示项目,因为项目很小,觉得用springsecurity有点 可以但没必要的意味。觉得自己手撸一个简单的拦截器,来实现登录的安全验证。

 

在网上查了一下,一般是继承WebMvcConfigurerAdapter这个类,但发现过时了

现在都是直接继承它的父类 WebMvcConfigurer

 

1、LoginInterceptor

/**
 * @Auther: zj
 * @Date: 2019/4/17 15:51
 * @Description: 拦截器
 */
@Component
//1、创建自己的拦截器类并实现 HandlerInterceptor 接口。
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 进入controller层之前拦截请求
     * */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getParameter( "token" )==null) {
            response.sendRedirect( request.getContextPath()+"/error.html" );
            return false;
        }
        return true;
    }

    /**
     * 处理请求完成后视图渲染之前的处理操作
     *
     * 请求之后,控制器中抛出了异常的话就不会执行
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println("Interceptor postHandle");
    }

    /**
     * 视图渲染之后的操作
     * 请求之后调用,不管抛不抛出异常都会被调用.参数中异常如果被异常处理器调用的话就不会传入到参数中.
     * */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        System.out.println("Interceptor afterCompletion");
    }
}

这个类,主要在3个时间 执行的操作,我这边仅仅是为了截取请求是否携带token的参数

2、LoginInterceptorConfiguration

/**
 * @Auther: zj
 * @Date: 2019/4/17 15:58
 * @Description: 拦截器config
 */
@Configuration
//2、创建一个Java类继承WebMvcConfigurerAdapter,并重写 addInterceptors 方法。
public class LoginInterceptorConfiguration implements WebMvcConfigurer {

    //3、实例化自定义的拦截器
    @Resource
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //如果interceptor中不注入redis或其他项目可以直接new
        //4、将拦截器对像手动添加到拦截器链中(在addInterceptors方法中添加)
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns( "/login" )
                                                                        .excludePathPatterns("/test/**")
                                                                        .excludePathPatterns( "/static/**" );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler( "/static/**" ).
                addResourceLocations( "classpath:/static/" );
    }


}

这个config类,主要是为了实例化刚才新建的拦截器类。

addPathPatterns 添加拦截的请求路径  
excludePathPatterns 就代表放行的请求
addResourceHandler 对静态资源的放行

3、测试

TestController:


@RestController
public class TestController {

  

    @GetMapping("/test/aaa")
    public String testad() {
        return "111";
    }

    @GetMapping("/aaa")
    public String testadaa() {
        return "111";
    }

    @GetMapping("/test/html")
    public void testhtml(HttpServletResponse response) throws IOException {
        response.sendRedirect( "/static/webSocketTest.html" );
    }



}

打开浏览器:

但前面没加/test时,会自动重定向到error.html

其实上面的静态资源被拦截了

浏览器输入localhost:8080/test/html时

~~ 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小牛呼噜噜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值