title: Basic and form based authentication with Spring security Javaconfig
body: <p>I'm trying to define two different security configurations for different url patterns, one of them using form login and another one using basic authentication for an api.</p>
  
  <p>The solution I'm looking for is similar to the one explained here <a href="http://meera-subbarao.blogspot.co.uk/2010/11/spring-security-combining-basic-and.html" rel="noreferrer">http://meera-subbarao.blogspot.co.uk/2010/11/spring-security-combining-basic-and.html</a> but I would like to do it using java config.</p>
  
  <p>Thanks in advance.</p>
  
  <p>This is the configuration I currently have:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private UserService userService;
  
      @Override
      protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userService);
      }
  
      @Override
      public void configure(WebSecurity web) throws Exception {
          // Ignore any request that starts with "/resources/".
          web.ignoring().antMatchers("/resources/**");
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeUrls().antMatchers("/", "/index", "/user/**", "/about").permitAll()
          .antMatchers("/admin/**").hasRole("ADMIN")
          .anyRequest().authenticated()
          .and().formLogin()
          .loginUrl("/login")
          .failureUrl("/login-error")
          .loginProcessingUrl("/security_check")
          .usernameParameter("j_username").passwordParameter("j_password")
          .permitAll();
  
          http.logout().logoutUrl("/logout");
          http.rememberMe().rememberMeServices(rememberMeServices()).key("password");
      }
  
      @Bean
      public RememberMeServices rememberMeServices() {
          TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices("password", userService);
          rememberMeServices.setCookieName("cookieName");
          rememberMeServices.setParameter("rememberMe");
          return rememberMeServices;
      }
  }
  </code></pre>