LoginHandlerInterceptor extends HandlerInterceptorAdapter implements ServletContextAware { @Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception { final Map<String, String> requestParameterValues = new HashMap<String, String>(); for (Enumeration<String> requestParameters = request.getParameterNames(); requestParameters .hasMoreElements();) { final String requestParameter = requestParameters.nextElement(); if (requestParameter.startsWith(ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX)) { String requestValue = request.getParameter(requestParameter); requestParameterValues.put( requestParameter.substring(ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX.length()), requestValue); } } ENV.set(requestParameterValues); String token = request.getHeader(ResourceConstants.TOKEN); String username = request.getHeader(ResourceConstants.USER_NAME); String password = request.getHeader(ResourceConstants.PASSWORD); Properties credentials = new Properties(); if (token != null) { credentials.put(ResourceConstants.TOKEN, token); } else { if (username != null) { credentials.put(ResourceConstants.USER_NAME, username); } if (password != null) { credentials.put(ResourceConstants.PASSWORD, password); } } this.securityService.login(credentials); return true; } LoginHandlerInterceptor(); @VisibleForTesting  LoginHandlerInterceptor(SecurityService securityService); static Map<String, String> getEnvironment(); @Override boolean preHandle(final HttpServletRequest request, final HttpServletResponse response,
      final Object handler); @Override void afterCompletion(final HttpServletRequest request, final HttpServletResponse response,
      final Object handler, final Exception ex); @Override void afterConcurrentHandlingStarted(HttpServletRequest request,
      HttpServletResponse response, Object handler); @Override void setServletContext(ServletContext servletContext);  }
@Test public void preHandle_returnsTrue() throws Exception { HttpServletRequest request = request().build(); assertThat(interceptor.preHandle(request, null, null)) .isTrue(); }
@Test public void preHandle_logsInWithUserNameAndPasswordFromRequestHeaders() throws Exception { HttpServletRequest request = request() .withHeader(USER_NAME, "expected user-name") .withHeader(PASSWORD, "expected password") .build(); interceptor.preHandle(request, null, null); Properties expectedLoginProperties = new Properties(); expectedLoginProperties.setProperty(USER_NAME, "expected user-name"); expectedLoginProperties.setProperty(PASSWORD, "expected password"); verify(securityService, times(1)).login(expectedLoginProperties); }