Springboot实现拦截器功能

2年前 (2022) 程序员胖胖胖虎阿
143 0 0

Springboot实现拦截器功能

preHandle: 预先处理,在目标的controller方法执行之前,进行处理

postHandle: 在目标的controller方法执行之后,到达指定页面之前进行处理

afterCompletion: 在页面渲染之后进行处理

方法:

1.Springboot通过实现HandlerInterceptor接口实现拦截器

2.通过WebMvcConfigurer实现一个配置类,再通过@Configuration 注解注入到容器

3.指定拦截规则

 以用户登录为案例,若用户没有登录session里面就没有用户的数据,就会转到首页登录页面

在正确登录之后,就将reglister保存到session中,再次访问页面的时候,登录拦截器就可以找到这个reglister对象,就不需要再次拦截到登录界面了.

Springboot实现拦截器功能Springboot实现拦截器功能

package com.zwz.springbootweb.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object reglister = session.getAttribute("Reglister");


        if (reglister != null) {
            return true;
        } else {
            request.setAttribute("msg", "请先登录!");

            request.getRequestDispatcher("/").forward(request,response);
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}

 

 之后通过实现WebMvcConfigurer接口实现一个配置类,在配置类中注入拦截器,最后再通过 @Configuration 注解注入配置.并且指定拦截的路径和需要放行的路径.

注意:拦截器  /**   会拦截一切资源,包括静态资源,需要将静态资源放行

Springboot实现拦截器功能

 

package com.zwz.springbootweb.config;

import com.zwz.springbootweb.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UrlPathHelper;


@Configuration
public class WebConfig implements WebMvcConfigurer{

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/loginjudge","/","/retolo","/static/**");
    }

}
版权声明:程序员胖胖胖虎阿 发表于 2022年10月21日 下午2:48。
转载请注明:Springboot实现拦截器功能 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...