Spring Boot 2.X 如何新增攔截器?
阿新 • • 發佈:2019-04-20
最近使用SpringBoot2.X搭建了一個專案,大部分介面都需要做登入校驗,所以打算使用註解+攔截器來實現,在此記錄下實現過程。
一、實現原理
1. 自定義一個註解@NeedLogin,如果介面需要進行登入校驗,則在介面方法或類方法上新增該註解。
2. 登入攔截器LoginInterceptor校驗介面的方法或類上是否有@NeedLogin註解,有註解則進行登入校驗。
二、主要程式碼
1. NeedLogin註解程式碼
package com.example.helloSpringBoot.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 登入註解 * * @Author: Java碎碎念 */ @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface NeedLogin { }
2. 登入攔截器LoginInterceptor
package com.example.helloSpringBoot.config; import com.example.helloSpringBoot.annotation.NeedLogin; import com.example.helloSpringBoot.util.WxUserInfoContext; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; 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; /** * 登入攔截器 * * @Author: Java碎碎念 */ @Component public class LoginInterceptor implements HandlerInterceptor { //這個方法是在訪問介面之前執行的,我們只需要在這裡寫驗證登陸狀態的業務邏輯,就可以在使用者呼叫指定介面之前驗證登陸狀態了 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { NeedLogin needLogin = ((HandlerMethod) handler).getMethodAnnotation(NeedLogin.class); if (null == needLogin) { needLogin = ((HandlerMethod) handler).getMethod().getDeclaringClass() .getAnnotation(NeedLogin.class); } // 有登入驗證註解,則校驗登入 if (null != needLogin) { WxUserInfoContext curUserContext = (WxUserInfoContext) request.getSession() .getAttribute("curUserContext"); //如果session中沒有,表示沒登入 if (null == curUserContext) { response.setCharacterEncoding("UTF-8"); response.getWriter().write("未登入!"); return false; } } } return true; } 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 { } }
3. 配置攔截器
package com.example.helloSpringBoot.config; 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.WebMvcConfigurer; /** * WebConfig * * @Author: Java碎碎念 * */ @Configuration public class WebConfig implements WebMvcConfigurer { @Autowired private LoginInterceptor loginInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { // 自定義攔截器,新增攔截路徑和排除攔截路徑 registry.addInterceptor(loginInterceptor).addPathPatterns("/**"); } }
三、驗證程式碼
HelloController中新增兩個方法,testNeedLogin()方法新增登入攔截,testNoLogin()方法不需要登入攔截。
package com.example.helloSpringBoot.controller; import com.example.helloSpringBoot.annotation.NeedLogin; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { /** * 測試不 需要登入 * * */ @RequestMapping("/testNoLogin") public String testNoLogin (){ return "呼叫成功,此介面不需要登入校驗!-Java碎碎念!"; } /** * 測試需要登入 * * */ @NeedLogin @RequestMapping("/testNeedLogin") public String testNeedLogin (){ return "testNeedLogin!"; } }
testNeedLogin執行截圖如下:
testNoLogin執行截圖如下:
上述三步操作完成後即