SpringBoot实现登录拦截器

在springBoot只给你如果不设置登录拦截器的话,在任何情况下都可以访问该项目中的所有资源

  1. 拦截器主要方法两个
    在这里插入图片描述
    1.LoginInterceptor 主要继承HandlerInterceptor 类来实现
package com.example.erp_project.config;

import com.example.erp_project.entity.UserEntity;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author Lolo don‘t feel
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        //从seesion中取出当前登录的信息,
        Object account = session.getAttribute("phone");
        //如果为空跳转到登录页面,并弹出提示信息
        if (account == null) {
            //System.out.println("用户未登陆");
            //PageController中会跳转到同名的页面
            response.sendRedirect("login");
            return false;
        } else {
            return true;
        }
    }
}

2.WebConfigurer 主要继承WebMvcConfigurer 类实现

package com.example.erp_project.config;

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;

/**
 * @author Lolo don‘t feel
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截所有的资源addPathPatterns("/**"),释放登陆相关的资源excludePathPatterns(
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(
                "/js/**", "/layui/**", "/dist/sliderVerify/**","/lib/**","/css/**",
                "/images/**", "/register", "/login", "/restPassword",
                "/agreement","/auth/login","/auth/register","/auth/restPassword"
        );
    }
}

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot实现登录拦截器可以通过编写一个实现了HandlerInterceptor接口的拦截器类来实现。以下是实现的步骤: 1. 创建一个实现HandlerInterceptor接口的拦截器类,例如LoginInterceptor。 2. 在拦截器类中实现preHandle方法,该方法在请求处理之前被调用。在该方法中,可以进行登录验证等操作。如果需要拦截请求并返回错误信息,可以使用response对象进行处理。 3. 在Spring Boot的配置文件中注册拦截器。可以通过实现WebMvcConfigurer接口中的addInterceptors方法来注册拦截器。 下面是一个示例代码: LoginInterceptor.java ``` public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 进行登录验证等操作 // 如果验证不通过,可以使用response对象返回错误信息 return true; // 返回true表示放行请求,返回false表示拦截请求 } } ``` WebMvcConfigurer.java ``` @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/**") // 拦截所有请求 .excludePathPatterns("/login"); // 不拦截登录请求 } } ``` 在上述代码中,LoginInterceptor类实现了HandlerInterceptor接口,并在preHandle方法中进行登录验证等操作。在WebMvcConfig类中重写了addInterceptors方法,注册了LoginInterceptor拦截器,并指定了拦截所有请求,但不拦截登录请求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值