您好,登錄后才能下訂單哦!
新建包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");???????//添加不攔截路徑 ????} }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。