1.建立攔截器(如token驗證攔截器)java
package com.antong.api.interceptor; import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.antong.common.exception.AuthException; import com.antong.common.jwt.JwtManager; /** * Copyright: Copyright (c) 2018 zq_tuo * * @ClassName: AuthenticationInterceptor.java * @Description: Token驗證過濾器,判斷是否已登陸以及權限驗證 * @version: v1.0.0 * @author: tuozq * @date: 2018年6月27日 上午10:43:06 * Modification History: * Date Author Version Description *---------------------------------------------------------* * 2018年6月27日 tuozq v1.0.0 修改緣由 */ public class AuthenticationInterceptor implements HandlerInterceptor { public final static String ACCESS_TOKEN = "token"; /* (non-Javadoc) * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object) * 在請求處理以前進行調用(Controller方法調用以前) */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // TODO Auto-generated method stub // 若是不是映射到方法直接經過 if (!(handler instanceof HandlerMethod)) { return true; } System.out.println(request.getRequestURI()); HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); // 判斷是否存在令牌信息,若是存在,則容許登陸 String accessToken = request.getParameter(ACCESS_TOKEN); if (null == accessToken) { throw new AuthException("401", "無token,請從新登陸"); } JwtManager.me().verifyJwtToken(accessToken); // 當前登陸用戶@CurrentUser //request.setAttribute(CurrentUserConstants.CURRENT_USER, user); return true; } /* (non-Javadoc) * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView) * 請求處理以後進行調用,可是在視圖被渲染以前(Controller方法調用以後) */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } /* (non-Javadoc) * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception) * 在整個請求結束以後被調用,也就是在DispatcherServlet 渲染了對應的視圖以後執行(主要是用於進行資源清理工做) */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } }
2. 配置攔截器 攔截哪些請求 web
package com.antong.api.config.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.antong.api.interceptor.AuthenticationInterceptor; /** * Copyright: Copyright (c) 2018 zq_tuo * * @ClassName: WebMvcConfigurer.java * @Description: mvc配置 * @version: v1.0.0 * @author: tuozq * @date: 2018年6月27日 上午10:46:34 * Modification History: * Date Author Version Description *---------------------------------------------------------* * 2018年6月27日 tuozq v1.0.0 修改緣由 */ @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { // token權限攔截器 registry.addInterceptor(authenticationInterceptor()).addPathPatterns("/**") .excludePathPatterns( "/api/user/doLogin", //登陸請求不進行攔截 "/swagger-resources", "/v2/api-docs", "/configuration/**" //swagger2相關請求不攔截 ); super.addInterceptors(registry); } /** * 全局token權限攔截器 * @return */ @Bean public AuthenticationInterceptor authenticationInterceptor() { return new AuthenticationInterceptor(); } }