Spring Boot攔截器配置攔截登陸
一,pom.xml的配置
這裡很簡單,先引入spring-boot-starter-parent,parent 是父模組,由父模組統一進行 spring-boot 版本管理,dependencies 中與 spring-boot 啟動繫結的包不需要再指定版本。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
二,新建WebConfigurer
1,如圖,新建的 config 包,用來裝初始化檔案,在配置之下新建 WebConfigurer。
2,WebConfigurer需要實現 WebMvcConfigurer 這個介面,並實現裡面的兩個方法。(在老版本的 spring-boot 中使用的是WebMvcConfigurerAdapter,新版本中已過時!!!還有不能通過繼承 WebMvcConfigurationSupport 這個類來實現,這樣會在某些情況下失效!!!),第二個 addInterceptors 方法用來註冊新增攔截器。
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class WebConfigurer implements WebMvcConfigurer { // 這個方法是用來配置靜態資源的,比如html,js,css,等等 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { } // 這個方法用來註冊攔截器,我們自己寫好的攔截器需要通過這裡添加註冊才能生效 @Override public void addInterceptors(InterceptorRegistry registry) { } }
三,新建登陸攔截器
如下圖所示,在 config 包下新建一個 intercepors 包,用來裝攔截器。然後在攔截器下新建 LoginInterceptor,用來攔截驗證登陸。
2,每一個攔截器有需要實現的 HandlerInterceptor 介面,這個介面有三個方法,每個方法會在請求呼叫的不同時期完成,因為我們需要在介面呼叫之前攔截請求判斷是否登陸,所以這裡需要使用 preHandle 方法,在裡面寫驗證邏輯,最後返回 true 或者 false,確定請求是否合法。記住加 @Component 註解,我們需要在上一步的 WebConfigurer 類中注入。使用者是自定義的使用者類,大家可以自己定義,我這裡就不貼出來了。
import com.impte.study.domain.po.User;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
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;
@Component
public class LoginInterceptor implements HandlerInterceptor {
//這個方法是在訪問介面之前執行的,我們只需要在這裡寫驗證登陸狀態的業務邏輯,就可以在使用者呼叫指定介面之前驗證登陸狀態了
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//每一個專案對於登陸的實現邏輯都有所區別,我這裡使用最簡單的Session提取User來驗證登陸。
HttpSession session = request.getSession();
//這裡的User是登陸時放入session的
User user = (User) session.getAttribute("user");
//如果session中沒有user,表示沒登陸
if (user == null){
//這個方法返回false表示忽略當前請求,如果一個使用者呼叫了需要登陸才能使用的介面,如果他沒有登陸這裡會直接忽略掉
//當然你可以利用response給使用者返回一些提示資訊,告訴他沒登陸
return false;
}else {
return true; //如果session裡有user,表示該使用者已經登陸,放行,使用者即可繼續呼叫自己需要的介面
}
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
}
}
四,在WebConfigurer中新增攔截器
1,首先將 LoginInterceptor 注入到 WebConfigurer 中。
@Autowired
private LoginInterceptor loginInterceptor;
2,然後在 WebConfigurer 中的 addInterceptors 中新增攔截器,使其生效。
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns("/**") 表示攔截所有的請求,
// excludePathPatterns("/login", "/register") 表示除了登陸與註冊之外,因為登陸註冊不需要登陸也可以訪問
registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/login", "/register");
super.addInterceptors(registry);
}
3,addPathPatterns 用來設定攔截路徑,excludePathPatterns 用來設定白名單,也就是不需要觸發這個攔截器的路徑。
完整程式碼:
import com.impte.study.config.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginInterceptor;
// 這個方法是用來配置靜態資源的,比如html,js,css,等等
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
// 這個方法用來註冊攔截器,我們自己寫好的攔截器需要通過這裡添加註冊才能生效
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns("/**") 表示攔截所有的請求,
// excludePathPatterns("/login", "/register") 表示除了登陸與註冊之外,因為登陸註冊不需要登陸也可以訪問
registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/login", "/register");
super.addInterceptors(registry);
}
}
五,總結
我在這裡只是用登陸的例子來展現來展現攔截器的基本使用,攔截器用途很廣,比如可以用來進行介面許可權控制。如果我的文章對你有幫助,請關注一波,CSDN新人駕到,還望各路大神多多照顧!