1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
public class TestFiler extends HandlerInterceptorAdapter {
private final Logger logger = LoggerFactory.getLogger(TestFiler.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.info("request请求地址path[{}] uri[{}]", request.getServletPath(), request.getRequestURI()); HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); UserAuthenticate userAuthenticate = method.getAnnotation(UserAuthenticate.class); if(Objects.nonNull(userAuthenticate)) { Long userId = getUserId(request); if (userId == null || (userAuthenticate.permission()) && !checkAuth(userId, request.getRequestURI())) { throw new FastRuntimeException(20001, "No access"); } } return true; }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { super.postHandle(request, response, handler, modelAndView); }
@Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { super.afterCompletion(request, response, handler, ex); }
private Long getUserId(HttpServletRequest request){ request.getHeader("H-User-Token"); Long userId = 1L; String userMobile = "1888888888"; request.setAttribute(HeaderCons.USER_ID, userId); request.setAttribute(HeaderCons.USER_MOBILE, userMobile); return userId; }
private boolean checkAuth(Long userId, String requestURI){ return true; }
}
|