title: RequestContextHolder.getRequestAttributes() null - Spring Security + Multi Tenant
body: <p>After implementing spring security (basic form authentication), I am unable to get the current request using <code>RequestContextHolder.getRequestAttributes()</code>, which worked fine before. </p>
  
  <p>The problem is that RequestContextHolder.getRequestAttributes() is null and I need to get an extra parametere (the tenant id) from the login request in order to select the correct database. </p>
  
  <p>Here's my code:</p>
  
  <p><strong>Security</strong></p>
  
  <pre><code>@EnableWebSecurity
  public class Security extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private MyUserDetailsService myUserDetailsService;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
  
                  .authorizeRequests()
                  .antMatchers("/assets/**")
                      .permitAll()
                  .anyRequest().authenticated()
                      .and()
                  .formLogin()
                      .loginPage("/dashboard/login")
                      .defaultSuccessUrl("/dashboard/home")
                      .permitAll()
                      .and()
                  .logout()
                      .permitAll();
      }
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth)
              throws Exception {
          auth.authenticationProvider(authenticationProvider());
      }
  
      @Bean
      public DaoAuthenticationProvider authenticationProvider() {
          DaoAuthenticationProvider authProvider
                  = new DaoAuthenticationProvider();
          authProvider.setUserDetailsService(myUserDetailsService);
          authProvider.setPasswordEncoder(passwordEncoder());
          return authProvider;
      }
  
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder(11);
      }
  }
  </code></pre>
  
  <p><strong>CurrentTenantIdentifierResolverImpl</strong></p>
  
  <pre><code>public class CurrentTenantIdentifierResolverImpl implements CurrentTenantIdentifierResolver {
  
      Logger log = LogManager.getLogger(CurrentTenantIdentifierResolverImpl.class);
  
  
      @Override
      public String resolveCurrentTenantIdentifier() {
          ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  
          HttpSession session = attr.getRequest().getSession(false); // true == allow create
          if (session != null) {
              String tenant = (String) session.getAttribute("tenant");
              log.trace("Tenant default resolved in session is: " + tenant);
              if (tenant != null) {
                  return tenant;
              }
          }
  
          String request = attr.getRequest().getRequestURI();
  
          String tenant = attr.getRequest().getParameter("tenant");
          if (request.equals("/dashboard/login") &amp;&amp; tenant != null) {
              return tenant;
          }
  
          //otherwise return default tenant
          log.trace("Tenant default not resolved in session");
          return null;
  
      }
  
      @Override
      public boolean validateExistingCurrentSessions() {
          return true;
      }
  }
  </code></pre>