SpringBoot拦截器的使用与配置

本文介绍了如何在SpringBoot中创建拦截器,实现HandlerInterceptor接口以进行登录判断,并详细阐述了三种配置拦截器的方法:实现WebMvcConfigurer接口(推荐),继承WebMvcConfigurationSupport类(不推荐,可能导致静态资源问题)以及使用已废弃的WebMvcConfigurerAdapter类。在配置过程中,注意拦截器类的@Component注解使用以及避免视图解析错误。
摘要由CSDN通过智能技术生成

一、新建拦截器实现HandlerInterceptor接口

判断是否登录的拦截器

package com.briup.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

public class isLoginInterceptor implements HandlerInterceptor{
   

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
   
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("customer");
		System.out.println("isLoginInterceptor");
		if(obj!=null) {
   
			return true;
		}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值