1.创建拦截器(如token验证拦截器)
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. 配置拦截器 拦截哪些请求
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 修改原因 */@Configurationpublic 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(); }}