Hello大家好,本章我們新增攔截器功能 。有問題可以聯絡我mr_beany@163.com。另求各路大神指點,感謝
在web開發的過程中,為了實現登入許可權驗證,安全驗證等,我們往往需要新增一個攔截器在使用者的的請求到達controller層的時候實現登入驗證,那麼SpringBoot如何新增攔截器呢?
一:新增攔截器
開啟core→configurer→WebConfigurer
新增如下
/**
* TODO 修改為自己的需求
*/
private static final String IZATION = "CHUCHEN";
/**
* 新增攔截器 請求頭攔截
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(
//注意,HandlerInterceptorAdapter 這裡可以修改為自己建立的攔截器
new HandlerInterceptorAdapter() {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String ization = request.getHeader("ization");
if(IZATION.equals(ization)){
return true;
}else{
RetResult<Object> result = new RetResult<>();
result.setCode(RetCode.UNAUTHORIZED).setMsg("簽名認證失敗");
responseResult(response, result);
return false;
}
}
}
//這裡新增的是攔截的路徑 /**為全部攔截
).addPathPatterns("/userInfo/selectAlla");
}private void responseResult(HttpServletResponse response, RetResult<Object> result) {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-type", "application/json;charset=UTF-8");
response.setStatus(200);
try {
response.getWriter().write(JSON.toJSONString(result, SerializerFeature.WriteMapNullValue));
} catch (IOException ex) {
LOGGER.error(ex.getMessage());
}
}複製程式碼
二:測試
輸入localhost:8080/userInfo/selectAll
輸入localhost:8080/userInfo/selectAlla
在header中新增簽名
三:自己建立攔截器
建立core→interceptor→Interceptor1
package com.example.demo.core.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 Interceptor1 implements HandlerInterceptor {
/**
* 在請求處理之前進行呼叫(Controller方法呼叫之前)
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println(">>>MyInterceptor1>>>>>>> preHandle");
// 只有返回true才會繼續向下執行,返回false取消當前請求
return true;
}
/**
* 請求處理之後進行呼叫,但是在檢視被渲染之前(Controller方法呼叫之後)
* @param request
* @param response
* @param handler
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println(">>>MyInterceptor1>>>>>>> postHandle");
}
/**
* 在整個請求結束之後被呼叫,也就是在DispatcherServlet 渲染了對應的檢視之後執行(主要是用於進行資源清理工作)
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println(">>>MyInterceptor1>>>>>>> afterCompletion");
}
}複製程式碼
四:修改WebConfigurer配置
修改addInterceptors 方法如下
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(
//注意,HandlerInterceptorAdapter 這裡可以修改為自己建立的攔截器
new Interceptor1() {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String ization = request.getHeader("ization");
if(IZATION.equals(ization)){
return true;
}else{
RetResult<Object> result = new RetResult<>();
result.setCode(RetCode.UNAUTHORIZED).setMsg("簽名認證失敗");
responseResult(response, result);
return false;
}
}
}
//這裡新增的是攔截的路徑 /**為全部攔截
).addPathPatterns("/userInfo/selectAlla");
}複製程式碼
五:測試
輸入localhost:8080/userInfo/selectAlla
在header中新增簽名
成功
專案地址
碼雲地址: gitee.com/beany/mySpr…
GitHub地址: github.com/MyBeany/myS…
寫文章不易,如對您有幫助,請幫忙點下star
結尾
新增攔截器功能已完成,後續功能接下來陸續更新,有問題可以聯絡我mr_beany@163.com。另求各路大神指點,感謝大家。