您好,登錄后才能下訂單哦!
新建包c(diǎn)om.example.interceptor;
創(chuàng)建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?{???????//請(qǐng)求進(jìn)入這個(gè)攔截器 ????????HttpSession?session?=?request.getSession();????????if(session.getAttribute("user")?==?null){???????//判斷session中有沒有user信息//????????????System.out.println("進(jìn)入攔截器"); ????????????if("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))){ ????????????????response.sendError(401); ????????????} ????????????response.sendRedirect("/");?????//沒有user信息的話進(jìn)行路由重定向 ????????????return?false; ????????}????????return?true;????????//有的話就繼續(xù)操作 ????}????@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??????????//使用注解?實(shí)現(xiàn)攔截public?class?WebAppConfigurer?implements?WebMvcConfigurer???{????@Autowired ????RightsInterceptor?rightsInterceptor;????@Override ????public?void?addInterceptors(InterceptorRegistry?registry)?{????????//登錄攔截的管理器 ????????InterceptorRegistration?registration?=?registry.addInterceptor(new?LoginInterceptor());?????//攔截的對(duì)象會(huì)進(jìn)入這個(gè)類中進(jìn)行判斷 ????????registration.addPathPatterns("/**");????????????????????//所有路徑都被攔截 ????????registration.excludePathPatterns("/","/login","/error","/static/**","/logout");???????//添加不攔截路徑 ????} }
在WebAppConfigurer.java中增加內(nèi)容
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??????????//使用注解?實(shí)現(xiàn)攔截public?class?WebAppConfigurer?implements?WebMvcConfigurer???{????@Autowired ????RightsInterceptor?rightsInterceptor;????@Override ????public?void?addInterceptors(InterceptorRegistry?registry)?{????????//登錄攔截的管理器 ????????InterceptorRegistration?registration?=?registry.addInterceptor(new?LoginInterceptor());?????//攔截的對(duì)象會(huì)進(jìn)入這個(gè)類中進(jìn)行判斷 ????????registration.addPathPatterns("/**");????????????????????//所有路徑都被攔截 ????????registration.excludePathPatterns("/","/login","/error","/static/**","/logout");???????//添加不攔截路徑//????????super.addInterceptors(registry); ????????//權(quán)限攔截的管理器 ????????InterceptorRegistration?registration1?=?registry.addInterceptor(rightsInterceptor); ????????registration1.addPathPatterns("/**");????????????????????//所有路徑都被攔截 ????????registration1.excludePathPatterns("/","/login","/error","/static/**","/logout");???????//添加不攔截路徑 ????} }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。