溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Boot2.0 設(shè)置攔截器

發(fā)布時(shí)間:2020-07-21 12:21:28 來(lái)源:網(wǎng)絡(luò) 閱讀:689 作者:沙漏半杯 欄目:編程語(yǔ)言

所有功能完成 配置登錄認(rèn)證

配置攔截器

在spring boot2.0 之后 通過(guò)繼承這個(gè)WebMvcConfigurer類 就可以完成攔截
  • 新建包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");???????//添加不攔截路徑
????}

}


向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI