如何使用SpringBoot攔截器

這篇文章主要為大家展示了如何使用SpringBoot攔截器,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),市中企業(yè)網(wǎng)站建設(shè),市中品牌網(wǎng)站建設(shè),網(wǎng)站定制,市中網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,市中網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

攔截器簡介

攔截器通常通過動態(tài)代理的方式來執(zhí)行。

攔截器的生命周期由IoC容器管理,可以通過注入等方式來獲取其他Bean的實例,使用更方便。

攔截器配置使用方式

實現(xiàn)攔截器接口:

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

private class AuthenticationInterceptor implements HandlerInterceptor {
	// 在請求處理之前進(jìn)行調(diào)用(Controller方法調(diào)用之前)
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException{
  	System.out.println(request.getRequestURL());
  	User user = (User)request.getSession().getAttribute("USER");
    if(user != null){
    	return true;
    }else {
    	System.out.println("no login...");
    	// request.getRequestDispatcher("/index.html").forward(request, response);
    	response.sendRedirect(request.getContextPath()+"login.html");
    	return false;
    }
    return false;
  }
  
  // 在請求處理之后視圖被渲染之前進(jìn)行調(diào)用(Controller方法調(diào)用之后)
  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    System.out.println("postHandle...");
  }

	// 在請求結(jié)束之后、也就是視圖被渲染之后進(jìn)行調(diào)用(主要是用于進(jìn)行資源清理工作)
  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    System.out.println("afterCompletion...");
  }
}

將攔截器加入到配置中:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebSecurityConfig implements WebMvcConfigurer{
  @Override
  public void addInterceptors(InterceptorRegistry registry){
    registry.addInterceptor(new AuthenticationInterceptor())
    		//所有路徑都被攔截
    		.addPathPatterns("/**")
    		//添加不攔截的路徑
    		.excludePathPatterns("/userLogin", "/css/**", "/images/**", "/js/**", "/login.html");
    registry.addInterceptor(new OtherInterceptor())
        .addPathPatterns("/**");
  }
}

備注:

由于 preHandle、postHandle、afterCompletion 是不同的方法,如果在這些方法之間使用共享變量來儲存值,會存在線程安全問題。而使用過濾器實現(xiàn)則不存在此問題。

以上就是關(guān)于如何使用SpringBoot攔截器的內(nèi)容,如果你們有學(xué)習(xí)到知識或者技能,可以把它分享出去讓更多的人看到。

網(wǎng)站名稱:如何使用SpringBoot攔截器
分享URL:http://muchs.cn/article14/jpepde.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化App設(shè)計、網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化