SpringBoot 自定义拦截器(内含源代码)

导读:本篇文章讲解 SpringBoot 自定义拦截器(内含源代码),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

SpringBoot 自定义拦截器(内含源代码)

源代码下载链接:https://download.csdn.net/download/weixin_46411355/87399903

一、自定义拦截器

创建登录拦截器

com/bjpowernode/springbootinterceptor02/interceptor
LoginInterceptor.java

package com.bjpowernode.springbootinterceptor02.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

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

public class LoginInterceptor implements HandlerInterceptor {
    /**
     *
     * @param request
     * @param response
     * @param handler 被拦截器的控制器对象
     * @return boolean
     *  true:请求能被controller处理
     *  false:请求被截断
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了LoginInterceptor的preHandle");
        return true;
    }
}

二、编写控制器

com/bjpowernode/springbootinterceptor02/controller
BootController.java

package com.bjpowernode.springbootinterceptor02.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BootController {

    @RequestMapping("/user/userAccount")
    @ResponseBody
    public String userAccount(){
        return "访问user/userAccount";
    }

    @RequestMapping("/user/userLogin")
    @ResponseBody
    public String userLogin(){
        return "访问user/userLogin";
    }
}

三、添加拦截器对象,注入到容器的配置类中

package com.bjpowernode.springbootinterceptor02.config;

import com.bjpowernode.springbootinterceptor02.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器对象,注入到容器中
        // 可以不采用new的方式直接在登录拦截器中加入@Component注解 在本类中用@Autowired注入
        LoginInterceptor loginInterceptor = new LoginInterceptor();


        //指定拦截的地址
        String path[] = {"/user/**"};
        //指定不拦截的地址
        String excludePath[] = {"/user/userLogin"};

        registry.addInterceptor(loginInterceptor)
                .addPathPatterns(path)
                .excludePathPatterns(excludePath);

    }
}

另一种写法

也可以使用@Component注解将LoginController交给Spring容器管理
在这里插入图片描述
则在添加拦截器对象,注入到容器的配置类中
不需要使用new LoginController来创建对象
直接通过@Autowired注解注入即可
在这里插入图片描述

四、最后application运行

访问 localhost:8080/user/userLogin,拦截器放行
在这里插入图片描述
访问localhost:8080user/userAccount,拦截器生效
在这里插入图片描述
控制台输出:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/85520.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!