您好,登錄后才能下訂單哦!
攔截器,請求的接口被訪問之前,進行攔截然后在之前或之后加入某些操作。攔截是AOP的一種實現策略。 攔截器主要用來按照指定規則拒絕請求。
Token令牌驗證
請求數據校驗
用戶權限校驗
放行指定接口
自定義類實現HandlerInterceptor接口
1)OneInterceptor 攔截器
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 攔截器一
*/
public class OneInterceptor implements HandlerInterceptor {
private static final Logger LOGGER
= LoggerFactory.getLogger(OneInterceptor.class.getName());
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object o) throws Exception {
String url =String.valueOf(request.getRequestURL()) ;
LOGGER.info("1、url=="+url);
// 放開攔截
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, ModelAndView modelAndView) throws Exception {
LOGGER.info("1、postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, Exception e) throws Exception {
LOGGER.info("1、afterCompletion");
}
}
2)TwoInterceptor 攔截器
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 攔截器二
*/
public class TwoInterceptor implements HandlerInterceptor {
private static final Logger LOGGER
= LoggerFactory.getLogger(TwoInterceptor.class.getName());
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object o) throws Exception {
String url =String.valueOf(request.getRequestURL()) ;
LOGGER.info("2、url=="+url);
// 放開攔截
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, ModelAndView modelAndView) throws Exception {
LOGGER.info("2、postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, Exception e) throws Exception {
LOGGER.info("2、afterCompletion");
}
}
import com.boot.intercept.intercept.OneInterceptor;
import com.boot.intercept.intercept.TwoInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Web配置文件
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
// 攔截所有路徑
// 注冊自定義兩個攔截器
registry.addInterceptor(new OneInterceptor()).addPathPatterns("/**");
registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/**");
}
}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class InterceptController {
@RequestMapping("/reqUrl")
public String reqUrl (){
return "success" ;
}
}
日志輸出內容如下
intercept.OneInterceptor : 1、url==http://127.0.0.1:8005/reqUrl
intercept.TwoInterceptor : 2、url==http://127.0.0.1:8005/reqUrl
intercept.TwoInterceptor : 2、postHandle
intercept.OneInterceptor : 1、postHandle
intercept.TwoInterceptor : 2、afterCompletion
intercept.OneInterceptor : 1、afterCompletionla
攔截器的攔截順序,是按照Web配置文件中注入攔截器的順序執行的。
GitHub地址:知了一笑
https://github.com/cicadasmile
碼云地址:知了一笑
https://gitee.com/cicadasmile
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。