Spring Security HTTP Basic for RESTFul and FormLogin (Cookies) for web - Annotations

30,585

Waited for 2 days and didn't get any help here. But my research provided me a solution :)

Solution

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Autowired
    private AuthenticationProvider authenticationProvider;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider);
    }

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable()
                    .antMatcher("/api/**")
                    .authorizeRequests()
                        .anyRequest().hasAnyRole("ADMIN", "API")
                        .and()
                    .httpBasic();
        }
    }

    @Configuration
    @Order(2)
    public static class FormWebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/css/**", "/js/**", "/img/**", "/lib/**");
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable() //HTTP with Disable CSRF
                    .authorizeRequests() //Authorize Request Configuration
                        .antMatchers("/connect/**").permitAll()
                        .antMatchers("/", "/register").permitAll()
                        .antMatchers("/admin/**").hasRole("ADMIN")
                        .anyRequest().authenticated()
                        .and() //Login Form configuration for all others
                    .formLogin()
                        .loginPage("/login").permitAll()
                        .and() //Logout Form configuration
                    .logout().permitAll();
        }
    }
}
Share:
30,585
Faraj Farook
Author by

Faraj Farook

Find me on LinkedIn or Github

Updated on November 27, 2020

Comments

  • Faraj Farook
    Faraj Farook over 3 years

    In Specific

    I want to have HTTP Basic authentication ONLY for a specific URL pattern.

    In Detail

    I'm creating an API interface for my application and that needs to be authenticated by simple HTTP basic authentication. But other web pages should not be using HTTP basic but rather a the normal form login.

    Current Configuration - NOT Working

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http //HTTP Security
                .csrf().disable() //Disable CSRF
                .authorizeRequests() //Authorize Request Configuration
                    .antMatchers("/connect/**").permitAll()
                    .antMatchers("/", "/register").permitAll()
                    .antMatchers("/admin/**").hasRole("ADMIN")
                    .antMatchers("/api/**").hasRole("API")
                    .anyRequest().authenticated()
                .and() //HTTP basic Authentication only for API
                    .antMatcher("/api/**").httpBasic()
               .and() //Login Form configuration for all others
                    .formLogin().loginPage("/login").permitAll()
                .and() //Logout Form configuration
                    .logout().permitAll();
    
    }
    
  • 89n3ur0n
    89n3ur0n over 8 years
    How can I remove that basic http auth popup. I have tried everything but unable to do so.
  • Faraj Farook
    Faraj Farook over 8 years
    don't use httpBasic, use formLogin.
  • 89n3ur0n
    89n3ur0n over 8 years
    tried that with no success, I disabled it by httpBasic().disable() and used formLogin(), but does not seem to be fixed.
  • Faraj Farook
    Faraj Farook over 8 years
    Post your code in a separate question and put the link. don't forget to put your config source code to it.
  • IllSc
    IllSc over 8 years
    Why do you use @Order annotation?
  • BitExodus
    BitExodus almost 8 years
    If you don't use "Order" you will get an error like the following one: Order on WebSecurityConfigurers must be unique
  • Jireugi
    Jireugi over 7 years
    The Order annotation defines the order in which the WebSecurityConfigurerAdapter classes get processed.
  • Admin
    Admin over 7 years
    @FarajFarook tell me please: Is it possible to use your approach to login to rest api by POST, returning 201 on successful and generating session ID ? Help me please, I am fightening with this a long time
  • Kanagavelu Sugumar
    Kanagavelu Sugumar over 6 years
    Could you please elaborate the mistakes and correction that made your expectation ..?