string boot 與 自定義interceptor的實(shí)例講解
前面學(xué)習(xí)過(guò)過(guò)濾器, 但是過(guò)濾器是針對(duì)servlet的, 用在springmvc和spring boot里面, 功能上, 感覺并不是很好用.
那這里來(lái)學(xué)習(xí)一下攔截器.
一. 攔截器的執(zhí)行順序
1. 目錄
2. 攔截器
攔截器里面, 我加了三個(gè)(First,Two,Third), 但是內(nèi)容都差不多.
package org.elvin.boot.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println("FirstInterceptor preHandle"); return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("FirstInterceptor postHandle"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("FirstInterceptor afterCompletion"); } }
preHandle 返回true, 才會(huì)繼續(xù)下面的執(zhí)行.
攔截器注冊(cè):
package org.elvin.boot.interceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class RegisterInterceptor extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new FirstInterceptor()); registry.addInterceptor(new TwoInterceptor()); registry.addInterceptor(new ThirdInterceptor()); super.addInterceptors(registry); } }
為了驗(yàn)證執(zhí)行順序, 這里使用了 thymeleaf, 然后在前臺(tái)訪問了我后臺(tái)傳過(guò)去的屬性, 在訪問的時(shí)候, 就會(huì)打印信息到控制臺(tái)
package org.elvin.boot.pojo; public class Book { private String name ; public String getName() { System.out.println("view : Book'name is " + name); return name; } public void setName(String name) { this.name = name; } }
Controller:
package org.elvin.boot.Controller; import org.elvin.boot.pojo.Book; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("first") public class FirstController { private String controllerPath = "first/"; @GetMapping("index") public String index(Model model){ System.out.println("controller : FirstController index doing..."); Book book = new Book(); book.setName("spring boot"); model.addAttribute("book", book); return controllerPath + "index"; } }
View:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Title</title> </head> <body> <h1 th:text="${book.name}"></h1> </body> </html>
在訪問 localhost:8080/first/index 的時(shí)候, 就會(huì)在控制臺(tái)輸出響應(yīng)的信息.
這樣, 就能看出單個(gè)攔截器的執(zhí)行順序.
1. 在控制器方法執(zhí)行之前, 執(zhí)行的 preHandle 方法
2. 執(zhí)行控制器的action方法
3. 執(zhí)行完action, 解析view之前(如果有的話), 執(zhí)行攔截器的 posthandle 方法
4. 解析view
5. 解析完之后, 執(zhí)行 afterCompletion 方法
當(dāng)注冊(cè)多個(gè)攔截器的時(shí)候, 執(zhí)行順序, 如圖上所示了.
二. 攔截器實(shí)現(xiàn)權(quán)限驗(yàn)證
同樣的, 先加入權(quán)限攔截器
package org.elvin.boot.interceptor; import org.elvin.boot.annotation.NoLogin; import org.springframework.util.StringUtils; import org.springframework.web.method.HandlerMethod; 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 handle) throws Exception { HandlerMethod method = (HandlerMethod ) handle; Class<?> controllerType = method.getBeanType(); if(method.getMethodAnnotation(NoLogin.class) != null || controllerType.getAnnotation(NoLogin.class) != null){ return true; } HttpSession session = request.getSession(); String token = (String)session.getAttribute("token"); if(!StringUtils.isEmpty(token)){ return true; } response.sendRedirect("/login/index"); return false; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { } }
然后注冊(cè)權(quán)限攔截器
package org.elvin.boot.interceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class RegisterInterceptor extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()); super.addInterceptors(registry); } }
在控制器中加入登錄控制器, 提供登錄頁(yè)面和注銷方法
package org.elvin.boot.Controller; import org.elvin.boot.annotation.NoLogin; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @NoLogin @Controller @RequestMapping("login") public class LoginController { @Autowired private HttpServletRequest request; @Autowired private HttpServletResponse response; private String controllerPath = "login/"; //@NoLogin @GetMapping("index") public String index(){ HttpSession session = request.getSession(); session.setAttribute("token", "token"); return controllerPath + "index"; } //@NoLogin @PostMapping("checkOut") @ResponseBody public String checkOut(){ HttpSession session = request.getSession(); session.setAttribute("token", null); return "ok"; } }
這里我做了一個(gè)免登錄注解, 可以加在Controller上, 也可以加在 action 上.
package org.elvin.boot.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface NoLogin { }
注解里面, 并不需要任何內(nèi)容.
登錄頁(yè)面(這里登錄頁(yè)面只是為了注銷用的, 所以訪問過(guò)這個(gè)頁(yè)面之后, 就表示登錄成功了).
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <div class="container"> <input type="button" value="注銷" id="checkOut"/> </div> <script th:src="@{/js/jquery-1.11.1.js}"></script> <script th:inline="javascript"> $(function () { $(".container").delegate("#checkOut", "click", function () { $.ajax({ url: [[@{/login/checkOut}]], type: 'post', data: {}, success: function (res) { if (res == "ok") { alert("注銷成功"); } } }); }); }); </script> </body> </html>
結(jié)果演示方式:
在瀏覽器中, 先打開 http://localhost:8080/login/index 頁(yè)面, 然后在新標(biāo)簽中訪問 http://localhost:8080/first/index 頁(yè)面.
你會(huì)發(fā)現(xiàn)訪問 first/index 的時(shí)候, 是可以訪問的.
此時(shí), 在login/index頁(yè)面中, 點(diǎn)擊注銷按鈕之后, 再刷新 first/index 頁(yè)面, 就會(huì)直接跳去登錄頁(yè)面.
以上這篇string boot 與 自定義interceptor的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java如何進(jìn)行Base64的編碼(Encode)與解碼(Decode)
這篇文章主要介紹了詳解Java如何進(jìn)行Base64的編碼(Encode)與解碼(Decode),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03如何使用JAVA實(shí)現(xiàn)數(shù)字水印
本文介紹了如何使用JAVA實(shí)現(xiàn)數(shù)字水印,主要用到了java.awt包中的AlphaComposite類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-07-07JSONObject?toJSONString錯(cuò)誤的解決
這篇文章主要介紹了JSONObject?toJSONString錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java網(wǎng)絡(luò)編程之UDP實(shí)現(xiàn)原理解析
UDP實(shí)現(xiàn)通信非常簡(jiǎn)單,沒有服務(wù)器,每個(gè)都是客戶端,每個(gè)客戶端都需要一個(gè)發(fā)送端口和一個(gè)接收端口,本文給大家介紹Java網(wǎng)絡(luò)編程之UDP實(shí)現(xiàn)原理解析,感興趣的朋友一起看看吧2021-09-09