Spring Boot2.0 設置攔截器

全部功能完成 配置登陸認證

配置攔截器

在spring boot2.0 以後 經過繼承這個WebMvcConfigurer類 就能夠完成攔截
  • 新建包com.example.interceptor;
  • 建立login攔截類
package com.example.interceptor;

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;


public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {       //請求進入這個攔截器
        HttpSession session = request.getSession();
        if(session.getAttribute("user") == null){       //判斷session中有沒有user信息
//            System.out.println("進入攔截器");
            if("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))){
                response.sendError(401);
            }
            response.sendRedirect("/");     //沒有user信息的話進行路由重定向
            return false;
        }
        return true;        //有的話就繼續操做
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
  • 在com.example包中添加攔截控制器
package com.example;


import com.example.interceptor.LoginInterceptor;
import com.example.interceptor.RightsInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;



@Configuration          //使用註解 實現攔截
public class WebAppConfigurer implements WebMvcConfigurer   {

    @Autowired
    RightsInterceptor rightsInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登陸攔截的管理器
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());     //攔截的對象會進入這個類中進行判斷
        registration.addPathPatterns("/**");                    //全部路徑都被攔截
        registration.excludePathPatterns("/","/login","/error","/static/**","/logout");       //添加不攔截路徑

    }

}
  • 在WebAppConfigurer.java中增長內容
package com.example;


import com.example.interceptor.LoginInterceptor;
import com.example.interceptor.RightsInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;



@Configuration          //使用註解 實現攔截
public class WebAppConfigurer implements WebMvcConfigurer   {

    @Autowired
    RightsInterceptor rightsInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //登陸攔截的管理器
        InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());     //攔截的對象會進入這個類中進行判斷
        registration.addPathPatterns("/**");                    //全部路徑都被攔截
        registration.excludePathPatterns("/","/login","/error","/static/**","/logout");       //添加不攔截路徑
//        super.addInterceptors(registry);


        //權限攔截的管理器
        InterceptorRegistration registration1 = registry.addInterceptor(rightsInterceptor);
        registration1.addPathPatterns("/**");                    //全部路徑都被攔截
        registration1.excludePathPatterns("/","/login","/error","/static/**","/logout");       //添加不攔截路徑
    }

}
相關文章
相關標籤/搜索