16 package org.openkilda.northbound.utils;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.core.annotation.AnnotationUtils;
23 import org.springframework.web.method.HandlerMethod;
24 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
26 import java.util.concurrent.TimeUnit;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
38 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
40 if (!supports(handler)) {
44 HandlerMethod handlerMethod = (HandlerMethod) handler;
46 if (annotation == null) {
47 Class<?> handlerClass = handlerMethod.getMethod().getDeclaringClass();
48 annotation = AnnotationUtils.findAnnotation(handlerClass,
ExtraAuthRequired.class);
49 if (annotation == null) {
54 long currentAuth = System.currentTimeMillis();
56 final String extraAuthHeader = request.getHeader(EXTRA_AUTH);
59 extraAuth = Long.parseLong(extraAuthHeader);
60 }
catch (NumberFormatException ex) {
61 LOGGER.warn(
"Invalid {} header: {}", EXTRA_AUTH, extraAuthHeader);
63 response.getWriter().write(
"Invalid Auth: " + currentAuth);
64 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
68 if (Math.abs(currentAuth - extraAuth) > TimeUnit.SECONDS.toMillis(120)) {
72 response.getWriter().write(
"Invalid Auth: " + currentAuth);
73 response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
80 private boolean supports(Object handler) {
81 return handler instanceof HandlerMethod;
static final String EXTRA_AUTH