最近使用SpringBoot2.X搭建了一個項目,大部分接口都須要作登陸校驗,因此打算使用註解+攔截器來實現,在此記錄下實現過程。java
1、實現原理web
1. 自定義一個註解@NeedLogin,若是接口須要進行登陸校驗,則在接口方法或類方法上添加該註解。
2. 登陸攔截器LoginInterceptor校驗接口的方法或類上是否有@NeedLogin註解,有註解則進行登陸校驗。spring
2、主要代碼session
1. NeedLogin註解代碼app
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. 登陸攔截器LoginInterceptoride
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. 配置攔截器post
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("/**"); } }
3、驗證代碼測試
HelloController中添加兩個方法,testNeedLogin()方法添加登陸攔截,testNoLogin()方法不須要登陸攔截。blog
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運行截圖以下:
上述三步操做完成後便可實現登陸攔截