Add Filter To Spring Security To Implement Multi Tenant

I need to update my Spring Security configuration to introduce multi-tenant management (where I get URL for each web request and through a configuration file I retrieve the correct schema). So I add a filter (because with handler the login page doesn't have the correct schema since the handler is called after spring security) to my spring security configuration but now I catch the URL, set the schema but the page still empty and doesn't redirect to login page and also if I write /login no HTML page appears.

This is how I have configured spring security:

@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private DataSource dataSource;
    private RoleServices roleServices;
    private CustomSuccessHandler customSuccessHandler;

    public void configAuthentication(AuthenticationManagerBuilder auth)throws Exception {
        .usersByUsernameQuery("select username,password,enabled from user where username=?")
        .authoritiesByUsernameQuery("select u.username, CONCAT('ROLE_' , r.role) from user u inner join role r on u.idRole = r.idRole where lower(u.username) = lower(?)");

    public PasswordEncoder passwordEncoder(){
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;

    public void configure(WebSecurity web) throws Exception {
        //Spring Security ignores request to static resources such as CSS or JS files.

    protected void configure(HttpSecurity http) throws Exception {
        List<Role> roles=roleServices.getRoles();
        //Retrieve array of roles(only string field without id)
        String[] rolesArray = new String[roles.size()];
        int i=0;
        for (Role role:roles){
            rolesArray[i++] = role.getRole();

           .authorizeRequests() //Authorize Request Configuration
        .and()//Login Form configuration for all others
        //important because otherwise it goes in a loop because login page require authentication and authentication require login page
           .deleteCookies("JSESSIONID", "JSESSIONID")
           .addFilterAfter(new MultiTenancyInterceptor(), BasicAuthenticationFilter.class);


I added MultiTenancyInterceptor filter where I set the Tenant

public class MultiTenancyInterceptor extends OncePerRequestFilter   {

    public void doFilterInternal(HttpServletRequest request,
            HttpServletResponse response,
            FilterChain filterChain)
            throws IOException, ServletException {  
        String url = request.getRequestURL().toString();
        URI uri;
        try {
            uri = new URI(url);
            String domain = uri.getHost();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block


but as I write the controller of login page doesn't receive the call:

@RequestMapping(value = { "/login" }, method = RequestMethod.GET)
public String loginPage(){
    return "login";

Do you see an error in my configure method? If you need further information I can add the other classes. Thanks PS: I noticed that doFilter is called twice for each page request



After the suggest of dur I add the code

filterChain.doFilter(request, response);

at the end of filter method