泉州北京網(wǎng)站建設(shè)如何制作app軟件
攔截器簡(jiǎn)介
攔截器是屬于springmvc體系的,只能攔截controller的請(qǐng)求。攔截器(Interceptor)是一種動(dòng)態(tài)攔截方法調(diào)用的機(jī)制,在SpringMVC中動(dòng)態(tài)攔截控制器方法的執(zhí)行。
Interceptor 作用
- 日志記錄:記錄請(qǐng)求信息的日志,以便進(jìn)行信息監(jiān)控、信息統(tǒng)計(jì)、計(jì)算 PV(Page View)等;
權(quán)限檢查:如登錄檢測(cè),進(jìn)入處理器檢測(cè)是否登錄; - 性能監(jiān)控:通過(guò)攔截器在進(jìn)入處理器之前記錄開(kāi)始時(shí)間,在處理完后記錄結(jié)束時(shí)間,從而得到該請(qǐng)求的處理時(shí)間。(反向代理,如 Apache 也可以自動(dòng)記錄)
- 通用行為:讀取 Cookie 得到用戶(hù)信息并將用戶(hù)對(duì)象放入請(qǐng)求,從而方便后續(xù)流程使用,還有如提取 Locale、Theme 信息等,只要是多個(gè)處理器都需要的即可使用攔截器實(shí)現(xiàn)。
SpringBoot 提供了 Interceptor 攔截器機(jī)制,用于請(qǐng)求的預(yù)處理和后處理。在 SpringBoot 中定義一個(gè)攔截器有兩種方法:第一種是實(shí)現(xiàn) HandlerInterceptor 接口,或者繼承實(shí)現(xiàn)了 HandlerInterceptor 接口的類(lèi)(例如:HandlerInterceptorAdapter);第二種方法時(shí)實(shí)現(xiàn) Spring 的 WebRequestInterceptor 接口,或者繼承實(shí)現(xiàn)了 WebRequestInterceptor 接口的類(lèi)。這些攔截器都是在Handler的執(zhí)行周期內(nèi)進(jìn)行攔截操作的。
示例
第一步:創(chuàng)建攔截器類(lèi)
創(chuàng)建攔截器類(lèi),讓其實(shí)現(xiàn)handlerIntercepter接口,在其preHandle()方法中作攔截判斷,注意該方法返回true表示不攔截繼續(xù)往下執(zhí)行,返回false表示攔截不再往下執(zhí)行
public class LoginHandler implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {Object user = request.getSession().getAttribute("user");if(user == null){//沒(méi)有登錄System.out.println("沒(méi)有登錄");response.sendRedirect(request.getContextPath()+"/user/openLogin");return false;}else {//已經(jīng)登錄System.out.println("已經(jīng)登錄");return true;}}
}
HandlerInterceptor中定義了如下三個(gè)默認(rèn)方法:
- preHandle:在Action執(zhí)行前調(diào)用
- postHandle:在Action執(zhí)行后調(diào)用,生成視圖前調(diào)用
- afterCompletion:在DispatcherServlet完全處理完請(qǐng)求之后被調(diào)用,可用于清理資源
第二步:創(chuàng)建配置類(lèi)
創(chuàng)建配置類(lèi),讓其實(shí)現(xiàn)WebMvcConfigurer接口,在其addInterceptors()方法中對(duì)1中創(chuàng)建的攔截器進(jìn)行配置
@Configuration
public class WegoMvcConfigure implements WebMvcConfigurer {/*** 攔截器配置*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {//注冊(cè)Interceptor攔截器InterceptorRegistration registration = registry.addInterceptor(new LoginHandler());registration.addPathPatterns("/**"); //所有路徑都被攔截registration.excludePathPatterns( //添加不攔截路徑"/user/openLogin", //登錄頁(yè)面"/user/login", //登錄請(qǐng)求"/**/*.html", //html靜態(tài)資源"/**/*.js", //js靜態(tài)資源"/**/*.css" //css靜態(tài)資源);}
}
第三步:定義用戶(hù)登錄和退出登錄的控制器
@Controller
@RequestMapping("/user")
public class UserController {@Resourceprivate UserService userService;/*** 打開(kāi)登錄頁(yè)面*/@GetMapping("/openLogin")public String openLogin(){return "frontend/login";}/*** 登錄*/@PostMapping("/login")public String login(String username,String password, HttpSession session){User user = userService.getUserByUsernameAndPassword(username , password);if (user == null) {// 登錄失敗,打開(kāi)登錄頁(yè)面return "frontend/login";}else {//登錄成功,將用戶(hù)信息保存到Session中,打開(kāi)首頁(yè)session.setAttribute("user",pageBean.getResult().get(0));return "redirect:/index";//重定向}}/*** 退出登錄 */@ResponseBody@GetMapping("/logout")String logout(HttpSession session){session.removeAttribute("user");return "success";}
}
第四步:測(cè)試
- 啟動(dòng)項(xiàng)目,請(qǐng)求首頁(yè)localhost/wego/index,發(fā)現(xiàn)直接打開(kāi)登錄頁(yè)面
- 在登錄頁(yè)面中登錄,成功后進(jìn)入首頁(yè)
- 請(qǐng)求localhost/wego/user/logout,退出登錄
- 再次請(qǐng)求登錄頁(yè)面發(fā)現(xiàn)又會(huì)打開(kāi)登錄頁(yè)面