Springboot中使用自定义拦截器(springboot2.7.9)-实现登录拦截

重点:Springboot中使用拦截器需要实现WebMvcConfigurer 接口,重写addInterceptors方法,并注入。

1、环境说明

案例中使用IDEA+springboot2.7.9+jdk1.8+mysql版本开发

2、创建工程

选择springboot版本+springbweb依赖

81a8f85289fe48d3a423e37d3624ca25.png

3、项目创建成功后包依赖如下

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.9</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.14</version>
    </dependency>
</dependencies>

4、自定义类实现拦截器HandlerInterceptor 接口

案例说明:访问资源的时候拦截器实现获取session中user的值,如果值不存在就重定向

如果值存在就放行

public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("===============拦截器===============");
        //从session中获取user的信息
        User user = (User) request.getSession().getAttribute("user");
        //判断用户是否登录
        if (null == user) {
            response.sendRedirect("user/error");//可以写你想重定向的路径
            //request.setAttribute("msg", "请先登录");
            //request.getRequestDispatcher("/login.html").forward(request, response);
            return false;
        }
        return true;
    }
}

5、将自定义拦截器注入到系统中

需要实现WebMvcConfigurer 类重写addInterceptors方法

重点:自定义配置类InterceptorConfig 需要使用注解@Component进行实例,否则不生效

@Component
public class InterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] addPathPatterns = {//拦截所有请求
                "/**"
        };
        String[] excludePathPatterns = {//设置不想拦截的路径
                "/user/login","/error"
        };
        registry.addInterceptor(new UserInterceptor()).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);
    }
}

6、启动类代码

@SpringBootApplication
@ComponentScan(basePackages ="com.txc")
public class SpringinterceptorApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringinterceptorApplication.class, args);
    }
}

 

 7、创建测试类

 

@Controller
public class UserController {
    @RequestMapping("/test")
    public String test(){
        System.out.println("=========test===========");
        return "";
    }
}

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

雾林小妖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值